Managing Services the PowerShell way – Part 5: WMI and CIM

The fifth installment in our series about managing Windows Services with PowerShell explains how to leverage WMI and CIM for configuring remote Services.

Jeffery HicksMVP By Jeffery Hicks - Wed, January 30, 2013 - 0 comments google+ icon

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with 20 years of experience. Follow his blog.

Articles in this series

PowerShell and Services

In the last article, I demonstrated some examples of using Set-Service to configure services. However there are some limitations, especially when working with services on remote machines. Part of the problem is that cmdlets like Get-Service and Set-Service are designed to work with a .NET Framework definition of a service object, System.ServiceProcess.ServiceController. From an IT Pro perspective there are a few things missing such as the account the service is running under. But fortunately we have Windows Management Instrumentation (WMI). We don’t have WMI-based cmdlets specific to services but this really isn’t that complicated.

Using WMI

We can use Get-WmiObject to retrieve an instance of a service object. I’m going to demonstrate using PowerShell 3.0 on Windows 8, but the same commands should work on PowerShell 2.0. It is not that difficult to retrieve all services, which are instances of the Win32_Service class.

Retrieve an instance of a service object with Get-WmiObject

Retrieve an instance of a service object with Get-WmiObject

As you can see in the screenshot above, I formatted the results as a table to make it easier to read. You get the same sort of information, although because this is a different object type, the property names might be different. Let’s look at a single service.

This is merely the default display. There are other properties which you can see with a command like this:

The next screenshot displays the result.

Display Service properties

Display Service properties

Once you know property names you can build some useful queries with the –Filter parameter.

Getting Startup Type

The StartMode property indicates whether a service is meant to auto start or manually start. Once you know this, you can run commands like this.

This will prepare a table, grouped by start mode with new key properties. I’ll let you run it on your own to see the results. Or perhaps you need to find all the services that are configured to auto start, but are currently not running.

I’m querying services locally, but it is just as easy to query one or more remote machines.

Getting Service Account

The other piece of information that we can only retrieve with WMI is the account that the service is running under. In WMI, this is the Startname property.

Of course you can filter on this property.

You can see the result in the screenshot below.

Getting Service account

Getting Service account

This is very handy if you are searching for services running under a specific account, such as domain account.

With one simple command I found services in that are running under some sort of administrator account, which may or may not be what I intend.

Using CIM

If PowerShell 3.0 is an option you can also use the CIM cmdlets to make the same types of queries. The advantage is that CIM uses PowerShell’s remoting port which makes it much more firewall friendly.

Manage Services with CIM cmdlets

Manage Services with CIM cmdlets

Filters work the same way.

Looks I have some Exchange issues to look into. How do I accomplish that with WMI or CIM? That’s what we’ll look at in the next part.

Summary

Using WMI or CIM is a great way for reporting on service configuration in your environment. The Win32_Service class offers a bit more information that is of interest to IT Pros. Plus you can run long running queries with –Asjob or use alternate credentials. There are ways you could also do that with Get-Service but they require a little more work. Next time we’ll look at changing services with WMI and CIM.

Series NavigationManaging Services the PowerShell way – Part 4: Configure Services - Managing Services the PowerShell way – Part 6: Modify through WMI

-1+1 - Rate this post
Loading ... Loading ...
Your question wasn't answered? Ask in the new 4sysops forum!
===Leave a Comment===

Login

Lost your password?