Tips and tricks for DSC:Modify LCM

There are several ways of modifying the settings of the Local Configuration Manager (LCM):

Either way this is most likely done as an extra step.

Why not adding this step directly into your configuration?

In this example I will show you how you can include this step into your configuration.
Asume you are going to deploy a new Exchange server. Your configuration might be looking like this:

Configuration Exchange
{
   
    Import-DscResource -ModuleName xSystemVirtualMemory
    Import-DscResource -Module xExchange
    Import-DscResource -Module xWebAdministration
    Import-DscResource -Module xPendingReboot
    Import-DscResource -Module xTimeZone
    Import-DSCResource -Module xNetworking
    ######################
    #OS specific settings#
    ######################
    ...
    ###############
    #Copy binaries#
    ###############
    ...
    ###################
    #Main installation#
    ###################
    ...
    ########################
    #Exchange configuration#
    ########################
}

At the beginning the property RebootNodeIfNeeded of LCM should be set to True as the server will need a few reboots until Exchange is installed. So it will look like this:

 

LCM_01As mentioned before you could/should change the property after you’re done in an additional step. Just to be on the safe side and no reboot happens, while the server is productive.

Why not within the configuration?

After the installation you need to reboot the machine anyways. Why not changing the LCM just before the reboot happens?

The idea is to change it at the end of the part Main installation. Within this part you could include a Script resource:

# final set:Disable automatic reboot
Script DisableRebootNodeIfNeeded
{
    SetScript  = {
    $Script =
        '
        $CurrentLCM = Get-DSCLocalConfigurationManager
        #comment out the next 2 lines if no pull server is used
        $DownloadManager = @{} 
        $CurrentLCM.DownloadManagerCustomData | %{$DownloadManager.Add($_.key ,$_.value)}
        Configuration LCMDisableReboot
        {
            Node "localhost"
                {
                    #configure LCM
                    LocalConfigurationManager
                    {
                        CertificateId                  = $CurrentLCM.CertificateId;
                        ConfigurationID                = $CurrentLCM.ConfigurationID;
                        RefreshMode                    = $CurrentLCM.RefreshMode;
                        DownloadManagerName            = $CurrentLCM.DownloadManagerName;
                        RebootNodeIfNeeded             = $False;
                        RefreshFrequencyMins           = $CurrentLCM.RefreshFrequencyMins;
                        ConfigurationModeFrequencyMins = $CurrentLCM.ConfigurationModeFrequencyMins; 
                        ConfigurationMode              = $CurrentLCM.ConfigurationMode;
                        #comment out the next line if no pull server is used
                        DownloadManagerCustomData      = $DownloadManager;
                        AllowModuleOverwrite           = $CurrentLCM.AllowModuleOverwrite;
                    }
                }
        }
        LCMDisableReboot -OutPutPath $env:TEMP
        Set-DscLocalConfigurationManager -Path $env:TEMP -ComputerName "localhost" -Verbose;
        '
        $SB=[scriptblock]::Create($Script)
        Invoke-Command -ScriptBlock $SB
}
    TestScript = {
    If ((Get-DscLocalConfigurationManager).RebootNodeIfNeeded) {
        return @($False)
    }
    Else {
        return @($True)
    }
}
    GetScript  = {
        @{
            TestScript = $TestScript
            SetScript  = $SetScript
            GetScript  = $GetScript
            Result     = (Get-DscLocalConfigurationManager).RebootNodeIfNeeded
        }
}
}

This Script resource reads the actual values from the LCM, changes ONLY the property RebootIfNeeded to false and invokes the configuration.

Asuming that you have as a next step the module xPendingReboot configured, the server will do a reboot after Exchange installation and has then the LCM configured not to reboot anymore.

Note: If the node is not configured for a pull server you need to comment out the lines for the DownloadManagerCustomData!

 

 

2 thoughts on “Tips and tricks for DSC:Modify LCM

    • Hi Yajnas,
      the DSC resource is only checking whether a reboot is needed or not. You cannot enforce a reboot using this module. If you want to reboot a machine based on a trigger, you could use the DSC script resource, which will reboot the machine, when the check fails.
      Ciao,
      Ingo

      Like

Leave a comment