Often I can see that DSC adopters use similar code:
Configuration TooVerbose
{
Import-DscResource -ModuleName PSDesiredStateConfiguration
Service abc
{
Name = 'abc'
State = 'Running'
Ensure = 'Present'
}
Service def
{
Name = 'abc'
State = 'Running'
Ensure = 'Present'
}
Service xyz
{
Name = 'abc'
State = 'Running'
Ensure = 'Present'
}
}
The problem is that this is too complex (too verbose) for simple result like for example three services that should run. The proper was is to simplify your code and ensure easy readability and protect yourself against typing errors.
Configuration ServiceRunning
{
Param
(
[string[]]$Name
)
Import-DscResource -ModuleName PSDesiredStateConfiguration
foreach ($nameItem in $name)
{
Service ('ServiceRunning{0}' -f $nameItem)
{
Name = $nameItem
State = 'Running'
Ensure = 'Present'
}
}
}
Configuration NiceAndShort
{
# Looks like a DSC Resource
ServiceRunning ServiceRunning
{
Name = 'abc', 'def', 'xyz'
}
}
NiceAndShort -OutputPath 'D:\SomePlaceOnYourComputer'
