Skip to content

Commit 67e22b5

Browse files
committed
drift management timer
1 parent ebb3bf3 commit 67e22b5

File tree

4 files changed

+90
-34
lines changed

4 files changed

+90
-34
lines changed

CIPPTimers.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@
8080
"RunOnProcessor": true,
8181
"PreferredProcessor": "standards"
8282
},
83+
{
84+
"Id": "4d80205c-674d-4fc1-abeb-a1ec37e0d796",
85+
"Command": "Start-DriftStandardsOrchestrator",
86+
"Description": "Orchestrator to process drift standards",
87+
"Cron": "0 0 */1 * * *",
88+
"Priority": 5,
89+
"RunOnProcessor": true,
90+
"PreferredProcessor": "standards"
91+
},
8392
{
8493
"Id": "97145a1d-28f0-4bb2-b929-5a43517d23cc",
8594
"Command": "Start-SchedulerOrchestrator",
@@ -197,4 +206,4 @@
197206
"RunOnProcessor": true,
198207
"IsSystem": true
199208
}
200-
]
209+
]

Modules/CIPPCore/Public/Entrypoints/Activity Triggers/Standards/Push-CIPPDriftManagement.ps1

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ function Push-CippDriftManagement {
77
$Item
88
)
99

10-
Write-Information "Received queue item for $($Item.Tenant)"
10+
Write-Information "Received drift standard item for $($Item.Tenant)"
1111

