
We continue to publish translations of articles on the management of Windows services that go to
4sysops.com . In the
previous post , we discussed how to manage Windows services using WMI. But if you are working with PowerShell 3.0, then new CIM cmdlets are available to you. It is easier to work with them - and in this post we will find out why.
So, under the cut translated article from the portal 4sysops.com
Managing Services the PowerShell way - Part 7Previous articles:Manage Windows services using PowerShell. Part 1. Get the status of servicesManage Windows services using PowerShell. Part 2. Stop, start, pause.Manage Windows services using PowerShell. Part 3. Configuring Services with WMI and CIMManage Windows services using PowerShell. Part 4. Changing Services Using WMIStop and start services using CIM cmdlets
One of the important differences between a CIM object and “pure” WMI is that the resultant object has no methods. This means that you cannot directly retrieve (invoke) a method, for example,
StopService () . Use the
Invoke-CimMethod cmdlet
instead . Consider an example for the spooler service on the local computer.
PS C:\> get-ciminstance win32_service -filter "Name='spooler'" ProcessId Name StartMode State Status ExitCode --------- ---- --------- ----- ------ -------- 0 Spooler Manual Stopped OK 1077 WMI, Invoke-CimMethod. PS C:\> get-ciminstance win32_service -filter "Name='spooler'" | Invoke-CimMethod -Name StartService ReturnValue PSComputerName ----------- -------------- 0
')
The operation of this cmdlet is much like
Invoke-WmiMethod . A return value of 0 indicates success. Let's now consider the
previous query , which required to find all services with autostart, but which were not running.
PS C:\> get-ciminstance win32_service -filter "startmode='auto' and state<>'running'" -computer $computers | Invoke-CimMethod -Name StartService –WhatIf
The
$ computers variable includes a list of all computers that interest us. Use
-Whatif .

To make changes, run the command, but without
–WhatIf .
PS C:\> get-ciminstance win32_service -filter "startmode='auto' and state<>'running'" -computer $computers | Invoke-CimMethod -Name StartService
In the screenshot you can see that the problem is with the computer chi-dc02 (ReturnValue is different from 0).

Let's work on what I did in the previous article using Invoke-WmiMethod. I am currently working in PowerShell 3.0, so you can take each service object, run it and create a custom object to output the results.
get-ciminstance win32_service -filter "startmode='auto' and state<>'running'" -computer $computers | foreach { $result = $_ | Invoke-CimMethod -Name StartService #create a custom object [pscustomobject]@{ Result=$Result.ReturnValue Name=$_.Name DisplayName=$_.Displayname Computername=$Result.PSComputername } } | Sort Computername,Name
It became easier, is not it?

You can add other custom properties, such as date and time. To display only errors, slightly change the sorting rules.
... | Sort Computername,Name | Where {$_.result -gt 0} Result Name DisplayName Computername ------ ---- ----------- ------------ 7 VMTools VMware Tools Service chi-dc02 2 gpsvc Group Policy Client chi-win8-01
Change the startup mode (StartMode)
Changing the service property (for example, Startmode) is also easy. For example, I do not need the Remote Access Connection Manager service to be running on the network. So I'm going to turn it off everywhere.
PS C:\> get-ciminstance win32_service -filter "name='rasman'" -comp $computers | Invoke-CimMethod -methodname ChangeStartmode -Arguments @{startmode=
A small difference from
Invoke-WmiMethod - the list of arguments should be in the form of a dictionary object (dictionary object). The difficulty here is in determining the key, which in this case is
StartMode . It is better to consult the documentation for the WMI method and see the name of the parameter. The attentive reader noticed that I used
–MethodName instead of the usual (and previously used) parameter
–Name . –Name is an undocumented short for –MethodName.
Since we use the dictionary object (dictionary object) for method parameters, it is easier to extract the Change method for the service object. In the previous article for the Spooler service, we set the value of the ErrorControl property to Ignore. Let's return its value to Normal using the CIM cmdlets now.
PS C:\> get-ciminstance win32_service -filter "name='spooler'" | Invoke-CimMethod -MethodName Change -Arguments @{ErrorControl=1} ReturnValue PSComputerName ----------- -------------- 0
It is much easier. There is no need to calculate positions and add empty ($ Null) values. Select a parameter method and do away with the case.
Total
Why use CIM cmdlets: more simple remote control and “friendly relations” with firewall. Of course, PowerShell 3.0 is required. Everything you can do to manage services with WMI can be done with CIM cmdlets, often requiring less effort.
I also came across one feature of the CIM cmdlets. Under normal circumstances, the remote computer should also work with PowerShell 3.0. This is true when you pass all instances of a given class. But when you use a filter, as I often do in this article, I can make a request using Get-CimInstance even if PowerShell 2.0 is running on the remote computer! I still find out why. In any case, I remind you that you need to test everything that we consider in a laboratory environment.
In the next article, we will look at credentials management, under which the service is running.
Previous articles:Manage Windows services using PowerShell. Part 1. Get the status of servicesManage Windows services using PowerShell. Part 2. Stop, start, pause.Manage Windows services using PowerShell. Part 3. Configuring Services with WMI and CIMManage Windows services using PowerShell. Part 4. Changing Services Using WMI