Alex
asked on
So close... yet so far, powershell again!
Right
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 :(
$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"
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 :(
ASKER
It's windows XP mate :(
That wouldn't work would it??!?!?!?!? I'm using SC because it's XP
That wouldn't work would it??!?!?!?!? I'm using SC because it's XP
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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"
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.
Open in new window