Author Posts

November 11, 2014 at 10:29 am

Hey there everyone. Me again. I've been writing more and more functions in my "SCCM Toolbox" for the admins in my environment, and thought I'd start sharing them here. I post snippets on my blog as well, but figure since this gets a little more exposure, that I'd share with the class. 🙂 Let me know if these are useful and I'll keep posting them!

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 
       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

November 11, 2014 at 10:37 am

Absolutely – thank you! Your'e welcome to contact me if you'd like to post these as blog articles, and I definitely recommend submitting to the TechNet Script Gallery as well.

November 11, 2014 at 10:53 am

Will,

Thank you!. You might want to look into Adam Bertram's module which he posted into the TechEd Gallery and his blog a couple of month ago. It includes additional functions to manage the CMClient remotely.

https://gallery.technet.microsoft.com/scriptcenter/ConfigMgr-Client-Action-16a364a5

CMClient Module: Invoke Common ConfigMgr Client Actions via Powershell

November 11, 2014 at 12:57 pm

Many thanks to both of you gents! I'll definitely send you some notes Don! And thanks for posting those links Daniel. I definitely need to work on my use of Try-Catch like Mr. Bertram's post. I'll keep exploring. 🙂

December 15, 2015 at 2:11 pm

Shouldn't the function name in this code be Invoke-CMClientAction as is listed in the example text?

December 16, 2015 at 3:03 pm

Hi William, thanks for taking the time to share. I have been looking for some good SCCM tools – I have some bits here and there cobbled together. I am relatively new to Powershell, but from me – I think your formatting is excellent

July 20, 2016 at 3:43 pm

When loading this into Visual Studio Code, it's balking at not having [CmdletBinding(SupportsShouldProcess=$true,...)], since it is invoked within the foreach() block.

August 17, 2018 at 9:59 am

How to use the given script, can anyone guide me. I'm quite new to powershell.