Configure a DSC Resource to restart

10,870

Solution 1

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'
    }    
}

Solution 2

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)

Share:
10,870
DamianB
Author by

DamianB

Updated on July 02, 2022

Comments

  • DamianB
    DamianB almost 2 years

    I have a DSC resource that installs dotnet feature and then installs an update to dotnet.

    In the Local Configuration Manager I have set RebootNodeIfNeeded to $true.

    After dotnet installs, it does not request a reboot (even used xPendingReboot module to confirm this).

    Configuration WebServer
    {
    WindowsFeature NetFramework45Core
    {
        Name = "Net-Framework-45-Core"
        Ensure = "Present"
    }
    
    xPendingReboot Reboot
    {
        Name = "Prior to upgrading Dotnet4.5.2"
    }
    
    cChocoPackageInstaller InstallDotNet452
    {
        name = "dotnet4.5.2"
    }
    
    }
    

    This is a problem as dotnet doesn't work properly with our app unless the server has been rebooted and we are trying to make these reboots happen automatically no user input required.

    Is there any way to make a resource push to the localdscmanager (LCM) that it needs a reboot when there's something being installed?

    I have found the below command

     $global:DSCMachineStatus = 1 
    

    Which sets a reboot. but I'm unsure as to how to use it to reboot right after the 4.5 module is installed.