Collection Enumeration with ForEach()
In my last article, I introduced you to a collection filtering option that you may not have been familiar with, the Where()
method. This and the related ForEach()
method I will cover today were introduced in PowerShell v4 to support Desired State Configuration (DSC). You might have a configuration file like this:
@{
#Node specific data
#allnodes is an array of hashtables, one per node
AllNodes = @(
@{NodeName = "*"
Features = @("Windows-Server-Backup","PowerShell","SNMP-Service")
FeaturesRemove = @("Telnet-Client","PowerShell-v2","BranchCache")
},
@{NodeName = "SRV1"; Role = "FilePrint","Test"},
@{NodeName = "SRV2"; Role = "Web"}
)
#non-node Specific data. No code allowed
NonNodeData = @{Services = "bits","remoteregistry","wuauserv"}
}
In the configuration, you can use the collection filtering method.
Node $allnodes.Where({ $_.role -eq 'FilePrint' }).Nodename {
WindowsFeature FileServices {
Name = "File-Services"
Ensure = "Present"
IncludeAllSubFeature = $True
}
WindowsFeature PrintServices {
Name = "Print-Services"
Ensure = "Present"
IncludeAllSubFeature = $True
}
} #close node
Want to read the full issue?