Tags

, ,


I use SCCM 2007 and often, after adding PCs to a collection, want to give them a kick so they download their new package.
Here’s a script I cobbled together to do that. Just alter the $PCs array at the bottom:

function Invoke-SCCMAction {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [string[]]$ComputerName = ".",

        [string]$ScheduleID = "MachinePolicyRetrievalEvalCycle"

    )

    BEGIN {
        Write-Verbose "Starting Invoke-SCCMAction."
        $ScheduleIDs = @{
            "DiscoveryDataCollectionCycle" = "{00000000-0000-0000-0000-000000000003}";
            "FileCollectionCycle" = "{00000000-0000-0000-0000-000000000010}";
            "HardwareInventoryCycle" = "{00000000-0000-0000-0000-000000000001}";
            "MachinePolicyRetrievalEvalCycle" = "{00000000-0000-0000-0000-000000000021}";
            "SoftwareInventoryCycle" = "{00000000-0000-0000-0000-000000000002}";
        }
        if ( -not $ScheduleIDs.ContainsKey($ScheduleID) ) {
            Write-Warning "Invalid SCCM Action specified.  Will use the default (Machine Policy)."
            $ScheduleID = "MachinePolicyRetrievalEvalCycle"
        }
    }

    PROCESS {
        $ComputerName | ForEach-Object {
            $Computer = $_.ToUpper()
            if (Test-Connection -Computer $Computer -Count 1 -Quiet) {
                $success = $true
                Try {
                    $SmsClient = [wmiclass]"\\$Computer\root\ccm:SMS_Client"
                    [void]$SmsClient.TriggerSchedule($ScheduleIds.$ScheduleID)
                } Catch {
                    Write-Warning "$Computer - failed to trigger the SCCM Action."
                    $success = $false
                }
                if ($success) {
                    Write-Verbose "$Computer - sucessfully triggered the SCCM Action."
                }
            } else {
                Write-Warning "$Computer - not pingable."
            }
        }
    }

    END {
        Write-Verbose "Completed Invoke-SCCMAction."
    }
}

$PCs= @("hostname1","hostname2") | Invoke-SCCMAction -ScheduleID "MachinePolicyRetrievalEvalCycle" -Verbose
Advertisements