WMI / Powershell and the Configuration Manager Client

A bit about me first :- my name is Anthony Watherston and I’m a Premier Field Engineer in Melbourne. Currently working with Configuration Manager and Orchestrator – plus I try to do everything I can with Powershell!

Last week I had a need for accessing the Configuration Manager client on a remote system. As this was developing an automated solution I didn’t have the option to use the Control Panel applet or the Configuration Manager console to trigger actions on the client. What I needed to find was a way to trigger actions remotely using Powershell – the answer lies in the methods associated with WMI classes.

As WMI is a class based system, each object has associated properties and many of these have methods as well. Below are some examples of how to call some of the built in WMI methods which are part of the Configuration Manager Client namespace. A detailed description of the client and its classes can be found at https://msdn.microsoft.com/en-us/library/jj874139.aspx

Determine if a system has a reboot pending

The Configuration Manager client has a class called CCM_ClientUtilities – in Wbemtest I can access it by connecting to root\ccm\clientsdk. In the diagram below we can see the methods associated with this class.

image

So how do I trigger these methods using Powershell – the Invoke-WMIMethod cmdlet.

To get a list of associated methods I can use the command below: -

Get-WmiObject -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -List | select -ExpandProperty Methods

This gives me the list of methods below:-

image

Now if I want to trigger one of these methods I can use my Invoke-WMIMethod cmdlet and supply the method name.

Invoke-WmiMethod -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name DetermineIfRebootPending

image

There is a lot of information in these results, I only want to know the value of the RebootPending flag. I can wrap my command in parentheses and specify the property name after a dot to only return that value.

(Invoke-WmiMethod -ComputerName $remoteMachine -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name DetermineIfRebootPending).RebootPending

image

Now that I have this information I could force the machine to reboot if the result is true using one of the other methods in this class – RestartComputer.

$rebootPending = (Invoke-WmiMethod -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name DetermineIfRebootPending).RebootPending

if ($rebootPending)
    {
    Invoke-WmiMethod -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name RestartComputer
    }

Of course if I wanted to perform this on a remote machine I can use the WMIObject computername parameter and specify a remote machine.

$remoteMachine = "AW-SVR01"

$rebootPending = (Invoke-WmiMethod -ComputerName $remoteMachine -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name DetermineIfRebootPending).RebootPending

if ($rebootPending)
    {
    Invoke-WmiMethod -ComputerName $remoteMachine -Namespace root\ccm\clientsdk -Class CCM_ClientUtilities -Name RestartComputer
    }

Trigger a client action

I can use the same theory in order to trigger policy retrieval on a machine. Each action is specified by a schedule value, supplying these to the TriggerSchedule method will force the client to perform an action. For instance the script below will trigger a Machine Policy Retrieval & Evaluation cycle on a client.

$trigger = "{00000000-0000-0000-0000-000000000021}"

Invoke-WmiMethod -Namespace root\ccm -Class sms_client -Name TriggerSchedule $trigger

As with the other script I can supply a computer name parameter to the command to have it execute on a remote machine.

Determine assigned site

The last method is one which will allow you to determine a client’s assigned site. I can use the GetAssignedSite method to retrieve the site code.

(Invoke-WMIMethod –Namespace root\ccm –Class SMS_Client –Name GetAssignedSiteCode).sSiteCode

image

There are many more methods available to use within WMI – stay tuned for more.