go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/reboot/windows.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package reboot
     5  
     6  import (
     7  	"io"
     8  	"strings"
     9  
    10  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    11  	"go.mondoo.com/cnquery/providers/os/resources/powershell"
    12  )
    13  
    14  const (
    15  	WindowsTestComponentServicesReboot = "Test-Path -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending'"
    16  	WindowsTestWsusReboot              = "Test-Path -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\RebootRequired'"
    17  )
    18  
    19  // WinReboot checks if the windows instance requires a reboot
    20  // Excellent resources:
    21  // https://blogs.technet.microsoft.com/heyscriptingguy/2013/06/10/determine-pending-reboot-statuspowershell-style-part-1/
    22  // https://blogs.technet.microsoft.com/heyscriptingguy/2013/06/11/determine-pending-reboot-statuspowershell-style-part-2/
    23  // Brian Wilhite powershell implementation:
    24  // https://github.com/bcwilhite/PendingReboot
    25  type WinReboot struct {
    26  	conn shared.Connection
    27  }
    28  
    29  func (s *WinReboot) Name() string {
    30  	return "Windows Reboot"
    31  }
    32  
    33  func (s *WinReboot) RebootPending() (bool, error) {
    34  	isRebootrequired := false
    35  
    36  	// Query the Component Based Servicing Reg Key
    37  	cmd, err := s.conn.RunCommand(powershell.Wrap(WindowsTestComponentServicesReboot))
    38  	if err != nil {
    39  		return false, err
    40  	}
    41  
    42  	content, err := io.ReadAll(cmd.Stdout)
    43  	if err != nil {
    44  		return false, err
    45  	}
    46  
    47  	if strings.TrimSpace(strings.ToLower(string(content))) == "true" {
    48  		isRebootrequired = true
    49  	}
    50  
    51  	// Query WUAU from the registry
    52  	cmd, err = s.conn.RunCommand(powershell.Wrap(WindowsTestWsusReboot))
    53  	if err != nil {
    54  		return false, err
    55  	}
    56  
    57  	content, err = io.ReadAll(cmd.Stdout)
    58  	if err != nil {
    59  		return false, err
    60  	}
    61  
    62  	if strings.TrimSpace(strings.ToLower(string(content))) == "true" {
    63  		isRebootrequired = true
    64  	}
    65  
    66  	// Query PendingFileRenameOperations from the registry
    67  	// Note: we are not using it since its also used by non-OS specific apps
    68  
    69  	return isRebootrequired, nil
    70  }