Wondering if someone knows how to do this as I've been struggling to do so.
I'm attempting to put together a PowerShell script that will hopefully update any service hooks which we have setup in Azure DevOps, so their statuses become enabled. Occasionally ADO will disable them, I think due to inactivity for a long period of time, which is a pain.
So far I have the following but I'm getting an error saying
"The notification subscription "2092cfc4-a95b-4800-976e-67ccf9deb4b1" for service hook subscription "2e0a69e7-c4aa-44ad-89d7-ca1e3809585e" no longer exists."
I understand that but not sure on how to fix it. I'm sure it exists because if I take $uri2, populate the subscriptionId with one of the $item variables in the loop and then put into a browser, I get the json returned like this so it's finding it right?
I've been referencing the various documentation on this page: https://learn.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/replace%20subscription?view=azure-devops-rest-5.1
I thought that I'd first need to get a list of all the web hook subscriptions and then iterate through each one grabbing the Id, using that Id within another request to update the service hook. I suspect half the problem is to do with my understanding on how it's done, if at all possible so thought I'd ask here.
Has anyone accomplished this or perhaps have some example script on how you might go about updating a field? Not sure if I need to pass more data into the body or not.
cls
$User = 'myUser'
$PersonalAccessToken = 'myPatToken'
$base64authinfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User, $PersonalAccessToken)))
$vstsAccount = "myOrgName"
$uri1 = "https://dev.azure.com/$vstsAccount/_apis/hooks/subscriptions?api-version=5.1"
$hooks = Invoke-RestMethod -Method Get -ContentType application/json -Uri $uri1 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)}
foreach ($item in $hooks.value) {
$body = @{
"publisherId" = "tfs"
"eventType" = "$($item.eventType)"
"resourceVersion" = "1.0"
"consumerId" = "webHooks"
"consumerActionId" = "httpRequest"
"status" = "enabled"
}
$bodyJson = $body | ConvertTo-Json
write-host "current status: $($item.status)"
write-host "$($item.id)"
write-host "$($item.eventType)"
$uri2 = "https://dev.azure.com/$vstsAccount/_apis/hooks/subscriptions/$($item.id)?api-version=5.1"
write-host $uri2
Invoke-RestMethod -Method Put -ContentType application/json -Uri $uri2 -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $bodyJson
}