Skip to content

Commit f386254

Browse files
extra logging
1 parent c96e480 commit f386254

File tree

1 file changed

+88
-61
lines changed

1 file changed

+88
-61
lines changed

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

Lines changed: 88 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,98 @@ function Push-CippDriftManagement {
99

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

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
21-
$Data = $Drift.currentDeviations | ForEach-Object {
22-
$currentValue = if ($_.receivedValue -and $_.receivedValue.Length -gt 200) {
23-
$_.receivedValue.Substring(0, 200) + '...'
24-
} else {
25-
$_.receivedValue
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+
}
2636
}
27-
[PSCustomObject]@{
28-
Standard = $_.standardDisplayName ? $_.standardDisplayName : $_.standardName
29-
'Expected Value' = $_.expectedValue
30-
'Current Value' = $currentValue
31-
Status = $_.status
37+
38+
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $Item.Tenant -InputObject 'driftStandard' -AuditLogLink $drift.standardId
39+
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
3253
}
33-
}
3454

35-
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $Item.Tenant -InputObject 'driftStandard' -AuditLogLink $drift.standardId
36-
$CIPPAlert = @{
37-
Type = 'email'
38-
Title = $GenerateEmail.title
39-
HTMLContent = $GenerateEmail.htmlcontent
40-
TenantFilter = $Item.Tenant
41-
}
42-
Write-Host 'Going to send the mail'
43-
Send-CIPPAlert @CIPPAlert -altEmail $email
44-
$WebhookData = @{
45-
Title = $GenerateEmail.title
46-
ActionUrl = $GenerateEmail.ButtonUrl
47-
ActionText = $GenerateEmail.ButtonText
48-
AlertData = $Data
49-
Tenant = $Item.Tenant
50-
} | ConvertTo-Json -Depth 15 -Compress
51-
$CippAlert = @{
52-
Type = 'webhook'
53-
Title = $GenerateEmail.title
54-
JSONContent = $WebhookData
55-
TenantFilter = $Item.Tenant
56-
}
57-
Write-Host 'Sending Webhook Content'
58-
Send-CIPPAlert @CippAlert -altWebhook $webhook
59-
#Always do PSA.
60-
$CIPPAlert = @{
61-
Type = 'psa'
62-
Title = $GenerateEmail.title
63-
HTMLContent = $GenerateEmail.htmlcontent
64-
TenantFilter = $Item.Tenant
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
75+
}
76+
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
6595
}
66-
Send-CIPPAlert @CIPPAlert
67-
return $true
68-
} else {
69-
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "No new drift deviations found for tenant $($Item.Tenant)" -sev Info
70-
return $true
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
71102
}
72-
Write-Information "Drift management completed for tenant $($Item.Tenant)"
73-
} catch {
74-
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "Error running Drift Check for tenant $($Item.Tenant) - $($_.Exception.Message)" -sev Error -LogData (Get-CippException -Exception $_)
75-
Write-Warning "Error running drift standards for tenant $($Item.Tenant) - $($_.Exception.Message)"
76-
Write-Information $_.InvocationInfo.PositionMessage
77-
throw $_.Exception.Message
78103
}
104+
105+
return $Result
79106
}

0 commit comments

Comments
 (0)