Link to home
Start Free TrialLog in
Avatar of Alex
AlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

So close... yet so far, powershell again!

Right

$Computers = Get-content "C:\Powershell Projects\TillSC\till.txt"
ForEach ($computer in $computers) {
	& sc.exe \\$Computer Query Browser
} Where-Object  {$.State -Like "Running"} | Select-Object State, @{n='Computer'; e={$Computer}} | Out-file "C:\Powershell Projects\TillSC\results.txt"

Open in new window


The output I get from SC is

SERVICE_NAME: Browser
        TYPE               : 20  WIN32_SHARE_PROCESS  
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

How do I query the state, see it's running and then output that to a results.txt.... My way didn't work :(
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Why use sc?

If you use Get-Service you'll already have an object. With sc you'd have to deal with parsing text.

The loop style you have (foreach) does not have an output pipeline, you can't chain Where-Object like that.

I'd do it like this.
Get-content "C:\Powershell Projects\TillSC\till.txt" | ForEach-Object {
    $computerName = $_
    Get-Service browser -ComputerName $computerName | Select-Object Status, @{n='ComputerName';e={ $computerName }}
} | Where-Object  Status -eq "Running" | Export-Csv "C:\Powershell Projects\TillSC\results.csv"

Open in new window

Avatar of Alex

ASKER

It's windows XP mate :(

That wouldn't work would it??!?!?!?!? I'm using SC because it's XP
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh and if, that, for some reason does not work. We can use WMI (again, with DCOM). Win32_Service predates XP by a good number of years.
Avatar of Alex

ASKER

....... I would use a 3 letter acronym here, but I don't wanna get banned, therefore "For..... ......!!!!! Thanks mate :)
Now, I've just seen you have an obscene number of systems to query. You absolutely need some multi-threading. The lowest hanging fruit is to use Jobs.

This is a small job, let's do 100 at a time.
Get-content "C:\Powershell Projects\TillSC\till.txt" | ForEach-Object {
    $computerName = $_

    while ((Get-Job -State 'Running').Count -gt 100) {
        Start-Sleep -Seconds 10
    }

    Start-Job -ArgumentList $_ -ScriptBlock {
        param ( $ComputerName )

        Get-Service browser -ComputerName $ComputerName | Select-Object Status, @{n='ComputerName';e={ $computerName }}
    }
}

Get-Job |
    Wait-Job |
    Receive-Job |
    Select-Object Status, ComputerName |
    Where-Object  Status -eq "Running" |
    Export-Csv "C:\Powershell Projects\TillSC\results.csv"

Open in new window