|
1 | 1 | function Invoke-CippGraphWebhookRenewal { |
2 | | - $RenewalDate = (Get-Date).AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffZ") |
| 2 | + $RenewalDate = (Get-Date).AddDays(1).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ss.fffZ') |
3 | 3 | $body = @{ |
4 | | - "expirationDateTime" = "$RenewalDate" |
| 4 | + 'expirationDateTime' = "$RenewalDate" |
5 | 5 | } | ConvertTo-Json |
6 | 6 |
|
7 | | - |
8 | 7 | $WebhookTable = Get-CIPPTable -TableName webhookTable |
9 | | - $WebhookData = Get-AzDataTableEntity @WebhookTable | Where-Object { $null -ne $_.SubscriptionID -and $_.SubscriptionID -ne '' -and ((Get-Date($_.Expiration)) -le ((Get-Date).AddHours(2))) } |
| 8 | + try { |
| 9 | + $WebhookData = Get-AzDataTableEntity @WebhookTable | Where-Object { $null -ne $_.SubscriptionID -and $_.SubscriptionID -ne '' -and ((Get-Date($_.Expiration)) -le ((Get-Date).AddHours(2))) } |
| 10 | + } catch { |
| 11 | + $WebhookData = @() |
| 12 | + } |
10 | 13 |
|
11 | | - foreach ($UpdateSub in $WebhookData) { |
12 | | - try { |
13 | | - $TenantFilter = $UpdateSub.PartitionKey |
| 14 | + if (($WebhookData | Measure-Object).Count -gt 0) { |
| 15 | + foreach ($UpdateSub in $WebhookData) { |
14 | 16 | try { |
15 | | - $null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($UpdateSub.SubscriptionID)" -tenantid $TenantFilter -type PATCH -body $body -Verbose |
16 | | - $UpdateSub.Expiration = $RenewalDate |
17 | | - $null = Add-AzDataTableEntity @WebhookTable -Entity $UpdateSub -Force |
18 | | - Write-LogMessage -Headers'CIPP' -API 'Renew_Graph_Subscriptions' -message "Renewed Subscription:$($UpdateSub.SubscriptionID)" -Sev "Info" -tenant $TenantFilter |
19 | | - |
20 | | - } catch { |
21 | | - # Rebuild creation parameters |
22 | | - $BaseURL = "$(([uri]($UpdateSub.WebhookNotificationUrl)).Host)" |
23 | | - if ($UpdateSub.TypeofSubscription) { |
24 | | - $TypeofSubscription = "$($UpdateSub.TypeofSubscription)" |
25 | | - } else { |
26 | | - $TypeofSubscription = 'updated' |
| 17 | + $TenantFilter = $UpdateSub.PartitionKey |
| 18 | + try { |
| 19 | + $null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/subscriptions/$($UpdateSub.SubscriptionID)" -tenantid $TenantFilter -type PATCH -body $body -Verbose |
| 20 | + $UpdateSub.Expiration = $RenewalDate |
| 21 | + $null = Add-AzDataTableEntity @WebhookTable -Entity $UpdateSub -Force |
| 22 | + Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Renewed Subscription:$($UpdateSub.SubscriptionID)" -Sev 'Info' -tenant $TenantFilter |
| 23 | + |
| 24 | + } catch { |
| 25 | + # Rebuild creation parameters |
| 26 | + $BaseURL = "$(([uri]($UpdateSub.WebhookNotificationUrl)).Host)" |
| 27 | + if ($UpdateSub.TypeofSubscription) { |
| 28 | + $TypeofSubscription = "$($UpdateSub.TypeofSubscription)" |
| 29 | + } else { |
| 30 | + $TypeofSubscription = 'updated' |
| 31 | + } |
| 32 | + $Resource = "$($UpdateSub.Resource)" |
| 33 | + $EventType = "$($UpdateSub.EventType)" |
| 34 | + |
| 35 | + Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Recreating: $($UpdateSub.SubscriptionID) as renewal failed." -Sev 'Info' -tenant $TenantFilter |
| 36 | + $CreateResult = New-CIPPGraphSubscription -TenantFilter $TenantFilter -TypeofSubscription $TypeofSubscription -BaseURL $BaseURL -Resource $Resource -EventType $EventType -Headers 'GraphSubscriptionRenewal' -Recreate |
| 37 | + |
| 38 | + if ($CreateResult -match 'Created Webhook subscription for') { |
| 39 | + Remove-AzDataTableEntity -Force @WebhookTable -Entity $UpdateSub |
| 40 | + } |
27 | 41 | } |
28 | | - $Resource = "$($UpdateSub.Resource)" |
29 | | - $EventType = "$($UpdateSub.EventType)" |
30 | | - |
31 | | - Write-LogMessage -Headers'CIPP' -API 'Renew_Graph_Subscriptions' -message "Recreating: $($UpdateSub.SubscriptionID) as renewal failed." -Sev "Info" -tenant $TenantFilter |
32 | | - $CreateResult = New-CIPPGraphSubscription -TenantFilter $TenantFilter -TypeofSubscription $TypeofSubscription -BaseURL $BaseURL -Resource $Resource -EventType $EventType -Headers 'GraphSubscriptionRenewal' -Recreate |
33 | | - |
34 | | - if ($CreateResult -match 'Created Webhook subscription for') { |
35 | | - Remove-AzDataTableEntity -Force @WebhookTable -Entity $UpdateSub |
36 | | - } |
37 | | - |
| 42 | + } catch { |
| 43 | + Write-LogMessage -API 'Renew_Graph_Subscriptions' -message "Failed to renew Webhook Subscription: $($UpdateSub.SubscriptionID). Linenumber: $($_.InvocationInfo.ScriptLineNumber) Error: $($_.Exception.message)" -Sev 'Error' -tenant $TenantFilter |
38 | 44 | } |
39 | | - |
40 | | - |
41 | | - } catch { |
42 | | - 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 |
43 | 45 | } |
44 | 46 | } |
45 | 47 | } |
0 commit comments