【发布时间】:2020-06-25 09:23:54
【问题描述】:
想知道是否有人知道如何做到这一点,因为我一直在努力这样做。
我正在尝试编写一个 PowerShell 脚本,该脚本有望更新我们在 Azure DevOps 中设置的任何服务挂钩,从而使它们的状态变为启用状态。偶尔ADO会禁用它们,我认为是由于长时间不活动,这是一种痛苦。
到目前为止,我有以下内容,但我收到一条错误消息
"通知订阅 “2092cfc4-a95b-4800-976e-67ccf9deb4b1”用于服务挂钩订阅 “2e0a69e7-c4aa-44ad-89d7-ca1e3809585e”不再存在。”
我明白这一点,但不确定如何解决。 我确定它存在,因为如果我使用 $uri2,用循环中的 $item 变量之一填充 subscriptionId,然后放入浏览器中,我会得到这样返回的 json,所以它找到它了吗?
我一直在参考此页面上的各种文档: https://docs.microsoft.com/en-us/rest/api/azure/devops/hooks/subscriptions/replace%20subscription?view=azure-devops-rest-5.1
我认为我首先需要获取所有 Web 挂钩订阅的列表,然后遍历每个获取 Id 的订阅,在另一个请求中使用该 Id 来更新服务挂钩。我怀疑问题的一半与我对如何完成的理解有关,如果可能的话,我想我会在这里问。
有没有人做到这一点,或者有一些关于如何更新字段的示例脚本?不确定我是否需要将更多数据传递到正文中。
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
}
【问题讨论】:
标签: powershell api azure-devops webhooks azure-devops-rest-api