PowerShell DSC: Remotely Configuring a Node to “RebootNodeIfNeeded”

I’ve started to experiment a bit with some PowerShell DSC – mostly because it’s now supported in Release Management (in Update 3 CTP at least).

Sometimes when you apply a configuration to a node (machine), the node requires a reboot (for example adding .NET4.5 requires the node to reboot). You can configure the node to reboot immediately (instead of just telling you “a reboot is required”) by changing a setting in the node’s LocalConfigurationManager. Of course, since this is configuration, it’s tempting to try to do this in a DSC script – for example:

Configuration SomeConfig
{
   Node someMachine
   {
      LocalConfigurationManager
      {
         RebootNodeIfNeeded = $true
      }
   }
}

This configuration “compiles” to a mof file and you can apply it successfully. However, it doesn’t actually do anything.

Set-DscLocalConfigurationManager on a Remote Node

Fortunately, there is a way to change the settings on the LocalConfigurationManager remotely – you use the cmdlet Set-DscLocalConfigurationManager with a CimSession object (i.e. you invoke it remotely). I stumbled across this when looking at the documentation for DSC Local Configuration Manager where the very last sentence says “To see the current Local Configuration Manager settings, you can use the Get-DscLocalConfigurationManager cmdlet. If you invoke this cmdlet with no parameters, by default it will get the Local Configuration Manager settings for the node on which you run it. To specify another node, use the CimSession parameter with this cmdlet.”

Here’s a script that you can modify to set “RebootNodeIfNeeded” on any node:

Configuration ConfigureRebootOnNode
{
    param (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]
        $NodeName
    )

    Node $NodeName
    {
        LocalConfigurationManager
        {
            RebootNodeIfNeeded = $true
        }
    }
}

Write-Host "Creating mofs"
ConfigureRebootOnNode -NodeName fabfiberserver -OutputPath .\rebootMofs

Write-Host "Starting CimSession"
$pass = ConvertTo-SecureString "P2ssw0rd" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("administrator", $pass)
$cim = New-CimSession -ComputerName fabfiberserver -Credential $cred

Write-Host "Writing config"
Set-DscLocalConfigurationManager -CimSession $cim -Path .\rebootMofs -Verbose

# read the config settings back to confirm
Get-DscLocalConfigurationManager -CimSession $cim

Just replace “fabfiberserver” with your node name and .\ the script. The last line of the script reads back the LocalConfigurationManager settings on the remote node, so you should see the RebootNodeIfNeeded setting is true.

image

Happy configuring!


© 2021. All rights reserved.