Configure a DSC Resource to restart Configure a DSC Resource to restart powershell powershell

Configure a DSC Resource to restart


Normally when I install .Net it works without rebooting, but if you want to force your configuration to reboot it after it installs it you can do the following. It won't work for drift (.net being removed after initial installation.) During configuration drift, the configuration will still install .net, but the script resource I added to reboot will believe it has already rebooted.

The DependsOn is very important here, you don't want this script running before the WindowsFeature has run successfully.

configuration WebServer{    WindowsFeature NetFramework45Core    {        Name = "Net-Framework-45-Core"        Ensure = "Present"    }    Script Reboot    {        TestScript = {            return (Test-Path HKLM:\SOFTWARE\MyMainKey\RebootKey)        }        SetScript = {            New-Item -Path HKLM:\SOFTWARE\MyMainKey\RebootKey -Force             $global:DSCMachineStatus = 1         }        GetScript = { return @{result = 'result'}}        DependsOn = '[WindowsFeature]NetFramework45Core'    }    }


To get $global:DSCMachineStatus = 1 working, you first need to configure Local Configuration Manager on the remote node to allow Automatic reboots. You can do it like this:

Configuration ConfigureRebootOnNode{    param (        [Parameter(Mandatory=$true)]        [ValidateNotNullOrEmpty()]        [String]        $NodeName    )    Node $NodeName    {        LocalConfigurationManager        {            RebootNodeIfNeeded = $true        }    }}ConfigureRebootOnNode -NodeName myserver Set-DscLocalConfigurationManager .\ConfigureRebootOnNode -Wait -Verbose

(code taken from colin's alm corner)