Skip to content

Commit

Permalink
prevent errors when no webhooks to renew
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDuprey committed Feb 25, 2025
1 parent 41d5c03 commit f60d0a7
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions Modules/CIPPCore/Public/Webhooks/Invoke-CIPPGraphWebhookRenewal.ps1
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
function Invoke-CippGraphWebhookRenewal {
$RenewalDate = (Get-Date).AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
$RenewalDate = (Get-Date).AddDays(1).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ')
$body = @{
"expirationDateTime" = "$RenewalDate"
'expirationDateTime' = "$RenewalDate"
} | ConvertTo-Json


$WebhookTable = Get-CIPPTable -TableName webhookTable
$WebhookData = Get-AzDataTableEntity @WebhookTable | Where-Object { $null -ne $_.SubscriptionID -and $_.SubscriptionID -ne '' -and ((Get-Date($_.Expiration)) -le ((Get-Date).AddHours(2))) }
try {
$WebhookData = Get-AzDataTableEntity @WebhookTable | Where-Object { $null -ne $_.SubscriptionID -and $_.SubscriptionID -ne '' -and ((Get-Date($_.Expiration)) -le ((Get-Date).AddHours(2))) }
} catch {
$WebhookData = @()
}

foreach ($UpdateSub in $WebhookData) {
try {
$TenantFilter = $UpdateSub.PartitionKey
if (($WebhookData | Measure-Object).Count -gt 0) {
foreach ($UpdateSub in $WebhookData) {
try {
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($UpdateSub.SubscriptionID)" -tenantid $TenantFilter -type PATCH -body $body -Verbose
$UpdateSub.Expiration = $RenewalDate
$null = Add-AzDataTableEntity @WebhookTable -Entity $UpdateSub -Force
Write-LogMessage -Headers'CIPP' -API 'Renew_Graph_Subscriptions' -message "Renewed Subscription:$($UpdateSub.SubscriptionID)" -Sev "Info" -tenant $TenantFilter

} catch {
# Rebuild creation parameters
$BaseURL = "$(([uri]($UpdateSub.WebhookNotificationUrl)).Host)"
if ($UpdateSub.TypeofSubscription) {
$TypeofSubscription = "$($UpdateSub.TypeofSubscription)"
} else {
$TypeofSubscription = 'updated'
$TenantFilter = $UpdateSub.PartitionKey
try {
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($UpdateSub.SubscriptionID)" -tenantid $TenantFilter -type PATCH -body $body -Verbose
$UpdateSub.Expiration = $RenewalDate
$null = Add-AzDataTableEntity @WebhookTable -Entity $UpdateSub -Force
Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Renewed Subscription:$($UpdateSub.SubscriptionID)" -Sev 'Info' -tenant $TenantFilter

} catch {
# Rebuild creation parameters
$BaseURL = "$(([uri]($UpdateSub.WebhookNotificationUrl)).Host)"
if ($UpdateSub.TypeofSubscription) {
$TypeofSubscription = "$($UpdateSub.TypeofSubscription)"
} else {
$TypeofSubscription = 'updated'
}
$Resource = "$($UpdateSub.Resource)"
$EventType = "$($UpdateSub.EventType)"

Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Recreating: $($UpdateSub.SubscriptionID) as renewal failed." -Sev 'Info' -tenant $TenantFilter
$CreateResult = New-CIPPGraphSubscription -TenantFilter $TenantFilter -TypeofSubscription $TypeofSubscription -BaseURL $BaseURL -Resource $Resource -EventType $EventType -Headers 'GraphSubscriptionRenewal' -Recreate

if ($CreateResult -match 'Created Webhook subscription for') {
Remove-AzDataTableEntity -Force @WebhookTable -Entity $UpdateSub
}
}
$Resource = "$($UpdateSub.Resource)"
$EventType = "$($UpdateSub.EventType)"

Write-LogMessage -Headers'CIPP' -API 'Renew_Graph_Subscriptions' -message "Recreating: $($UpdateSub.SubscriptionID) as renewal failed." -Sev "Info" -tenant $TenantFilter
$CreateResult = New-CIPPGraphSubscription -TenantFilter $TenantFilter -TypeofSubscription $TypeofSubscription -BaseURL $BaseURL -Resource $Resource -EventType $EventType -Headers 'GraphSubscriptionRenewal' -Recreate

if ($CreateResult -match 'Created Webhook subscription for') {
Remove-AzDataTableEntity -Force @WebhookTable -Entity $UpdateSub
}

} catch {
Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Failed to renew Webhook Subscription: $($UpdateSub.SubscriptionID). Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $($_.Exception.message)" -Sev 'Error' -tenant $TenantFilter
}


} catch {
Write-LogMessage -Headers'CIPP' -API 'Renew_Graph_Subscriptions' -message "Failed to renew Webhook Subscription: $($UpdateSub.SubscriptionID). Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $($_.Exception.message)" -Sev "Error" -tenant $TenantFilter
}
}
}

0 comments on commit f60d0a7

Please sign in to comment.