Skip to content

Commit e973378

Browse files
Remove logging
1 parent 10b0bd8 commit e973378

File tree

1 file changed

+66
-67
lines changed

1 file changed

+66
-67
lines changed

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

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +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-
}
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
3721

38-
$GenerateEmail = New-CIPPAlertTemplate -format 'html' -data $Data -CIPPURL $CIPPURL -Tenant $Item.Tenant -InputObject 'driftStandard' -AuditLogLink $drift.standardId
39-
$CIPPAlert = @{
40-
Type = 'email'
41-
Title = $GenerateEmail.title
42-
HTMLContent = $GenerateEmail.htmlcontent
43-
TenantFilter = $Item.Tenant
44-
}
45-
Write-Host 'Going to send the mail'
46-
Send-CIPPAlert @CIPPAlert -altEmail $email
47-
$WebhookData = @{
48-
Title = $GenerateEmail.title
49-
ActionUrl = $GenerateEmail.ButtonUrl
50-
ActionText = $GenerateEmail.ButtonText
51-
AlertData = $Data
52-
Tenant = $Item.Tenant
53-
} | ConvertTo-Json -Depth 15 -Compress
54-
$CippAlert = @{
55-
Type = 'webhook'
56-
Title = $GenerateEmail.title
57-
JSONContent = $WebhookData
58-
TenantFilter = $Item.Tenant
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
5928
}
60-
Write-Host 'Sending Webhook Content'
61-
Send-CIPPAlert @CippAlert -altWebhook $webhook
62-
#Always do PSA.
63-
$CIPPAlert = @{
64-
Type = 'psa'
65-
Title = $GenerateEmail.title
66-
HTMLContent = $GenerateEmail.htmlcontent
67-
TenantFilter = $Item.Tenant
29+
[PSCustomObject]@{
30+
Standard = $deviation.standardDisplayName ? $deviation.standardDisplayName : $deviation.standardName
31+
'Expected Value' = $deviation.expectedValue
32+
'Current Value' = $currentValue
33+
Status = $deviation.status
6834
}
69-
Send-CIPPAlert @CIPPAlert
70-
return $true
71-
} else {
72-
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "No new drift deviations found for tenant $($Item.Tenant)" -sev Info
73-
return $true
7435
}
75-
Write-Information "Drift management completed for tenant $($Item.Tenant)"
76-
} catch {
77-
Write-LogMessage -API 'DriftStandards' -tenant $Item.Tenant -message "Error running Drift Check for tenant $($Item.Tenant) - $($_.Exception.Message)" -sev Error -LogData (Get-CippException -Exception $_)
78-
Write-Warning "Error running drift standards for tenant $($Item.Tenant) - $($_.Exception.Message)"
79-
Write-Information $_.InvocationInfo.PositionMessage
80-
throw $_.Exception.Message
36+
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
61+
}
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
8176
}
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
8283
}
83-
84-
return $Result
8584
}

0 commit comments

Comments
 (0)