get-services and also display server names
-
Friday, June 29, 2012 9:28 AM
I got a pshell script below that queries a list of servers and display a specific service status. It works fine but I would also like to output the servername. How do I go about it? any idea?
#####BEGIN#####
$server=get-content "D:\server_list.txt"
$result=@()
ForEach ($objitem in $server) {
$result += get-service -ComputerName $objitem | where {$_.DisplayName -eq "IIS Admin Service"}
}$result
###END###
PoSH newbie, BaSH Oldie
All Replies
-
Friday, June 29, 2012 9:56 AM
Like this?
#####BEGIN##### $server=get-content "D:\server_list.txt" $result=@() ForEach ($objitem in $server) { $result += $objitem $result += get-service -ComputerName $objitem | where {$_.DisplayName -eq "IIS Admin Service"} } $result ###END###Grant Ward, a.k.a. Bigteddy
-
Friday, June 29, 2012 1:48 PM
I'd do it like this:
#####BEGIN##### $server=get-content "D:\server_list.txt" $result = @() ForEach ($objitem in $server) { $result += get-service -ComputerName $objitem -Name "IIS Admin Service" |Select-Object MachineName,Status,Name,DisplayName } $result ###END###KarlWhen you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Marked As Answer by navarro_aries Monday, July 02, 2012 2:29 AM
-
Friday, June 29, 2012 1:49 PM
That can be shortened by eliminating the intermediate variable Server as well:
#####BEGIN##### $result = @() ForEach ($objitem in get-content "D:\server_list.txt") { $result += get-service -ComputerName $objitem -Name "IIS Admin Service" |Select-Object MachineName,Status,Name,DisplayName } $result ###END###KarlWhen you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"}) -
Monday, July 02, 2012 2:32 AM
adding the machinename in the select-object did the trick. I didnt know it support that attribute, because if you just do get-service -computername <server> | fl the result will not show the computername...after i did get-member I saw a value machinename. I've added it in the select-object as what was advised in this thread and boom it displayed the server names...
thanks again for your help folks...
PoSH newbie, BaSH Oldie
-
Monday, July 02, 2012 2:38 PM
Glad to help :)
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"}) -
Monday, July 02, 2012 4:47 PM
adding the machinename in the select-object did the trick. I didnt know it support that attribute, because if you just do get-service -computername <server> | fl the result will not show the computername...after i did get-member I saw a value machinename. I've added it in the select-object as what was advised in this thread and boom it displayed the server names...
thanks again for your help folks...
PoSH newbie, BaSH Oldie
I was remiss in not addressing the " if you just do get-service -computername <server> | fl the result will not show the computername..." part of your post.
You can do get-service -computername <server> | fl * and see the MachineName property.
Whenever you think something might be available, and aren't seeing it, use both the Format-List * and Get-Member tricks :)
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer
My Blog: http://unlockpowershell.wordpress.com
My Book: Windows PowerShell 2.0 Bible
My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})- Marked As Answer by navarro_aries Tuesday, July 10, 2012 6:57 AM

