[英]Get Azure DevOps service connection service principal id with powershell
I am working on automating Azure Active Directory App Registrations and Azure Devops Service Connections, and have hit a wall.我正在致力于自动化 Azure Active Directory 应用程序注册和 Azure Devops 服务连接,但遇到了困难。
I want to query Azure DevOps service connections (service endpoints) by Service Principal ID (or at least get the id).我想通过服务主体 ID(或至少获取 ID)查询 Azure DevOps 服务连接(服务端点)。 This is possible when using Azure CLI:这在使用 Azure CLI 时是可能的:
az devops service-endpoint list --query "[?authorization.parameters.serviceprincipalid=='xxx']"
But since I am running this in Azure automation account as a powershell runbook, the Azure CLI is not supported.但由于我在 Azure 自动化帐户中将其作为 powershell 运行手册运行,因此不支持 Azure CLI。
Then I tried the Azure DevOps REST API, and called it from powershell, but the response does not contain the service principal ID, but just this:然后我尝试了 Azure DevOps REST API,并从 powershell 调用它,但响应不包含服务主体 ID,而只是这样:
authorization : @{parameters=; scheme=ServicePrincipal}
Does anyone have an idea on how to solve this?有没有人知道如何解决这个问题?
UPDATE更新
I am calling the rest API like this:我这样呼叫 rest API:
$uriAccount = $UriOrg + "_apis/serviceendpoint/endpoints?endpointNames={name}&api-version=6.1-preview.4"
$result = Invoke-RestMethod -Uri $uriAccount -Method get -Headers $AzureDevOpsAuthenicationHeader
And $result.value gives me this: $result.value 给了我这个:
authorization : @{parameters=; scheme=ServicePrincipal}
You can try the REST API Endpoints - Get Service Endpoints By Names .您可以尝试 REST API 端点 - 按名称获取服务端点。
GET https://dev.azure.com/{organization}/{project}/_apis/serviceendpoint/endpoints?endpointNames={endpointNames}&api-version=6.0-preview.4
In this REST API, you can find the id and details by the name of a service connection.在这个 REST API 中,您可以通过服务连接的名称找到 id 和详细信息。
Here is an example to use the REST API in PowerShell:下面是在 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
Update:更新:
The cause for this question is that you output result
in the wrong way.出现这个问题的原因是你output result
方法错误。
For JSON response bodies, there is no intuitive way to get results without specifying the final layer.对于 JSON 响应主体,如果不指定最终层,则没有直观的方法来获取结果。 Here is my modified code, notice how I print result
:这是我修改后的代码,请注意我如何打印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.