Share via


Get-WmiObject Win32_Service for a list of services

Question

Friday, March 22, 2013 6:17 PM

I would like to get the details on services, using a list of services.

This returns all the services on a server: 

Get-WmiObject Win32_Service | Format-Table Name, DisplayName, State, StartMode, StartName

I would like to return only the services defined in a list.  I tried this:

Get-WmiObject Win32_Service -Name (get-content P:\Data\SQLservices.txt) | Format-Table Name, DisplayName, State, StartMode, StartName

But receive this error:

Get-WmiObject : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Namespace'. Specified method is not supported.
At line:1 char:34

  • Get-WmiObject Win32_Service -Name <<<<  (get-content P:\Data\SQLservices.txt) | Format-Table Name, DisplayName, State, StartMode, StartName
        + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Any suggestions??

-Al H

All replies (14)

Friday, March 22, 2013 8:11 PM âś…Answered | 1 vote

My fault, I didn't read your previous post where you actually gave the contents of your list. -Contains doesn't support wildcards, which explains why you are getting partial results. Your going to have to do something like this instead:

Contents of file (replace * with %)

MSSQL%SQLSERVERAGENTMSSQLServerOLAPServiceSQLBrowsermsftesql%MsDtsServerReportServer%SQLWriterMSSQLServerADHelper100MSSQLFDLauncher%MsDtsServer100SQLAgent%MSOLAP%

Script

get-content P:\Data\SQLservices.txt | ForEach-Object {     Get-WmiObject Win32_Service -Filter "Name Like '$_'" | Format-Table Name, DisplayName, State, StartMode, StartName}

The output is not as clean because of the multiple WMI calls though...

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat


Friday, March 22, 2013 6:31 PM

get-content P:\Data\SQLservices.txt | ForEach-Object { Get-WmiObject Win32_Service -Name $_ | Format-Table Name, DisplayName, State, StartMode, StartName}

Actually that won't work as the -Name parameter takes in a string not a string array, so you will have to do something like above, but this is also assuming that your text file has the services listed line by line

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


Friday, March 22, 2013 6:48 PM | 1 vote

Although it goes against my own personal use of Where-Object with Get-WMIObject, this one would make only one WMI call and then filter using Where-Object.

Get-WmiObject Win32_Service | Where {    (get-content P:\Data\SQLservices.txt) -Contains $_.Name} | Format-Table Name, DisplayName, State, StartMode, StartName

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat


Friday, March 22, 2013 7:08 PM

get-content P:\Data\SQLservices.txt | ForEach-Object { Get-WmiObject Win32_Service -Name $_ | Format-Table Name, DisplayName, State, StartMode, StartName}

Actually that won't work as the -Name parameter takes in a string not a string array, so you will have to do something like above, but this is also assuming that your text file has the services listed line by line

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Clayman2 - Thanks, but that code produced the follow error:

Get-WmiObject : Invalid parameter
At line:1 char:69

  • get-content P:\Data\SQLservices.txt | ForEach-Object { Get-WmiObject <<<<  Win32_Service -Name $_ | Format-Table Name, DisplayName, State, StartMode, StartName}
        + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
        + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

-Al H


Friday, March 22, 2013 7:20 PM

How are the contents of your text file, also, does it contain the actual service name or the display name of a service?

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


Friday, March 22, 2013 7:23 PM | 1 vote

Thats my fault actually, -Name isnt a parameter for Get-WMIObject

I would try Boe's method, but remove the -Name $_

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


Friday, March 22, 2013 7:24 PM

It looks like it's the Service Name.  Here are the contents of the file: 

MSSQL*
SQLSERVERAGENT
MSSQLServerOLAPService
SQLBrowser
msftesql*
MsDtsServer
ReportServer*
SQLWriter
MSSQLServerADHelper100
MSSQLFDLauncher*
MsDtsServer100
SQLAgent*
MSOLAP*

-Al H


Friday, March 22, 2013 7:28 PM | 1 vote

