Question:
Guys I’ve got a script that queries services in remote computers. It runs 2 nested foreach loops, 1 for the computer list and the internal one for the services list. I want to capture the output of the process in a 2-dimensional array.
1 2 3 4 5 6 7 8 9 |
The desired output should be like this: Computer Service State StartupMode ------------------------------------------- data data data data data data data data data data data data data data data data |
I already obtained the data and presented it on the console but have failed to store it in a multicolumn array.
Hash tables can only accept 2 columns.
Answer:
Try This one:
Using WMI because Get-Service
missing the StartupMode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$Array = @() $Computers = "Computer1","Computer2","Computer3" Foreach ($Computer in $Computers) { $Services = Get-WmiObject Win32_Service -ComputerName $Computer | Select name,state,startmode Foreach ($Service in $Services) { $Row = "" | Select Computer,Service,State,StartupMode $Row.Computer = $Computer $Row.Service = $Service.Name $Row.State = $Service.State $Row.StartupMode = $Service.StartMode $Array += $Row } } $Array |