DSCResources/MSFT_xPendingReboot/MSFT_xPendingReboot.psm1

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
Function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([Hashtable])]
     param
    (
    [Parameter(Mandatory=$true)]
    [string]$Name,
    
    [Parameter()]
    [bool]$SkipCcmClientSDK
    )

    $ComponentBasedServicingKeys = (Get-ChildItem 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name
    if ($ComponentBasedServicingKeys)
    {
        $ComponentBasedServicing = $ComponentBasedServicingKeys.Split("\") -contains "RebootPending"
    }
    else
    {
        $ComponentBasedServicing = $false
    }

    $WindowsUpdateKeys = (Get-ChildItem 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name
    if ($WindowsUpdateKeys)
    {
        $WindowsUpdate = $WindowsUpdateKeys.Split("\") -contains "RebootRequired"
    }
    else
    {
        $WindowsUpdate = $false
    }

    $PendingFileRename = (Get-ItemProperty 'hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0
    $ActiveComputerName = (Get-ItemProperty 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
    $PendingComputerName = (Get-ItemProperty 'hklm:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
    $PendingComputerRename = $ActiveComputerName -ne $PendingComputerName

    

    if (-not $SkipCcmClientSDK)
    {
        $CCMSplat = @{
            NameSpace='ROOT\ccm\ClientSDK'
            Class='CCM_ClientUtilities'
            Name='DetermineIfRebootPending'
            ErrorAction='Stop'
        }
        
        Try {
            $CCMClientSDK = Invoke-WmiMethod @CCMSplat
        }
        Catch
        {
            Write-Warning "Unable to query CCM_ClientUtilities: $_"
        }
    }

    $SCCMSDK = ($CCMClientSDK.ReturnValue -eq 0) -and ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)

    return @{
    Name = $Name
    ComponentBasedServicing = $ComponentBasedServicing
    WindowsUpdate = $WindowsUpdate
    PendingFileRename = $PendingFileRename
    PendingComputerRename = $PendingComputerRename
    CcmClientSDK = $SCCMSDK
    }
}

Function Set-TargetResource
{
    [CmdletBinding()]
     param
    (
    [Parameter(Mandatory=$true)]
    [string]$Name,
    [bool]$SkipComponentBasedServicing,
    [bool]$SkipWindowsUpdate,
    [bool]$SkipPendingFileRename,
    [bool]$SkipPendingComputerRename,
    [bool]$SkipCcmClientSDK
    )

    $global:DSCMachineStatus = 1
}

Function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([Boolean])]
     param
    (
    [Parameter(Mandatory=$true)]
    [string]$Name,
    [bool]$SkipComponentBasedServicing,
    [bool]$SkipWindowsUpdate,
    [bool]$SkipPendingFileRename,
    [bool]$SkipPendingComputerRename,
    [bool]$SkipCcmClientSDK
    )

    $status = Get-TargetResource $Name -SkipCcmClientSDK $SkipCcmClientSDK

    if(-not $SkipComponentBasedServicing -and $status.ComponentBasedServicing)
    {
        Write-Verbose 'Pending component based servicing reboot found.'
        return $false
    }

    if(-not $SkipWindowsUpdate -and $status.WindowsUpdate)
    {
        Write-Verbose 'Pending Windows Update reboot found.'
        return $false
    }

    if(-not $SkipPendingFileRename -and $status.PendingFileRename)
    {
        Write-Verbose 'Pending file rename found.'
        return $false
    }

    if(-not $SkipPendingComputerRename -and $status.PendingComputerRename)
    {
        Write-Verbose 'Pending computer rename found.'
        return $false
    }

    Write-Verbose 'No pending reboots found.'
    return $true
}

Export-ModuleMember -Function *-TargetResource

$regRebootLocations = $null