Thats my fault actually, -Name isnt a parameter for Get-WMIObject

I would try Boe's method, but remove the -Name $_

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Thanks for the catch! I removed the -Name parameter from the code example.

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat


Friday, March 22, 2013 7:43 PM

Thanks guys..  Unfortunately there is a problem with Boe's method.  It doesn't return all the results.

I was originally using this code:

Get-Service -Name (get-content P:\Data\SQLservices.txt) -ErrorAction SilentlyContinue 

Which produced these results:

*Status      Name                      DisplayName                           *
*                                                          *
*Running  MsDtsServer100        SQL Server Integration Services 10.0  *
*Running  MSSQL$SQL172        SQL Server (SQL172)                   *
*Stopped  MSSQLServerADHe... SQL Active Directory Helper Service   *
*Running  SQLAgent$SQL172   SQL Server Agent (SQL172)             *
*Running  SQLBrowser             SQL Server Browser                    *
*Running  SQLWriter                SQL Server VSS Writer      *

But I decided that my boss would want to know if the service had been disabled or set to manual.  So I found Get-WmiObject Win32_Service, which provides the StartMode, and StartName.

Although as I mentioned above, the script is missing some (important) results using the same text file:

*Name                                     DisplayName                                  State        StartMode    StartName                        *
*                                                                                                                         *
*MsDtsServer100                    SQL Server Integration Services... Running    Auto            AD-ENT\SOMEDOMAINTACCOUNT          *
*MSSQLServerADHelper100    SQL Active Directory Helper Ser...   Stopped     Disabled      NT AUTHORITY\NETWORKSERVICE      *
*SQLBrowser                          SQL Server Browser                       Running     Auto          NT AUTHORITY\LOCALSERVICE        *
*SQLWriter                             SQL Server VSS Writer                   Running     Auto          LocalSystem   *

Any idea why they are different and if I can have 'the best of both worlds' ??

-Al H


Friday, March 22, 2013 7:59 PM

I can't seem to duplicate your issue. I ran my command against a SQL server and got back all of the results. Can you post the command you are running using Get-WMIObject even if it is the same as what I posted. I just want to make sure nothing was lost along the way.

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat


Friday, March 22, 2013 8:01 PM

I can't seem to duplicate your issue. I ran my command against a SQL server and got back all of the results. Can you post the command you are running using Get-WMIObject even if it is the same as what I posted. I just want to make sure nothing was lost along the way.

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat

You bet:

Get-WmiObject Win32_Service | Where {
    (get-content P:\Data\SQLservices.txt) -Contains $_.Name
} | Format-Table Name, DisplayName, State, StartMode, StartName

-Al H


Friday, March 22, 2013 8:19 PM

My fault, I didn't read your previous post where you actually gave the contents of your list. -Contains doesn't support wildcards, which explains why you are getting partial results. Your going to have to do something like this instead:

Contents of file (replace * with %)

MSSQL%
SQLSERVERAGENT
MSSQLServerOLAPService
SQLBrowser
msftesql%
MsDtsServer
ReportServer%
SQLWriter
MSSQLServerADHelper100
MSSQLFDLauncher%
MsDtsServer100
SQLAgent%
MSOLAP%

Script

get-content P:\Data\SQLservices.txt | ForEach-Object { 
    Get-WmiObject Win32_Service -Filter "Name Like '$_'" | Format-Table Name, DisplayName, State, StartMode, StartName
}

Boe Prox
Blog | PoshWSUS | PoshPAIG | PoshChat

That did it Boe, thanks!!

-Al H


Wednesday, May 20, 2015 5:59 PM

I would need just the opposite.  So specifying a list of exceptions which I don't want to have in my results. This is my script.  Can anyone help me to filter these services 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 "}"


Wednesday, May 20, 2015 6:11 PM

I would need just the opposite.  So specifying a list of exceptions which I don't want to have in my results. This is my script.  Can anyone help me to filter these services 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 "}"

You should create your own thread, rather then use someone else's

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet