I am having problems to group an array of objects by Environment. The following script show three environments with servers in each one. Now, I am trying to loop those objects by groups but getting errors with the Environment names. The following is my sample logic:
$array = @(("DEVELOPMENT", ("SRV1")), ("QA", ("SRV2", "SRV3")), ("PRODUCTION", ("SRV4", "SRV5")))
ForEach ($system in $array) {
$envName = $array[0]
ForEach ($hostname in $system[1]) {
Write-Host ("Result for " + $hostname + " in " + $envName)
}
}
The variable name $envName always returns the same wrong result.
[0]:"DEVELOPMENT"
[1]:"SRV1"
How can I group and loop $array[0] and $system[1] in the following way?
DEVELOPMENT = SRV1
QA = SRV2, SRV3
PRODUCTION = SRV4, SRV5
$envName = $array[0]you should write$envName = $system[0].