Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Filter out Get-WmiObject Win32_service for a list of services

$
0
0

For monitoring purposes I would like to filter out the results of the script below.

I would like to be able to specify a list of exceptions (of services) which I don't want to have in my results. This is my script.  Can anyone help me to filter these services out?

List of services I would like to filter out:

Service Portable Device Enumerator Service
Service Shell Hardware Detection
Service Office Software Protection Platform
Service WinHTTP Web Proxy Auto-Discovery Service
Service Diagnostic System Host
Service Microsoft Software Shadow Copy Provider
Service Volume Shadow Copy

My script:

# First, fetch the list of auto started services
$colItems = Get-WmiObject Win32_Service | where-object { $_.StartMode -ne 'Disabled' }
# Output the JSON header
Write-Host "{";
write-host "`t ""data"":[";
write-host

#temp variable
$temp = 1

# For each object in the list of services, print the output of the JSON message with the object properties that we are interessted in
foreach ($objItem in $colItems) {
$exe_dir = $objItem.PathName
$exe_dir = $exe_dir -replace '"?(.+\\).+exe.*','$1'
$exe_dir = $exe_dir -replace '\\','/'

$desc_val = $objItem.Description
$desc_val = $desc_val -replace '\"','@'

if ($temp -eq 0){
Write-Host ",";
}
else{
$temp = 0;
}
$line = " { `"{#SERVICESTATE}`":`"" + $objItem.State + "`", `"{#SERVICEDISPLAY}`":`"" + $objItem.DisplayName + "`", `"{#SERVICENAME}`":`"" + $objItem.Name+ "`", `"{#SERVICEDESC}`":`"" + $desc_val + "`", `"{#SERVICEDIR}`":`"" + $exe_dir + "`" }"
Write-Host -NoNewline $line
}

# Close the JSON message
write-host
write-host
write-host "`t ]";
write-host "}"


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>