-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathInvoke-CMClientJob.ps1
59 lines (55 loc) · 1.74 KB
/
Invoke-CMClientJob.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
#region Initialize-CMClientJob
<#
.SYNOPSIS
This is a helper function that starts and manages background jobs for the all
functions in this module.
.DESCRIPTION
.PARAMETER OriginatingFunction
The function where the job request came from. This is used to keep track of
which background jobs were started by which function.
.PARAMETER ScriptBlock
This is the scriptblock that is passed to the system.
.PARAMETER Computername
The computer name that the function is connecting to. This is used to keep track
of the functions initiated on computers.
.PARAMETER ScheduleID
This is the schedule ID to designate which client action to initiate. This is needed
because it has to be sent to the background job scriptblock.
.EXAMPLE
.NOTES
#>
function Initialize-CMClientJob {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$OriginatingFunction,
[Parameter(Mandatory = $true)]
[scriptblock]$ScriptBlock,
[Parameter(Mandatory = $true)]
[string]$Computername,
[Parameter(Mandatory = $true)]
[string]$ScheduleID
)
Begin {
## The total number of jobs that can be concurrently running
$MaxJobThreads = 75
## How long to wait when the max job threads has been met to start another job
$JobWaitSecs = 1
}
Process {
try {
Write-Verbose "Starting job `"$ComputerName - $OriginatingFunction`"..."
Start-Job -ScriptBlock $ScriptBlock -Name "$ComputerName - $OriginatingFunction" -ArgumentList $Computername, $ScheduleID | Out-Null
While ((Get-Job -state running).count -ge $MaxJobThreads) {
Write-Verbose "Maximum job threshold has been met. Waiting $JobWaitSecs second(s) to try again...";
Start-Sleep -Seconds $JobWaitSecs
}
} catch {
Write-Error $_.Exception.Message
}
}
End {
}
}
#endregion