Powershell – How to Use PowerShell DSC to Keep Services on Windows Server Running

dscpowershellservicewindows-server-2016

I've recently noticed that our Windows Server 2016 VMs on Azure sometimes fail to start some of their services after rebooting the system (for example ADFS, Remote Desktop Broker and others) even though they're set to Automatic and I've set the recovery options to restart the service in case of failure. This happens even when a long period of time has passed since the reboot, so it's not a matter of waiting. When I go and start the service manually it works fine.

What I'd like to do is use PowerShell DSC to ensure that every service that has a startup type set to Automatic / Automatic Delayed is always running. I found the following blog post which explains how to do this for an individual service, but how would I go about doing it for ANY service that has the startup set to Automatic?

I found another useful post here but it's a regular PowerShell script and I'd like to do that with DSC instead.

Thank you.


Update: I've used the following DSC Script resource:

SetScript = {
            $service = Get-WmiObject -Class Win32_Service -Filter "startmode = 'auto' AND state != 'running'"
            foreach ($svc in $service)
                {
                    Start-Service $svc.Name
                }
        }
TestScript = {
            $service = Get-WmiObject -Class Win32_Service -Filter "startmode = 'auto' AND state != 'running'"
            if ($null -eq $service)
                {
                    return $true
                }
            else
                {
                    return $false
                }
        }

Best Answer

You cant do that with powershell dsc unless you use a script step or a list of services.