-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathAdd-CIPPScheduledTask.ps1
96 lines (89 loc) · 3.67 KB
/
Add-CIPPScheduledTask.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
91
92
93
94
95
96
function Add-CIPPScheduledTask {
[CmdletBinding()]
param(
[pscustomobject]$Task,
[bool]$Hidden,
$DisallowDuplicateName = $false,
[string]$SyncType = $null,
$Headers
)
$Table = Get-CIPPTable -TableName 'ScheduledTasks'
if ($DisallowDuplicateName) {
$Filter = "PartitionKey eq 'ScheduledTask' and Name eq '$($Task.Name)'"
$ExistingTask = (Get-CIPPAzDataTableEntity @Table -Filter $Filter)
if ($ExistingTask) {
return "Task with name $($Task.Name) already exists"
}
}
$propertiesToCheck = @('Webhook', 'Email', 'PSA')
$PostExecutionObject = ($propertiesToCheck | Where-Object { $task.PostExecution.$_ -eq $true })
$PostExecution = $PostExecutionObject ? ($PostExecutionObject -join ',') : ($Task.PostExecution.value -join ',')
$Parameters = [System.Collections.Hashtable]@{}
foreach ($Key in $task.Parameters.PSObject.Properties.Name) {
$Param = $task.Parameters.$Key
if ($null -eq $Param -or $Param -eq '' -or ($Param | Measure-Object).Count -eq 0) {
continue
}
if ($Param -is [System.Collections.IDictionary] -or $Param.Key) {
$ht = @{}
foreach ($p in $Param.GetEnumerator()) {
$ht[$p.Key] = $p.Value
}
$Parameters[$Key] = [PSCustomObject]$ht
} else {
$Parameters[$Key] = $Param
}
}
if ($Headers) {
$Parameters.Headers = $Headers | Select-Object -Property 'x-forwarded-for', 'x-ms-client-principal', 'x-ms-client-principal-idp', 'x-ms-client-principal-name'
}
$Parameters = ($Parameters | ConvertTo-Json -Depth 10 -Compress)
$AdditionalProperties = [System.Collections.Hashtable]@{}
foreach ($Prop in $task.AdditionalProperties) {
$AdditionalProperties[$Prop.Key] = $Prop.Value
}
$AdditionalProperties = ([PSCustomObject]$AdditionalProperties | ConvertTo-Json -Compress)
if ($Parameters -eq 'null') { $Parameters = '' }
if (!$Task.RowKey) {
$RowKey = (New-Guid).Guid
} else {
$RowKey = $Task.RowKey
}
$Recurrence = if ([string]::IsNullOrEmpty($task.Recurrence.value)) {
$task.Recurrence
} else {
$task.Recurrence.value
}
if ([int64]$task.ScheduledTime -eq 0 -or [string]::IsNullOrEmpty($task.ScheduledTime)) {
$task.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds
}
$excludedTenants = if ($task.excludedTenants.value) {
$task.excludedTenants.value -join ','
}
$entity = @{
PartitionKey = [string]'ScheduledTask'
TaskState = [string]'Planned'
RowKey = [string]$RowKey
Tenant = $task.TenantFilter.value ? "$($task.TenantFilter.value)" : "$($task.TenantFilter)"
excludedTenants = [string]$excludedTenants
Name = [string]$task.Name
Command = [string]$task.Command.value
Parameters = [string]$Parameters
ScheduledTime = [string]$task.ScheduledTime
Recurrence = [string]$Recurrence
PostExecution = [string]$PostExecution
AdditionalProperties = [string]$AdditionalProperties
Hidden = [bool]$Hidden
Results = 'Planned'
}
if ($SyncType) {
$entity.SyncType = $SyncType
}
try {
Add-CIPPAzDataTableEntity @Table -Entity $entity -Force
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
return "Could not add task: $ErrorMessage"
}
return "Successfully added task: $($entity.Name)"
}