Invoking Your SCCM 2012 Client Remotely – With PowerShell!

powershell3I actually wrote about this some time ago when I first started PowerShell’ing.  But I’ve since improved my script, and I’m sharing it with you now.

Sometimes we need to make a change to our CM clients’, or the environment that they reside in that requires us to trigger an action on the CM client itself.  Be it pull in an updated hardware inventory, force the machine to run a Software Updates Scan, or a Machine Policy to get a machine to detect a new software deployment; it can be a painful experience for an administrator to have to go from machine to machine to run an action.

There are the right-click tools for those that have access to the console, but not everyone does, and not every company will allow you to install them.  Wouldn’t it be nice to have a function that you could include in a module you’ve built, that has the ability to invoke a CM client?  Well, now I have something to offer you:
Function Invoke-CMClient{
<# .SYNOPSIS Invoke commands remotely on an SCCM Client for a system or systems. .DESCRIPTION This function allows you to remotely trigger some of the more common actions that you would find on the local Configuration Manager console. .PARAMETER -ComputerName <string[]> Specifies the target computer for the management operation. Enter a fully qualified domain name, a NetBIOS name, or an IP address. When the remote computer is in a different domain than the local computer, the fully qualified domain name is required. This command defaults to localhost. .PARAMETER -Action Specifies the action to be taken on the SCCM Client. The available actions are as follows: HardwareInv - Runs a Hardware Inventory Cycle on the target machine. SoftwareInv - Runs a Software Inventory Cycle on the target machine. UpdateScan - Runs a Software Updates Scan Cycle on the target machine. MachinePol - Runs a Machine Policy Retrieval and Evaluation Cycle on the target machine. UserPolicy - Runs a User Policy Retrieval and Evaluation Cycle on the target machine. FileCollect - Runs a File Collection Cycle on the target machine. .INPUTS You can pipe a computer name to Invoke-CMClient .EXAMPLE Invoke-CMClientAction -ComputerName server01 -Action HardwareInv The above command will invoke the Configuration Manager Client's Hardware Inventory Cycle on the targeted computer. The return will look like the following: __GENUS : 1 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PARAMETERS __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : server01 __NAMESPACE : ROOT\ccm __PATH : \\server01\ROOT\ccm:__PARAMETERS ReturnValue : PSComputerName : server01 .NOTES Created by Will Anderson. http://lastwordinnerd.com/category/posts/powershell-scripting/ This script is provided AS IS without warranty of any kind. #>

PARAM(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[string[]]$ComputerName = $env:COMPUTERNAME,

[Parameter(Mandatory=$True)]
[ValidateSet('HardwareInv','SoftwareInv','UpdateScan','MachinePol','UserPolicy','DiscoveryInv','FileCollect')]
[string]$Action

)#Close Param

#$Action...actions...actions...
SWITCH ($action) {
'HardwareInv' {$_action = "{00000000-0000-0000-0000-000000000001}"}
'SoftwareInv' {$_action = "{00000000-0000-0000-0000-000000000002}"}
'UpdateScan' {$_action = "{00000000-0000-0000-0000-000000000113}"}
'MachinePol' {$_action = "{00000000-0000-0000-0000-000000000021}"}
'UserPolicy' {$_action = "{00000000-0000-0000-0000-000000000027}"}
'FileCollect' {$_action = "{00000000-0000-0000-0000-000000000010}"}
} #switch

FOREACH ($Computer in $ComputerName){
if ($PSCmdlet.ShouldProcess("$action $computer")) {

Invoke-WmiMethod -ComputerName $Computer -Namespace root\CCM -Class SMS_Client -Name TriggerSchedule -ArgumentList "$_action"

}#if
}#End FOREACH Statement

}#Close Function Invoke-CMClient
This function allows you to invoke an SCCM Client action remotely on a number of computers simultaneously.   I’ve included the actions most commonly used from my perspective, but you can add more if you like.  You can get a good list of them here, or just explore the SMS_Client class for a full list.