简体   繁体   English

获取 Azure DevOps 服务连接服务主体 id 与 powershell

[英]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.

相关问题 在 Azure DevOps Powershell 管道任务中获取自己的服务主体名称 - Get own Service Principal Name in an Azure DevOps Powershell pipeline task 如何获取 Azure DevOps 服务主体 ID + 名称 - How to get the Azure DevOps Service Principal ID + Name 密码管理:Azure Devops - 服务连接,服务主体手册 - Password management : Azure Devops - Service connection, service principal manual 如何使用应用程序 ID(服务主体)的令牌向 Azure Devops 进行身份验证? - How to authenticate to Azure Devops by using token of an Application ID(service principal)? Azure bicep 获取服务主体的应用程序 ID - Azure bicep get application id of service principal 如何获取Azure devops服务连接ID - How to fetch Azure devops service connection id 使用 PowerShell 获取 Azure 自动化帐户连接的服务主体 - Getting the service principal for an Azure Automation Account connection using PowerShell Azure Devops - 服务主体手动(或)服务主体自动用于生产部署? - Azure Devops - Service Principal manual (or) Service Principal automatic for production deployments? 为什么在Azure DevOps中需要在Azure Resource Manager服务连接中指定Service Principal? - Why do I need to specify a Service Principal in Azure Resource Manager service connection in Azure DevOps? Azure Devops - 服务主体手动设置? - Azure Devops - Service Principal manual setup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM