首页 > 解决方案 > 如何使用 pwsh 脚本生成的新密码重置 azure 管道服务连接的密码

问题描述

我在 Azure 管道中创建了一堆服务连接,以通过 SSH 隧道访问 Unix/Linux VM,是否可以使用脚本、bash 或 pwsh 更改服务连接的密码?新密码使用脚本生成并存储在 azure key vault 中。

标签: azureazure-pipelines

解决方案


是否可以使用脚本、bash 或 pwsh 更改服务连接的密码?新密码使用脚本生成并存储在 azure key vault 中。

是的。您可以使用 Rest API 来实现:<a href="https://docs.microsoft.com/en-us/rest/api/azure/devops/serviceendpoint/endpoints/update-service-endpoint?view=azure- devops-rest-6.0" rel="nofollow noreferrer">端点 - 更新服务端点

这是 PWSH 示例:

$token = "PAT"

$url=" https://dev.azure.com/{Oranization}/{Project}/_apis/serviceendpoint/endpoints/{serviceconnectionid}?api-version=6.0-preview.4"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

Write-Host "Currentconfiguration = $($response | ConvertTo-Json -Depth 100)"

$response.authorization.parameters.password= "$(keyvaultname)"


$json = @($response) | ConvertTo-Json -Depth 99

$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Basic $token"}

若要获取 Azue Key Vault 值,可以使用 Azure CLI:az keyvault secret show

az keyvault secret show [--id]
                        [--name]
                        [--query-examples]
                        [--subscription]
                        [--vault-name]
                        [--version]

然后您可以将获得的秘密值注入 pwsh 变量并将其应用于 Rest API 示例。


推荐阅读