簡體   English   中英

獲取 Azure DevOps 服務連接服務主體 id 與 powershell

[英]Get Azure DevOps service connection service principal id with powershell

我正在致力於自動化 Azure Active Directory 應用程序注冊和 Azure Devops 服務連接,但遇到了困難。

我想通過服務主體 ID(或至少獲取 ID)查詢 Azure DevOps 服務連接(服務端點)。 這在使用 Azure CLI 時是可能的:

az devops service-endpoint list --query "[?authorization.parameters.serviceprincipalid=='xxx']"

但由於我在 Azure 自動化帳戶中將其作為 powershell 運行手冊運行,因此不支持 Azure CLI。

然后我嘗試了 Azure DevOps REST API,並從 powershell 調用它,但響應不包含服務主體 ID,而只是這樣:

authorization : @{parameters=; scheme=ServicePrincipal}

有沒有人知道如何解決這個問題?

更新

我這樣呼叫 rest API:

$uriAccount = $UriOrg + "_apis/serviceendpoint/endpoints?endpointNames={name}&api-version=6.1-preview.4"
$result = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader 

$result.value 給了我這個:

authorization : @{parameters=; scheme=ServicePrincipal}

您可以嘗試 REST API 端點 - 按名稱獲取服務端點

GET https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4

在這個 REST API 中,您可以通過服務連接的名稱找到 id 和詳細信息。

下面是在 PowerShell 中使用 REST API 的示例:

$token = "{pat}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4"
$head = @{ Authorization =" Basic $token" }
Invoke-RestMethod -Uri $url -Method GET -Headers $head

更新:

出現這個問題的原因是你output result方法錯誤。

對於 JSON 響應主體,如果不指定最終層,則沒有直觀的方法來獲取結果。 這是我修改后的代碼,請注意我如何打印result

$token = "{pat}"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4"
$head = @{ Authorization =" Basic $token" }
$reslut = Invoke-RestMethod -Uri $url -Method GET -Headers $head
echo $result.value.authorization.parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM