internal/functions/Test-PendingReboot.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function Test-PendingReboot {
    <#
        .SYNOPSIS
            Based on https://github.com/adbertram/PSSqlUpdater
            This function tests various registry values to see if the local computer is pending a reboot
        .NOTES
            Inspiration from: https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
        .EXAMPLE
            PS> Test-PendingReboot
 
            This example checks various registry values to see if the local computer is pending a reboot.
    #>

    [OutputType([bool])]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [DbaInstanceParameter]$ComputerName,
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential,
        [bool]$EnableException = $EnableException
    )
    process {
        try {
            $icmParams = @{
                ComputerName = $ComputerName.ComputerName
                Raw          = $true
            }
            if ($PSBoundParameters.ContainsKey('Credential')) {
                $icmParams.Credential = $Credential
            }

            $OperatingSystem = Get-DbaCmObject -ComputerName $ComputerName.ComputerName -ClassName Win32_OperatingSystem

            # If Vista/2008 & Above query the CBS Reg Key
            If ($OperatingSystem.BuildNumber -ge 6001) {
                $PendingReboot = Invoke-Command2 @icmParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' -Name 'RebootPending' -ErrorAction SilentlyContinue }
                if ($PendingReboot) {
                    Write-Message -Level Verbose -Message 'Reboot pending detected in the Component Based Servicing registry key'
                    return $true
                }
            }

            # Query WUAU from the registry
            $PendingReboot = Invoke-Command2 @icmParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' -Name 'RebootRequired' -ErrorAction SilentlyContinue }
            if ($PendingReboot) {
                Write-Message -Level Verbose -Message 'WUAU has a reboot pending'
                return $true
            }

            # Query PendingFileRenameOperations from the registry
            $PendingReboot = Invoke-Command2 @icmParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction SilentlyContinue }
            if ($PendingReboot -and $PendingReboot.PendingFileRenameOperations) {
                Write-Message -Level Verbose -Message 'Reboot pending in the PendingFileRenameOperations registry value'
                return $true
            }
            return $false
        } catch {
            Stop-Function -Message "Failed to obtain any intormation from remote registry on $ComputerName" -ErrorRecord $_
        }
    }
}