Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: service status using powershell

$
0
0

Dave pretty much nails it.

$service is a collection of services.

To add a specific service, you'll have to first pick it out of the collection like so:

# This is a Wmi query filter
$service = get-wmiobject Win32_service -computername $strComputer -Filter { Name = 'serviceName' }
 
# This is a where-object filter with PSv2 syntax. I'm assuming this is the version you're using since you use Get-WmiObject?
# Note: It's much slower than using a Wmi query filter. 
$service = get-wmiobject Win32_service -computername $strComputer | Where { $_.Name -eq 'serviceName' }

To add every service you'll have to do

foreach ($strComputer in $colComputers)
{
    $service = get-wmiobject Win32_service -computername $strComputer
 
    foreach($s in $service) {
        $c.Cells.Item($intRow,1) = $s.name
        $c.Cells.Item($intRow,2) = $s.state
 
        $intRow = $intRow + 1
    }
}

On a completely different note, may I suggest you don't use single letter variable for anything but iterators? It may not be an issue right with this script, but as they get bigger and longer, eventually you'll find yourself scratching your head over why everything is called $a, $b, $c, $d and $e.


Viewing all articles
Browse latest Browse all 13067

Trending Articles