Tuesday, October 28, 2014

Handling Reboots in a DSC Configuration

When automating a server configuration you’ll eventually run into the scenario where a reboot is required before a component can be installed.  In Desired State Configuration this can be handled with the xPendingReboot PowerShell Module (from the DSC Resource Kit).  Ed Wilson did a nice write-up of this, including some of the pre-reqs here: Use PowerShell DSC to Check Pending Reboot.  

I had to make a couple of changes to his sample before really shooting myself in the foot.  Turns out the first thing DSC does after rebooting is resume the configuration, which in my simplified demo case just told the LCM to go ahead and reboot again.  It took me a while to break that reboot loop (nice one, doofus).

In the sample below I have used Start-Sleep to give you a fighting chance to stop the cycle of reboots Winking smile.  The sample below WILL reboot your server, so don’t do it to your friends or users.

configuration Reboots

{

    # Get this from TechNet Gallery

    Import-DsCResource -ModuleName xPendingReboot

 

    node (hostname)

    {     

        LocalConfigurationManager

        {

            # This is false by default

            RebootNodeIfNeeded = 'true'

        }

       

        Script TestReboot

        {

            GetScript  = { return 'foo'}

            TestScript = { return $false}

            SetScript  = {

                # Insert a delay before the reboot, otherwise the machine will be stuck in a reboot cycle

                Start-Sleep -Seconds (5*60)

 

                # Setting the global:DSCMachineStatus = 1 tells DSC that a reboot is required

                $global:DSCMachineStatus = 1

            }

        }

 

        # Reboot if pending

        xPendingReboot RebootCheck1

        {

            Name = "RebootCheck1"

        }

 

    }

}

 

Set-Location c:\temp

 

Reboots

 

Set-DscLocalConfigurationManager -Path .\Reboots -Verbose

 

Start-DscConfiguration -Verbose -wait -Path .\Reboots -Force

 

 

No comments: