@@ -9,98 +9,76 @@ function Push-CippDriftManagement {
99
1010 Write-Information " Received drift standard item for $ ( $Item.Tenant ) "
1111
12- $Result = Measure-CippTask - TaskName ' DriftManagement' - EventName ' CIPP.DriftCompleted' - Metadata @ {
13- Tenant = $Item.Tenant
14- } - Script {
15- try {
16- $Drift = Get-CIPPDrift - TenantFilter $Item.Tenant
17- if ($Drift.newDeviationsCount -gt 0 ) {
18- $Settings = $Drift.driftSettings
19- $email = $Settings.driftAlertEmail
20- $webhook = $Settings.driftAlertWebhook
21- $CippConfigTable = Get-CippTable - tablename Config
22- $CippConfig = Get-CIPPAzDataTableEntity @CippConfigTable - Filter " PartitionKey eq 'InstanceProperties' and RowKey eq 'CIPPURL'"
23- $CIPPURL = ' https://{0}' -f $CippConfig.Value
24- $Data = $Drift.currentDeviations | ForEach-Object {
25- $currentValue = if ($_.receivedValue -and $_.receivedValue.Length -gt 200 ) {
26- $_.receivedValue.Substring (0 , 200 ) + ' ...'
27- } else {
28- $_.receivedValue
29- }
30- [PSCustomObject ]@ {
31- Standard = $_.standardDisplayName ? $_.standardDisplayName : $_.standardName
32- ' Expected Value' = $_.expectedValue
33- ' Current Value' = $currentValue
34- Status = $_.status
35- }
36- }
37-
38- $GenerateEmail = New-CIPPAlertTemplate - format ' html' - data $Data - CIPPURL $CIPPURL - Tenant $Item.Tenant - InputObject ' driftStandard' - AuditLogLink $drift.standardId
12+ try {
13+ $Drift = Get-CIPPDrift - TenantFilter $Item.Tenant
14+ if ($Drift.newDeviationsCount -gt 0 ) {
15+ $Settings = $Drift.driftSettings
16+ $email = $Settings.driftAlertEmail
17+ $webhook = $Settings.driftAlertWebhook
18+ $CippConfigTable = Get-CippTable - tablename Config
19+ $CippConfig = Get-CIPPAzDataTableEntity @CippConfigTable - Filter " PartitionKey eq 'InstanceProperties' and RowKey eq 'CIPPURL'"
20+ $CIPPURL = ' https://{0}' -f $CippConfig.Value
3921
40- # Measure email alert sending
41- Measure-CippTask - TaskName ' DriftEmailAlert' - EventName ' CIPP.DriftStatus' - Metadata @ {
42- Tenant = $Item.Tenant
43- Section = ' EmailAlert'
44- } - Script {
45- $CIPPAlert = @ {
46- Type = ' email'
47- Title = $GenerateEmail.title
48- HTMLContent = $GenerateEmail.htmlcontent
49- TenantFilter = $Item.Tenant
50- }
51- Write-Host ' Going to send the mail'
52- Send-CIPPAlert @CIPPAlert - altEmail $email
22+ # Process deviations more efficiently with foreach instead of ForEach-Object
23+ $Data = foreach ($deviation in $Drift.currentDeviations ) {
24+ $currentValue = if ($deviation.receivedValue -and $deviation.receivedValue.Length -gt 200 ) {
25+ $deviation.receivedValue.Substring (0 , 200 ) + ' ...'
26+ } else {
27+ $deviation.receivedValue
5328 }
54-
55- # Measure webhook alert sending
56- Measure-CippTask - TaskName ' DriftWebhookAlert' - EventName ' CIPP.DriftStatus' - Metadata @ {
57- Tenant = $Item.Tenant
58- Section = ' WebhookAlert'
59- } - Script {
60- $WebhookData = @ {
61- Title = $GenerateEmail.title
62- ActionUrl = $GenerateEmail.ButtonUrl
63- ActionText = $GenerateEmail.ButtonText
64- AlertData = $Data
65- Tenant = $Item.Tenant
66- } | ConvertTo-Json - Depth 15 - Compress
67- $CippAlert = @ {
68- Type = ' webhook'
69- Title = $GenerateEmail.title
70- JSONContent = $WebhookData
71- TenantFilter = $Item.Tenant
72- }
73- Write-Host ' Sending Webhook Content'
74- Send-CIPPAlert @CippAlert - altWebhook $webhook
29+ [PSCustomObject ]@ {
30+ Standard = $deviation.standardDisplayName ? $deviation.standardDisplayName : $deviation.standardName
31+ ' Expected Value' = $deviation.expectedValue
32+ ' Current Value' = $currentValue
33+ Status = $deviation.status
7534 }
35+ }
7636
77- # Measure PSA alert sending
78- Measure-CippTask - TaskName ' DriftPSAAlert' - EventName ' CIPP.DriftStatus' - Metadata @ {
79- Tenant = $Item.Tenant
80- Section = ' PSAAlert'
81- } - Script {
82- # Always do PSA.
83- $CIPPAlert = @ {
84- Type = ' psa'
85- Title = $GenerateEmail.title
86- HTMLContent = $GenerateEmail.htmlcontent
87- TenantFilter = $Item.Tenant
88- }
89- Send-CIPPAlert @CIPPAlert
90- }
91- return $true
92- } else {
93- Write-LogMessage - API ' DriftStandards' - tenant $Item.Tenant - message " No new drift deviations found for tenant $ ( $Item.Tenant ) " - sev Info
94- return $true
37+ $GenerateEmail = New-CIPPAlertTemplate - format ' html' - data $Data - CIPPURL $CIPPURL - Tenant $Item.Tenant - InputObject ' driftStandard' - AuditLogLink $drift.standardId
38+
39+ # Send email alert if configured
40+ $CIPPAlert = @ {
41+ Type = ' email'
42+ Title = $GenerateEmail.title
43+ HTMLContent = $GenerateEmail.htmlcontent
44+ TenantFilter = $Item.Tenant
45+ }
46+ Write-Information " Sending email alert for tenant $ ( $Item.Tenant ) "
47+ Send-CIPPAlert @CIPPAlert - altEmail $email
48+ # Send webhook alert if configured
49+ $WebhookData = @ {
50+ Title = $GenerateEmail.title
51+ ActionUrl = $GenerateEmail.ButtonUrl
52+ ActionText = $GenerateEmail.ButtonText
53+ AlertData = $Data
54+ Tenant = $Item.Tenant
55+ } | ConvertTo-Json - Depth 5 - Compress
56+ $CippAlert = @ {
57+ Type = ' webhook'
58+ Title = $GenerateEmail.title
59+ JSONContent = $WebhookData
60+ TenantFilter = $Item.Tenant
9561 }
96- Write-Information " Drift management completed for tenant $ ( $Item.Tenant ) "
97- } catch {
98- Write-LogMessage - API ' DriftStandards' - tenant $Item.Tenant - message " Error running Drift Check for tenant $ ( $Item.Tenant ) - $ ( $_.Exception.Message ) " - sev Error - LogData (Get-CippException - Exception $_ )
99- Write-Warning " Error running drift standards for tenant $ ( $Item.Tenant ) - $ ( $_.Exception.Message ) "
100- Write-Information $_.InvocationInfo.PositionMessage
101- throw $_.Exception.Message
62+ Write-Information " Sending webhook alert for tenant $ ( $Item.Tenant ) "
63+ Send-CIPPAlert @CippAlert - altWebhook $webhook
64+ # Always send PSA alert
65+ $CIPPAlert = @ {
66+ Type = ' psa'
67+ Title = $GenerateEmail.title
68+ HTMLContent = $GenerateEmail.htmlcontent
69+ TenantFilter = $Item.Tenant
70+ }
71+ Send-CIPPAlert @CIPPAlert
72+ return $true
73+ } else {
74+ Write-LogMessage - API ' DriftStandards' - tenant $Item.Tenant - message " No new drift deviations found for tenant $ ( $Item.Tenant ) " - sev Info
75+ return $true
10276 }
77+ Write-Information " Drift management completed for tenant $ ( $Item.Tenant ) "
78+ } catch {
79+ Write-LogMessage - API ' DriftStandards' - tenant $Item.Tenant - message " Error running Drift Check for tenant $ ( $Item.Tenant ) - $ ( $_.Exception.Message ) " - sev Error - LogData (Get-CippException - Exception $_ )
80+ Write-Warning " Error running drift standards for tenant $ ( $Item.Tenant ) - $ ( $_.Exception.Message ) "
81+ Write-Information $_.InvocationInfo.PositionMessage
82+ throw $_.Exception.Message
10383 }
104-
105- return $Result
10684}
0 commit comments