PowerShell DSC – Service Example

email me

Show more about DSC...

 

Using a DSC script, set service to Automatic and Running, start configuration, and view configuration.

The basic processing works like this:

make a declarative script > generate mof > apply mof > verify configuration is running


Code

Set-Location "C:\DSC\"

Configuration ServiceExample
{
Import-DscResource -ModuleName PSDesiredStateConfiguration
Node localhost
{

Service ServiceExample
{
Name        = "wuauserv"
StartupType = "Automatic" # Automatic, Disabled, and Manual
State       = "Running"
}
}
}

# create mof
ServiceExample

# load configuration from mof
Start-DscConfiguration -Path ServiceExample -ComputerName localhost -Wait -Verbose -Force

# check configuration
Get-DscConfiguration

 


Notes

Install-Module -Name PSDesiredStateConfiguration
winrm quickconfig

Desired State Configuration Overview for Engineers

PSDesiredStateConfiguration

Get-DscConfiguration

DSC mofs C:\Windows\System32\Configuration

 

Contents of the mof file

/*
@TargetNode='localhost'
@GeneratedBy=Homelab
@GenerationDate=04/16/2019 12:49:32
@GenerationHost=HOMELAB
*/

instance of MSFT_ServiceResource as $MSFT_ServiceResource1ref
{
ResourceID = "[Service]ServiceExample";
State = "Running";
SourceInfo = "C:\\DSC\\ServiceExample.ps1::9::9::Service";
Name = "wuauserv";
StartupType = "Automatic";
ModuleName = "PSDesiredStateConfiguration";

ModuleVersion = "1.0";

ConfigurationName = "ServiceExample";

};
instance of OMI_ConfigurationDocument

{
Version="2.0.0";

MinimumCompatibleVersion = "1.0.0";

CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};

Author="Homelab";

GenerationDate="04/16/2019 12:49:32";

GenerationHost="HOMELAB";

Name="ServiceExample";

};