Invoke-CMClientAction.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#region Invoke-CMClientAction
<#
.SYNOPSIS
 This is a helper function that initiates many ConfigMgr client actions.
.DESCRIPTION
 
.PARAMETER Computername
 The system you'd like to initate the action on.
.PARAMETER AsJob
 A switch parameter that initates a job in the background
.PARAMETER ClientAction
 The client action to initiate.
.EXAMPLE
 PS C:\> Invoke-CMClientAction -Computername 'Value1' -AsJob
 This example shows how to call the Invoke-CMClientAction function with named parameters.
.NOTES
#>

function Invoke-CMClientAction {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string]$Computername,
        [Parameter(Mandatory = $true)]
        [ValidateSet('MachinePolicy',
            'DiscoveryData',
            'ComplianceEvaluation',
            'AppDeployment', 
            'HardwareInventory',
            'UpdateDeployment',
            'UpdateScan',
            'SoftwareInventory')]
        [string]$ClientAction,
        [Parameter()]
        [switch]$AsJob
    )
    
    Begin {

        try {
            $ScheduleIDMappings = @{
                'MachinePolicy' = '{00000000-0000-0000-0000-000000000021}';
                'DiscoveryData' = '{00000000-0000-0000-0000-000000000003}';
                'ComplianceEvaluation' = '{00000000-0000-0000-0000-000000000071}';
                'AppDeployment' = '{00000000-0000-0000-0000-000000000121}';
                'HardwareInventory' = '{00000000-0000-0000-0000-000000000001}';
                'UpdateDeployment' = '{00000000-0000-0000-0000-000000000108}';
                'UpdateScan' = '{00000000-0000-0000-0000-000000000113}';
                'SoftwareInventory' = '{00000000-0000-0000-0000-000000000002}';
            }
            $ScheduleID = $ScheduleIDMappings[$ClientAction]
        } catch {
            Write-Error $_.Exception.Message
        }
        
    }
    Process {
        try {
            ## $args[0] represents the computername and $args[1] represents the scheduleID
            $ActionScriptBlock = {
                [void] ([wmiclass] "\\$($args[0])\root\ccm:SMS_Client").TriggerSchedule($args[1]);
                if (!$?) {
                    throw "Failed to initiate a $ClientAction on $($args[0])"
                    }
                }
            
            if ($AsJob.IsPresent) {
                $Params = @{
                    'Computername' = $Computername;
                    'OriginatingFunction' = $ClientAction;
                    'ScriptBlock' = $ActionScriptBlock;
                    'ScheduleID' = $ScheduleID
                }
                Initialize-CMClientJob @Params
            } else {
                Write-Verbose "Initializing $ClientAction on $Computername."
                Invoke-Command -ScriptBlock $ActionScriptBlock -ArgumentList $Computername,$ScheduleID
            }
        } catch {
            Write-Error $_.Exception.Message
        }
        
    }
    End {
        
    }
}
#endregion