hashtable from Richard Siddaway

I don’t like to do this – just quote another blog, but Richard Siddaway has almost perfectly described a use of hashtables, and i will just copy and paste the relevant code here.  I cite the original, but the major purpose for this blog is to help ME, the writer, find and organize my knowledge, so this is important to me, and you get to watch over my shoulder.

$data = @{
"server01" = "BITS","NtFrs","MSMQ","Kdc";
"server02" = "MSExchangeAB","W32Time","W3SVC"
}
foreach ($server in $data.Keys){ 
Get-Service -ComputerName $server -Name ($data[$server]) | 
select @{N=”Server”; E={$server}}, Status, Name, DisplayName 
}

Using $server in the foreach loop is of course totally up to  you – it does not ‘echo’ Server01 or 02 in any way.  ($data[$server])  is the value of $server, which is the array of services for that server.  Could all be the same as the other servers, or all different.  the program will error badly on a service not found, but it will go to the next server without a problem.

If you want just the running processes, get those immediately after the get-service like this

Get-Service -ComputerName db-coral -Name * | 
where-object {$_.status -eq "running"} |
select @{N=”Server”; E={$server}}, Status, Name, DisplayName

Finally,  some contents, for example DisplayName, might be truncated.  One solution is to use Format-List (FL) after a pipe at the end to put output into a list so that more data is presented.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.