Okay I am almost there on this one but stuck with the last piece. I want to get a list of services on remote servers, and filter out things like “NT Authority” and such. K. I got that figured out. I can also do it one server at a time. What I want to do is run it against a .txt list using a “foreach” statement and there is where I get stuck. Either I get a dump of stuff that does me do good, or a bunch of errors depending on how I do this. I also need to know what server is running what service. Here’s what I’ve got, hopefully somebody can tell me either 1) this is not possible or 2) where I’m screwed up.

I’ve been trying this and looking stuff up for ages and I’m out of ideas.

$computers = bc "$path to text file containing servers)

foreach ($computer in $computers) {

Get-WMIObject Win32_Service -ComputerName $computer | Where-Object {$.startname -ne “localSystem” }| Where-Object {$.startname -ne “NT AUTHORITY\LocalService” } |Where-Object {$_.startname -ne “NT AUTHORITY\NetworkService” } |select startname, name }

11 Spice ups

Edit: Sorry, that wasn’t the answer.

Edit 2: This seems to work for me.

$computers = Get-Content -Path "C:\Temp2\comp.csv"
Foreach ($computer in $computers){
    Get-WMIObject Win32_Service -ComputerName $computer | Where-Object {$_.startname -ne "LocalSystem" -and $_.startname -ne "NT AUTHORITY\LocalService" -and $_.startname -ne "NT AUTHORITY\NetworkService"} |select startname, name
}

Of course, “C:\Temp2\comp.csv” would need to be replaced with your path.

Edit 3: Adding “Write-Host $computer” as the first line of the foreach loop helps.

2 Spice ups

Keep in mind we should be using ‘cim’ really instead of WMI.

$computers = Get-Content "C:\Temp2\comp.csv"

$serviceReport = 
foreach ($computer in $computers){

    Get-CimInstance Win32_Service -ComputerName $computer  | 
    Where-Object {
        $_.startname -ne "LocalSystem" -and 
        $_.startname -ne "NT AUTHORITY\LocalService" -and 
        $_.startname -ne "NT AUTHORITY\NetworkService" -and
        $_.StartName -notlike ""
    } |
    foreach{
        [pscustomobject]@{
            ComputerName = $computer
            StartName    = $_.StartName
            ServiceName  = $_.name 
        }
    }

}

$serviceReport |
export-csv "serviceReport-$(get-date -f 'yyyyMMdd').csv" -NoTypeInformation

you might want to add error handling and such (if the machine is not reachable, etc)

3 Spice ups

Thanks @ich-ni-san ​ and @alexw ​. I’ll try these out later today or tomorrow. I got end users setting things on fire but you’ve given me two good ideas.

Thanks. I would say I provided an idea, while Neally provided a “good” idea.

Hey. It’s more than I had before, which was bupkiss. You also suggested the write-host computer which I was not aware of.

Thanks man I think you got me pointed in the right direction. There’s a few odds and ends I’m trying to sort out with the .txt file and other such things but I appreciate your help.

care to elaborate?

I have a script that scans active directory and spits out a list of computers. When I reference this list, I get a bunch of errors saying powershell can’t connect to the specified destination and then lists the computer name. When I manually put the servers I want in there, it works.

I mean, I’m after servers in particular but my sup wants to know what other services may or may not be running on whatever computer across the domain. I’m fairly green where it comes to Powershell (thus my inquiry) so maybe this is not possible? I don’t know.

I’ll also add, I frequently use powershell to reference a list of computers and do $thing so I know PS remoting is enabled on our domain. Why it won’t work in this instance is beyond me.

could be a bunch of reason. Depends on the specific error message.

Maybe the account does not have the right rights, maybe it’s long latency and it times out, maybe WinRM is not setup all the way, maybe it’s an older OS and needs WMI rather than CIM , etc.

1 Spice up