1212
try {
13-
$Drift = Get-CIPPDrift -TenantFilter $item.tenant
13+
$Drift = Get-CIPPDrift -TenantFilter $Item.Tenant
1414
if ($Drift.newDeviationsCount -gt 0) {
1515
$email = (Get-CIPPTenantAlignment -TenantFilter $Item.Tenant | Where-Object -Property standardType -EQ 'drift').standardSettings.email
1616
$webhook = (Get-CIPPTenantAlignment -TenantFilter $Item.Tenant | Where-Object -Property standardType -EQ 'drift').standardSettings.webhook
@@ -30,12 +30,12 @@ function Push-CippDriftManagement {
3030
Status = $_.status
3131
}
3232
}
33-
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $item.tenant -InputObject 'driftStandard'
33+
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $Item.tenant -InputObject 'driftStandard'
3434
$CIPPAlert = @{
3535
Type = 'email'
3636
Title = $GenerateEmail.title
3737
HTMLContent = $GenerateEmail.htmlcontent
38-
TenantFilter = $item.Tenant
38+
TenantFilter = $Item.Tenant
3939
}
4040
Write-Host 'Going to send the mail'
4141
Send-CIPPAlert @CIPPAlert -altEmail $email
@@ -50,7 +50,7 @@ function Push-CippDriftManagement {
5050
Type = 'webhook'
5151
Title = $GenerateEmail.title
5252
JSONContent = $WebhookData
53-
TenantFilter = $item.tenant
53+
TenantFilter = $Item.tenant
5454
}
5555
Write-Host 'Sending Webhook Content'
5656
Send-CIPPAlert @CippAlert -altWebhook $webhook
@@ -70,7 +70,7 @@ function Push-CippDriftManagement {
7070
Write-Information "Drift management completed for tenant $($Item.Tenant)"
7171
} catch {
7272
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "Error running Drift Check for tenant $($Item.Tenant) - $($_.Exception.Message)" -sev Error -LogData (Get-CippException -Exception $_)
73-
Write-Warning "Error running standard $($Item.Standard) for tenant $($Item.Tenant) - $($_.Exception.Message)"
73+
Write-Warning "Error running drift standards for tenant $($Item.Tenant) - $($_.Exception.Message)"
7474
Write-Information $_.InvocationInfo.PositionMessage
7575
throw $_.Exception.Message
7676
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-CIPPStandardsRun.ps1

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,72 @@ function Invoke-CIPPStandardsRun {
1515
[Parameter(Mandatory = $false)]
1616
$TemplateID,
1717
[Parameter(Mandatory = $false)]
18-
$runManually = $false
19-
18+
$runManually = $false,
19+
[Parameter(Mandatory = $false)]
20+
[switch]$Drift
2021
)
21-
Write-Host "Starting process for standards - $($tenantFilter). TemplateID: $($TemplateID) RunManually: $($runManually) Force: $($Force.IsPresent)"
22+
Write-Information "Starting process for standards - $($tenantFilter). TemplateID: $($TemplateID) RunManually: $($runManually) Force: $($Force.IsPresent) Drift: $($Drift.IsPresent)"
2223

23-
$AllTasks = Get-CIPPStandards
24+
if ($Drift.IsPresent) {
25+
Write-Information 'Drift Standards Run'
26+
$AllTasks = Get-CIPPTenantAlignment | Where-Object -Property standardtype -EQ 'drift' | Select-Object -Property Tenant | Sort-Object -Unique -Property Tenant
2427

25-
if ($Force.IsPresent) {
26-
Write-Host 'Clearing Rerun Cache'
27-
Test-CIPPRerun -ClearAll -TenantFilter $TenantFilter -Type 'Standard'
28-
}
28+
#For each item in our object, run the queue.
29+
$Queue = New-CippQueueEntry -Name 'Drift Standards' -TotalTasks ($AllTasks | Measure-Object).Count
2930

30-
#For each item in our object, run the queue.
31-
$Queue = New-CippQueueEntry -Name "Applying Standards ($TenantFilter)" -TotalTasks ($AllTasks | Measure-Object).Count
32-
33-
$InputObject = [PSCustomObject]@{
34-
OrchestratorName = 'StandardsOrchestrator'
35-
QueueFunction = @{
36-
FunctionName = 'GetStandards'
37-
QueueId = $Queue.RowKey
38-
StandardParams = @{
39-
TenantFilter = $TenantFilter
40-
runManually = $runManually
31+
$Batch = foreach ($Task in $AllTasks) {
32+
[PSCustomObject]@{
33+
FunctionName = 'CIPPDriftManagement'
34+
Tenant = $Task.Tenant
4135
}
4236
}
43-
SkipLog = $true
44-
}
45-
if ($TemplateID) {
46-
$InputObject.QueueFunction.StandardParams['TemplateId'] = $TemplateID
37+
38+
$InputObject = [PSCustomObject]@{
39+
OrchestratorName = 'DriftStandardsOrchestrator'
40+
Batch = @($Batch)
41+
SkipLog = $true
42+
}
43+
44+
$InstanceId = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
45+
Write-Information "Started orchestration with ID = '$InstanceId' for drift standards run"
46+
#$Orchestrator = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
47+
return
48+
} else {
49+
Write-Information 'Classic Standards Run'
50+
$AllTasks = Get-CIPPStandards
51+
52+
if ($Force.IsPresent) {
53+
Write-Information 'Clearing Rerun Cache'
54+
Test-CIPPRerun -ClearAll -TenantFilter $TenantFilter -Type 'Standard'
55+
}
56+
57+
if ($AllTasks.Count -eq 0) {
58+
Write-Information "No standards found for tenant $($TenantFilter)."
59+
return
60+
}
61+
62+
#For each item in our object, run the queue.
63+
$Queue = New-CippQueueEntry -Name "Applying Standards ($TenantFilter)" -TotalTasks ($AllTasks | Measure-Object).Count
64+
65+
$InputObject = [PSCustomObject]@{
66+
OrchestratorName = 'StandardsOrchestrator'
67+
QueueFunction = @{
68+
FunctionName = 'GetStandards'
69+
QueueId = $Queue.RowKey
70+
StandardParams = @{
71+
TenantFilter = $TenantFilter
72+
runManually = $runManually
73+
Drift = $Drift.IsPresent
74+
}
75+
}
76+
SkipLog = $true
77+
}
78+
if ($TemplateID) {
79+
$InputObject.QueueFunction.StandardParams['TemplateId'] = $TemplateID
80+
}
81+
Write-Information "InputObject: $($InputObject | ConvertTo-Json -Depth 5 -Compress)"
82+
$InstanceId = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
83+
Write-Information "Started orchestration with ID = '$InstanceId'"
84+
#$Orchestrator = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
4785
}
48-
Write-Host "InputObject: $($InputObject | ConvertTo-Json -Depth 5 -Compress)"
49-
$InstanceId = Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Depth 5 -Compress)
50-
Write-Host "Started orchestration with ID = '$InstanceId'"
51-
#$Orchestrator = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
5286
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function Start-DriftStandardsOrchestrator {
2+
<#
3+
.SYNOPSIS
4+
Start the Drift Standards Orchestrator
5+
#>
6+
[CmdletBinding(SupportsShouldProcess = $true)]
7+
param()
8+
9+
if ($PSCmdlet.ShouldProcess('Start-DriftStandardsOrchestrator', 'Starting Drift Standards Orchestrator')) {
10+
Write-LogMessage -API 'Standards' -message 'Starting Standards Schedule' -sev Info
11+
Invoke-CIPPStandardsRun -Drift
12+
}
13+
}

0 commit comments

Comments
 (0)