SCCMClientActions.psm1

#Enumeration of SCCM client actions
Enum SCCMClientAction
{
    ApplicationDeploymentEvaluationCylce = 121
    DiscoveryDataCollectionCycle = 3
    FileCollectionCycle = 10
    HardwareInventoryCycle = 1
    MachinePolicyRetrievalCycle = 21
    MachinePolicyEvaluationCycle = 22
    SoftwareInventoryCycle = 2
    SoftwareMeteringUsageReportCycle = 31
    SoftwareUpdatesAssignmentsEvaluationCycle = 108
    SoftwareUpdateScanCycle = 113
    StateMessageRefresh = 111
    UserPolicyRetrievalCycle = 26
    UserPolicyEvaluationCycle = 27
    WindowsInstallersSourceListUpdateCycle = 32
}

<#
.Synopsis
   Starts an SCCM action on a local or remote Windows device
.DESCRIPTION
   Starts an SCCM action on a local or remote Windows device
 
   Corresponds to the options presented under the 'Actions' tab in the 'Configuration Manager' Control Panel applet
.EXAMPLE
   Start-SCCMClientAction -ClientAction HardwareInventoryCycle
 
   This command starts the hardware inventory Cycle action on the local device
.EXAMPLE
   Start-SCCMClientAction -ClientAction SoftwareInventoryCycle -ComputerName DeviceA
 
   This command starts the software Inventory Cycle action on a single remote device
.EXAMPLE
   Start-SCCMClientAction -ClientAction SoftwareUpdateScanCycle -ComputerName DeviceA, DeviceB
 
   This command starts the software Update Scan Cycle action on multiple devices
.INPUTS
   SCCMClientAction, String
   This cmdlet requires an SCCMClientAction enum value and optionally a string array of computernames
.NOTES
   User must have ability to remotely manage WMI on the target device and the device must be available to contact
#>

function Start-SCCMClientAction
{
    [CmdletBinding(SupportsShouldProcess=$true, 
                  PositionalBinding=$true,
                  ConfirmImpact='Medium')]
    [Alias('sacma')]
    [OutputType([String])]
    Param
    (
        #SCCM Client Action to start
        [Parameter(Mandatory=$true, 
                   ValueFromPipeline=$false,
                   ValueFromPipelineByPropertyName=$false,
                   Position=0)]
        [ValidateNotNull()]                
        [SCCMClientAction]$clientAction,

        #Computer name(s) to run the action against
        [Parameter(Mandatory=$false, 
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true,  
                   Position=1)]
        [ValidateNotNullorEmpty()]                
        [string[]]$ComputerName
    )

    Begin{}
    Process
    {
        <#Convert enum to a GUID format
        (number padded to 3 digits with leading zeros, starting guid with 0s, dashes and curly braces)#>

        $actionGuid = "{00000000-0000-0000-0000-000000000$($clientAction.ToString('d').PadLeft(3,'0'))}"
        #If no computername was specified then default to localhost
        if ($null -eq $ComputerName){ $ComputerName = 'localhost' }
            #iterate through each computer provided
            foreach ($computer in $ComputerName){
                #Set up wmi query
                if ($pscmdlet.ShouldProcess($computer, "Run SCCM action: $clientAction")){                         
                    $params = @{
                        'Namespace'    = 'root\CCM'
                        'ClassName'    = 'SMS_Client'
                        'MethodName'   = 'TriggerSchedule'
                        'Arguments'    = @{ sScheduleID = $actionGuid }
                    }
                    try{
                        #Run action any computers specified as localhost without a computername parameter
                        if($computer -eq 'localhost'){                        
                            Invoke-CimMethod @params -ErrorAction Stop | Out-Null
                        }
                        else{
                            Invoke-CimMethod @params -ComputerName $computer -ErrorAction Stop | Out-Null
                        }
                        #Invoke-WmiMethod @params | Out-Null
                        Write-Verbose "$clientAction action successfully queued with SCCM client on $computer"
                    }
                    catch{
                        throw 'Unable to queue SCCM client action'
                    }    
                }   
            }
    }
    End{}
}