I’m trying to do a quick inventory of what startup account each of my servers is using. I wrote the following script, but its not working. Can someone help?

$servers = get-content ‘c:\servers.txt’

foreach ($line in $servers)

{

$result = get-wmiobject win32_service -Computer $line | Select-Object name, startname, startmode

echo “ServerName is” $line $result

}

It doesn’t appear to like the $line varible after the -computername. If I put just one server name in there it works

2 Spice ups

This is the article you will want to take a look at:

I ran the code on my machine with a text file with a few computers in it. I got errors because of permissions, but it likes the $line variable. Are you getting errors or just a blank line of output?

I am getting a error:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x80070
6BA)
At C:\Users\user\AppData\Local\Temp\92d54e48-cf00-4fd6-81d3-d720d52e4eed.ps
1:9 char:24

  • $result = get-wmiobject <<<< win32_service -Computer $line | Select-Object
    name, startname, startmode
  • CategoryInfo : InvalidOperation: (:slight_smile: [Get-WmiObject], COMExcept
    ion
  • FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands
    .GetWmiObjectCommand

do you have powershell remoting enabled on the other machines?

No. Does it have to be? I can run on every server if I take out the $line variable and replace it with just a server name

Just so happens there’s another script you can try:

http://community.spiceworks.com/scripts/show/1639-get-serviceinformation-extended

1 Spice up

Thanks. I’ll give this a try and see what happens

try this too: Get-Service (Microsoft.PowerShell.Management) - PowerShell | Microsoft Learn

it can specify Computer name and doesn’t depend on powershell remoting.

I got it to work by making the first line an array $servers = @(“server1”,“server2”) instead of $servers = get-content ‘c:\servers.txt’

Not sure why the first one did not work

Didn’t have a text file called servers.txt on your C:\ drive? But you got it going, hope it was what you needed!

I had the text file and the it was being read by the first variable. But for some reason when I tried to use it after -computername it would not work.

Thanks for the suggestions

The Get-content in the script is the default value so you don’t need to specify anything in -ComputerName. If you want to do a different path you would use -ComputerName (Get-Content myfullpath\mytextfile.txt)

Have to surround in paranthesis, otherwise Powershell will get confused with what parameter goes to what command.

2 Spice ups

I swear I tried it 700 times without -computername and it did not work. It’s working now using get-content instead of the array

Thank you

1 Spice up

The basic problems are that PowerShell parses differently then echo expects and there are other ways to get output in PowerShell.

This should work:

$servers = get-content ‘c:\servers.txt’
foreach ($line in $servers) {
$result = get-wmiobject win32_service -Computer $line | Select-Object name, startname, startmode
“ServerName [{0}] Startname [{1}] Startmode [{2}]” -f $line, $result.startname, $result.startmode
}

With PowerShell, you really want to avoid Echo (which is just an alias of Write-Host) whenever you can. The problem with it is you can’t really do anything with the output. If you keep it in object form you can then pipe to Sort, Format-Table, Format-List, Export-CSV, whatever you want. Once you use Write-Host you’re done.

I would eliminate the Write-Host completely, and just output the Select

get-wmiobject win32_service -Computer $line | Select-Object name, startname, startmode

Done. Now the user can take the output and do with it as they want!

Oh, and don’t forget about the code button!

1 Spice up