|
1 | 1 | function Add-CIPPScheduledTask { |
2 | | - [CmdletBinding()] |
| 2 | + [CmdletBinding(DefaultParameterSetName = 'Default')] |
3 | 3 | param( |
| 4 | + [Parameter(Mandatory = $true, ParameterSetName = 'Default')] |
4 | 5 | [pscustomobject]$Task, |
| 6 | + |
| 7 | + [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
5 | 8 | [bool]$Hidden, |
| 9 | + |
| 10 | + [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
6 | 11 | $DisallowDuplicateName = $false, |
| 12 | + |
| 13 | + [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
7 | 14 | [string]$SyncType = $null, |
| 15 | + |
| 16 | + [Parameter(Mandatory = $false, ParameterSetName = 'RunNow')] |
| 17 | + [switch]$RunNow, |
| 18 | + |
| 19 | + [Parameter(Mandatory = $true, ParameterSetName = 'RunNow')] |
| 20 | + [string]$RowKey, |
| 21 | + |
| 22 | + [Parameter(Mandatory = $false, ParameterSetName = 'Default')] |
| 23 | + [Parameter(Mandatory = $false, ParameterSetName = 'RunNow')] |
8 | 24 | $Headers |
9 | 25 | ) |
10 | 26 |
|
11 | 27 | $Table = Get-CIPPTable -TableName 'ScheduledTasks' |
12 | | - if ($DisallowDuplicateName) { |
13 | | - $Filter = "PartitionKey eq 'ScheduledTask' and Name eq '$($Task.Name)'" |
14 | | - $ExistingTask = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) |
15 | | - if ($ExistingTask) { |
16 | | - return "Task with name $($Task.Name) already exists" |
| 28 | + |
| 29 | + if ($RunNow.IsPresent -and $RowKey) { |
| 30 | + try { |
| 31 | + $Filter = "PartitionKey eq 'ScheduledTask' and RowKey eq '$($RowKey)'" |
| 32 | + $ExistingTask = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) |
| 33 | + $ExistingTask.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds |
| 34 | + $ExistingTask.TaskState = 'Planned' |
| 35 | + Add-CIPPAzDataTableEntity @Table -Entity $ExistingTask -Force |
| 36 | + Write-LogMessage -headers $Headers -API 'RunNow' -message "Task $($ExistingTask.Name) scheduled to run now" -Sev 'Info' -Tenant $ExistingTask.Tenant |
| 37 | + return "Task $($ExistingTask.Name) scheduled to run now" |
| 38 | + } catch { |
| 39 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 40 | + Write-LogMessage -headers $Headers -API 'RunNow' -message "Could not run task: $ErrorMessage" -Sev 'Error' |
| 41 | + return "Could not run task: $ErrorMessage" |
| 42 | + } |
| 43 | + } else { |
| 44 | + if ($DisallowDuplicateName) { |
| 45 | + $Filter = "PartitionKey eq 'ScheduledTask' and Name eq '$($Task.Name)'" |
| 46 | + $ExistingTask = (Get-CIPPAzDataTableEntity @Table -Filter $Filter) |
| 47 | + if ($ExistingTask) { |
| 48 | + return "Task with name $($Task.Name) already exists" |
| 49 | + } |
17 | 50 | } |
18 | | - } |
19 | 51 |
|
20 | | - $propertiesToCheck = @('Webhook', 'Email', 'PSA') |
21 | | - $PostExecutionObject = ($propertiesToCheck | Where-Object { $task.PostExecution.$_ -eq $true }) |
22 | | - $PostExecution = $PostExecutionObject ? ($PostExecutionObject -join ',') : ($Task.PostExecution.value -join ',') |
23 | | - $Parameters = [System.Collections.Hashtable]@{} |
24 | | - foreach ($Key in $task.Parameters.PSObject.Properties.Name) { |
25 | | - $Param = $task.Parameters.$Key |
| 52 | + $propertiesToCheck = @('Webhook', 'Email', 'PSA') |
| 53 | + $PostExecutionObject = ($propertiesToCheck | Where-Object { $task.PostExecution.$_ -eq $true }) |
| 54 | + $PostExecution = $PostExecutionObject ? ($PostExecutionObject -join ',') : ($Task.PostExecution.value -join ',') |
| 55 | + $Parameters = [System.Collections.Hashtable]@{} |
| 56 | + foreach ($Key in $task.Parameters.PSObject.Properties.Name) { |
| 57 | + $Param = $task.Parameters.$Key |
26 | 58 |
|
27 | | - if ($null -eq $Param -or $Param -eq '' -or ($Param | Measure-Object).Count -eq 0) { |
28 | | - continue |
29 | | - } |
30 | | - if ($Param -is [System.Collections.IDictionary] -or $Param.Key) { |
31 | | - $ht = @{} |
32 | | - foreach ($p in $Param.GetEnumerator()) { |
33 | | - $ht[$p.Key] = $p.Value |
| 59 | + if ($null -eq $Param -or $Param -eq '' -or ($Param | Measure-Object).Count -eq 0) { |
| 60 | + continue |
| 61 | + } |
| 62 | + if ($Param -is [System.Collections.IDictionary] -or $Param.Key) { |
| 63 | + $ht = @{} |
| 64 | + foreach ($p in $Param.GetEnumerator()) { |
| 65 | + $ht[$p.Key] = $p.Value |
| 66 | + } |
| 67 | + $Parameters[$Key] = [PSCustomObject]$ht |
| 68 | + } else { |
| 69 | + $Parameters[$Key] = $Param |
34 | 70 | } |
35 | | - $Parameters[$Key] = [PSCustomObject]$ht |
36 | | - } else { |
37 | | - $Parameters[$Key] = $Param |
38 | 71 | } |
39 | | - } |
40 | 72 |
|
41 | | - if ($Headers) { |
42 | | - $Parameters.Headers = $Headers | Select-Object -Property 'x-forwarded-for', 'x-ms-client-principal', 'x-ms-client-principal-idp', 'x-ms-client-principal-name' |
43 | | - } |
| 73 | + if ($Headers) { |
| 74 | + $Parameters.Headers = $Headers | Select-Object -Property 'x-forwarded-for', 'x-ms-client-principal', 'x-ms-client-principal-idp', 'x-ms-client-principal-name' |
| 75 | + } |
44 | 76 |
|
45 | | - $Parameters = ($Parameters | ConvertTo-Json -Depth 10 -Compress) |
46 | | - $AdditionalProperties = [System.Collections.Hashtable]@{} |
47 | | - foreach ($Prop in $task.AdditionalProperties) { |
48 | | - $AdditionalProperties[$Prop.Key] = $Prop.Value |
49 | | - } |
50 | | - $AdditionalProperties = ([PSCustomObject]$AdditionalProperties | ConvertTo-Json -Compress) |
51 | | - if ($Parameters -eq 'null') { $Parameters = '' } |
52 | | - if (!$Task.RowKey) { |
53 | | - $RowKey = (New-Guid).Guid |
54 | | - } else { |
55 | | - $RowKey = $Task.RowKey |
56 | | - } |
| 77 | + $Parameters = ($Parameters | ConvertTo-Json -Depth 10 -Compress) |
| 78 | + $AdditionalProperties = [System.Collections.Hashtable]@{} |
| 79 | + foreach ($Prop in $task.AdditionalProperties) { |
| 80 | + $AdditionalProperties[$Prop.Key] = $Prop.Value |
| 81 | + } |
| 82 | + $AdditionalProperties = ([PSCustomObject]$AdditionalProperties | ConvertTo-Json -Compress) |
| 83 | + if ($Parameters -eq 'null') { $Parameters = '' } |
| 84 | + if (!$Task.RowKey) { |
| 85 | + $RowKey = (New-Guid).Guid |
| 86 | + } else { |
| 87 | + $RowKey = $Task.RowKey |
| 88 | + } |
57 | 89 |
|
58 | | - $Recurrence = if ([string]::IsNullOrEmpty($task.Recurrence.value)) { |
59 | | - $task.Recurrence |
60 | | - } else { |
61 | | - $task.Recurrence.value |
62 | | - } |
| 90 | + $Recurrence = if ([string]::IsNullOrEmpty($task.Recurrence.value)) { |
| 91 | + $task.Recurrence |
| 92 | + } else { |
| 93 | + $task.Recurrence.value |
| 94 | + } |
63 | 95 |
|
64 | | - if ([int64]$task.ScheduledTime -eq 0 -or [string]::IsNullOrEmpty($task.ScheduledTime)) { |
65 | | - $task.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds |
66 | | - } |
67 | | - $excludedTenants = if ($task.excludedTenants.value) { |
68 | | - $task.excludedTenants.value -join ',' |
69 | | - } |
70 | | - $entity = @{ |
71 | | - PartitionKey = [string]'ScheduledTask' |
72 | | - TaskState = [string]'Planned' |
73 | | - RowKey = [string]$RowKey |
74 | | - Tenant = $task.TenantFilter.value ? "$($task.TenantFilter.value)" : "$($task.TenantFilter)" |
75 | | - excludedTenants = [string]$excludedTenants |
76 | | - Name = [string]$task.Name |
77 | | - Command = [string]$task.Command.value |
78 | | - Parameters = [string]$Parameters |
79 | | - ScheduledTime = [string]$task.ScheduledTime |
80 | | - Recurrence = [string]$Recurrence |
81 | | - PostExecution = [string]$PostExecution |
82 | | - AdditionalProperties = [string]$AdditionalProperties |
83 | | - Hidden = [bool]$Hidden |
84 | | - Results = 'Planned' |
85 | | - } |
86 | | - if ($SyncType) { |
87 | | - $entity.SyncType = $SyncType |
88 | | - } |
89 | | - try { |
90 | | - Add-CIPPAzDataTableEntity @Table -Entity $entity -Force |
91 | | - } catch { |
92 | | - $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
93 | | - return "Could not add task: $ErrorMessage" |
| 96 | + if ([int64]$task.ScheduledTime -eq 0 -or [string]::IsNullOrEmpty($task.ScheduledTime)) { |
| 97 | + $task.ScheduledTime = [int64](([datetime]::UtcNow) - (Get-Date '1/1/1970')).TotalSeconds |
| 98 | + } |
| 99 | + $excludedTenants = if ($task.excludedTenants.value) { |
| 100 | + $task.excludedTenants.value -join ',' |
| 101 | + } |
| 102 | + $entity = @{ |
| 103 | + PartitionKey = [string]'ScheduledTask' |
| 104 | + TaskState = [string]'Planned' |
| 105 | + RowKey = [string]$RowKey |
| 106 | + Tenant = $task.TenantFilter.value ? "$($task.TenantFilter.value)" : "$($task.TenantFilter)" |
| 107 | + excludedTenants = [string]$excludedTenants |
| 108 | + Name = [string]$task.Name |
| 109 | + Command = [string]$task.Command.value |
| 110 | + Parameters = [string]$Parameters |
| 111 | + ScheduledTime = [string]$task.ScheduledTime |
| 112 | + Recurrence = [string]$Recurrence |
| 113 | + PostExecution = [string]$PostExecution |
| 114 | + AdditionalProperties = [string]$AdditionalProperties |
| 115 | + Hidden = [bool]$Hidden |
| 116 | + Results = 'Planned' |
| 117 | + } |
| 118 | + if ($SyncType) { |
| 119 | + $entity.SyncType = $SyncType |
| 120 | + } |
| 121 | + try { |
| 122 | + Add-CIPPAzDataTableEntity @Table -Entity $entity -Force |
| 123 | + } catch { |
| 124 | + $ErrorMessage = Get-NormalizedError -Message $_.Exception.Message |
| 125 | + return "Could not add task: $ErrorMessage" |
| 126 | + } |
| 127 | + return "Successfully added task: $($entity.Name)" |
94 | 128 | } |
95 | | - return "Successfully added task: $($entity.Name)" |
96 | 129 | } |
0 commit comments