Если ваш список представляет собой массив объектов , что-то вроде этого:
$list = [PsCustomObject]@{'ServerName' = 'Server1'; 'IP' = '192.168.1.1'},
[PsCustomObject]@{'ServerName' = 'Server1'; 'IP' = '192.168.1.2'},
[PsCustomObject]@{'ServerName' = 'Server2'; 'IP' = '10.10.10.10'},
[PsCustomObject]@{'ServerName' = 'Server2'; 'IP' = '10.100.10.10'},
[PsCustomObject]@{'ServerName' = 'Server2'; 'IP' = '192.168.1.3'},
[PsCustomObject]@{'ServerName' = 'Server3'; 'IP' = '192.168.3.3'}
, тогда просто используйте Group-Object
и l oop поверх
$list | Group-Object ServerName | ForEach-Object {
# loop over the IP's in the server Group
foreach ($item in $_.Group) {
# add the rule. For Demo just output something
Write-Host "Adding rule for '$($item.ServerName)' on IP: $($item.IP)"
}
}
Результат:
Adding rule for 'Server1' on IP: 192.168.1.1
Adding rule for 'Server1' on IP: 192.168.1.2
Adding rule for 'Server2' on IP: 10.10.10.10
Adding rule for 'Server2' on IP: 10.100.10.10
Adding rule for 'Server2' on IP: 192.168.1.3
Adding rule for 'Server3' on IP: 192.168.3.3