Is this possible?
- Hi all,
I need to retrieve services from 200 windows 2003/windows 2000 servers. I know I can run get-service from local computer. Is there a way that I can get services from these 200 servers and export to the file?
Thank you!
Answers
- Sure;
Use Get-WMIObject
This will get you one servers services:
$services = Get-WmiObject -ComputerName "remote computer" Win32_Service
For a list of servers in (say) C:\servers.txt:
$servers = Get-Content "C:\servers.txt" $ServerServices = @() $ServerObj = @() foreach($server in $servers) { $ServerObj = New-Object psObject $ServerObj |Add-Member -MemberType NoteProperty -Name "Server" -Value $server -Force $services = Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status foreach ($service in $services) { $ServerObj |Add-Member -MemberType NoteProperty -Name $service.Name -Value $service.Status -Force } $ServerServices += $ServerObj } $ServerServices > test.txtKarl- Proposed As Answer byVadims PodansMVPFriday, November 06, 2009 6:00 PM
- Marked As Answer byMervyn ZhangMSFT, ModeratorFriday, November 13, 2009 3:47 AM
All Replies
- Sure;
Use Get-WMIObject
This will get you one servers services:
$services = Get-WmiObject -ComputerName "remote computer" Win32_Service
For a list of servers in (say) C:\servers.txt:
$servers = Get-Content "C:\servers.txt" $ServerServices = @() $ServerObj = @() foreach($server in $servers) { $ServerObj = New-Object psObject $ServerObj |Add-Member -MemberType NoteProperty -Name "Server" -Value $server -Force $services = Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status foreach ($service in $services) { $ServerObj |Add-Member -MemberType NoteProperty -Name $service.Name -Value $service.Status -Force } $ServerServices += $ServerObj } $ServerServices > test.txtKarl- Proposed As Answer byVadims PodansMVPFriday, November 06, 2009 6:00 PM
- Marked As Answer byMervyn ZhangMSFT, ModeratorFriday, November 13, 2009 3:47 AM
- Great thanks.
But, If I do servers.txt from above command,
I got this:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:5 char:26
+ $services = Get-WmiObject <<<< -ComputerName $server Win32_Service |Select-Object Name, Status
Add-Member : Cannot bind argument to parameter 'Name' because it is null.
At line:8 char:54
+ $ServerObj |Add-Member -MemberType NoteProperty -Name <<<< $service.Name -Value $service.Status -Force
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:5 char:26
+ $services = Get-WmiObject <<<< -ComputerName $server Win32_Service |Select-Object Name, Status
Add-Member : Cannot bind argument to parameter 'Name' because it is null.
At line:8 char:54
+ $ServerObj |Add-Member -MemberType NoteProperty -Name <<<< $service.Name -Value $service.Status -Force
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:5 char:26
+ $services = Get-WmiObject <<<< -ComputerName $server Win32_Service |Select-Object Name, Status
Add-Member : Cannot bind argument to parameter 'Name' because it is null.
At line:8 char:54
+ $ServerObj |Add-Member -MemberType NoteProperty -Name <<<< $service.Name -Value $service.Status -Force
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:5 char:26
+ $services = Get-WmiObject <<<< -ComputerName $server Win32_Service |Select-Object Name, Status
Add-Member : Cannot bind argument to parameter 'Name' because it is null.
What am I missing? - Also, I need to find out services running account too? such as some are running local system or some domain users?
I care most is that services are runnng domain user account. - What does your C:\servers.txt file look like?
Karl - Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status, StartName
Karl Great thanks.
One thing to consider here, to start, is that if you have a network or client-side firewall enabled, you'll need to have WMI exceptions in the firewall rules.
But, If I do servers.txt from above command,
I got this:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:5 char:26
How do you manage these 100s of servers? You may have to consider using another method if you're WMI calls are getting blocked... What's the point of going to these 100s of machines to enable WMI, unless you'll be doing lots of these queries going-forward.
As for who the service is running as, Karl mentioned the "StartName" property which should provide that.- If you would rather stay with get-service:
$servers = gc "c:\servers.txt"
$services = get-service -computername $servers | group MachineName
$services |%{$_.Name; $_.Group |%{"`t{0,-30} {1}" -f $_.Name,$_.Status}} | out-file c:\servicestatus.txt -append
Though this can't get you the account they are running under. - Karl,
servers.txt looks like
server1
server2
server3
then I copy your code into powershell
$servers = Get-Content "C:\servers.txt"
$ServerServices = @()
$ServerObj = @()
foreach($server in $servers)
{
$ServerObj = New-Object psObject
$ServerObj |Add-Member -MemberType NoteProperty -Name "Server" -Value $server -Force
$services = Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status
foreach ($service in $services)
{
$ServerObj |Add-Member -MemberType NoteProperty -Name $service.Name -Value $service.Status -Force
}
$ServerServices += $ServerObj
}
$ServerServices > test.txt
Is something I missing? Karl,
Thanks for your time and help.
If I manually put two servers in the notepad and saved as servers .txt file as
server1
server2
and then I copy your script to powershell and got the results. So, it could be my format or the servers do not exist.
If I need to get the serives' running as accounts, you mentioned to use
Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status, StartName
So the final script is:
$servers = Get-Content "C:\servers1.txt"
$ServerServices = @()
$ServerObj = @()
foreach($server in $servers)
{
$ServerObj = New-Object psObject
$ServerObj |Add-Member -MemberType NoteProperty -Name "Server" -Value $server -Force
$services = Get-WmiObject -ComputerName $server Win32_Service |Select-Object Name, Status, StartNameforeach ($service in $services)
{
$ServerObj |Add-Member -MemberType NoteProperty -Name $service.Name -Value $service.Status -Force
}$ServerServices += $ServerObj
}$ServerServices > test.txt
______________________
But, I still can not get the services's run as account only the status. Can you help with the above script?
thank you.- http://msdn.microsoft.com/en-us/library/aa394418%28VS.85%29.aspx
It seems to indicate that this property is available with Windows 2000 and up.
I'd try this locally on one of these servers where you might have PowerShell loaded:
PS>get-wmiobject win32_service|select name,status,startname
Maybe you can't get this value remotely... There are some remote limitations to WMI. - Thanks for the help.
After I ran the final script I indicated above the message from my Vista PC, I did not come out the powershell and then I ran
get-wmiobject win32_service|select name,status,startname
Interesting, I got the services's running acount from one server in the server.txt shown on the screen instead of my local computer
I am a little confused now. I can not pipe out to the text file for running account???
Thanks. - Check this from 2007, it may still be applicable:
http://groups.google.co.uk/group/microsoft.public.win32.programmer.wmi/browse_thread/thread/4cef045b79c1b5cb/1ee2b09a1fa130ab?lnk=st&q=win32_service.startname+is+null&rnum=1&hl=en - Hi,
You may consider other tools such as WMIC.
WMIC - Take Command-line Control over WMI
http://technet.microsoft.com/en-us/library/bb742610.aspx
Q 10. How do I manage remote computers using WMI?
http://www.microsoft.com/technet/scriptcenter/resources/wmifaq.mspx#E4DAC
Thanks.
This posting is provided "AS IS" with no warranties, and confers no rights.

