Detect that the LCM needs a reboot to continue with configuration

Hi,

I´m using a lot of self written DSC Resource and sometimes these resources needs a reboot. So I added $global:DSCMachineStatus = 1 to these scripts. When the DSC Resource executes the $global:DSCMachineStatus = 1 the LCM is waiting for a reboot to continue.
Is it possible to detect that the LCM needs a reboot? Maybe query somehow the DSCMachineStatus? Setting the LCM RebootNodeIfNeeded = $true is not an option.

Hi Thorsten,

I’ve still to do proper digging into it, but I’d imagine that the detection mechanism that is used by the LCM is querying several registry keys that Windows can use to detect if a restart is required :

HKLM\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing- Look for RebootPending
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\ - Look for RebootRequired
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\ - Look for PendingFileRenameOperations

This being so, it should be the case that if any of these are set that the LCM sets the flag to a reboot being required.

So querying of these keys will give you an idea if the LCM is going to flag for a reboot being required.

In PowerShell v5 (and v4, if you’re running Server 2012 R2 or Windows 8.1 with the latest patches), you can run Get-DscLocalConfigurationManager and check the value of the LCMState property. It will be set to “PendingReboot” under these conditions.

I don’t think that feature was available in the original version of DSC that shipped with WMF 4.0, but I don’t have a system online at the moment where I can double check that.

Thanks for your reply. I´ve written a short script to detect that an reboot is pending based on the registry keys as Tim mentioned. But none of these keys represents that the LCM needs a reboot.

$ComponentBasedServicing = (Get-ChildItem 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name.Split("\") -contains "RebootPending"
$WindowsUpdate = (Get-ChildItem 'hklm:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name.Split("\") -contains "RebootRequired"
$PendingFileRename = (Get-ItemProperty 'hklm:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0

Write-Warning "ComponentBasedServicing=$ComponentBasedServicing;WindowsUpdate=$WindowsUpdate;PendingFileRename=$PendingFileRename"

If($ComponentBasedServicing -or $WindowsUpdate -or $PendingFileRename)
{
   Write-Warning "Found pending reboot!"
   return $true
}
return $false

I´m using PowerShell v4, the PendingReboot state is WMF 5.0.
I created a test configuration with 2 self written DSC modules. The Set-TargetResource function of the first resource sets DSCMachineStatus to 1.
After the first DSC resource executes the LCM is still “Idle”. The second DSC resource is triggered after reboot or after a Consistency check…but why…I thought that the LCM waits until a reboot occurs and then continues…Currently I´m really confused.

Could be that you’re just running into annoying behavior of the WMF 4.0 version of DSC. Microsoft has been referring to that as “DSC 0.9” due to things of this nature. If you can get everything onto Server 2012 R2, the patches for that OS will bring you many of the features of WMF 5.0, which eases some of the pain. Otherwise, just deploy WMF 5.0 asap once it is officially released. :slight_smile: