azure - 在 Azure DevOps Powershell 管道任务中获取自己的服务主体名称

标签 azure powershell azure-devops pipeline

当在 Azure DevOps 发布管道中使用 system.debug=true 运行 Azure Powershell 任务时,您将获得类似于以下内容的输出:

# anonymized
...
2019-09-05T12:19:41.8983585Z ##[debug]INPUT_CONNECTEDSERVICENAMEARM: '7dd40b2a-1c37-4c0a-803e-9b0044a8b54e'
2019-09-05T12:19:41.9156487Z ##[debug]ENDPOINT_URL_7dd40b2a-1c37-4c0a-803e-9b0044a8b54e: 'https://management.azure.com/'
2019-09-05T12:19:41.9188051Z ##[debug]ENDPOINT_AUTH_7dd40b2a-1c37-4c0a-803e-9b0044a8b54e: '********'
2019-09-05T12:19:41.9221892Z ##[debug]ENDPOINT_DATA_7dd40b2a-1c37-4c0a-803e-9b0044a8b54e: '{"subscriptionId":"b855f753-d5b3-48f4-b7cd-5beb58fb5508","subscriptionName":"Entenhausen","environment":"AzureCloud","creationMode":"Automatic","azureSpnRoleAssignmentId":"5ddcc3fe-f93c-4771-8041-50b49f76b828","azureSpnPermissions":"[{\"roleAssignmentId\":\"5ddcc3fe-f93c-4771-8041-50b49f76b828\",\"resourceProvider\":\"Microsoft.RoleAssignment\",\"provisioned\":true}]","spnObjectId":"76055cb6-3b75-4191-9309-306b32dad443","appObjectId":"e4b90b9d-7a73-42a3-ae6e-4daec910def4","environmentUrl":"https://management.azure.com/","galleryUrl":"https://gallery.azure.com/","serviceManagementUrl":"https://management.core.windows.net/","resourceManagerUrl":"https://management.azure.com/","activeDirectoryAuthority":"https://login.microsoftonline.com/","environmentAuthorityUrl":"https://login.windows.net/","graphUrl":"https://graph.windows.net/","managementPortalUrl":"https://manage.windowsazure.com/","armManagementPortalUrl":"https://portal.azure.com/","activeDirectoryServiceEndpointResourceId":"https://management.core.windows.net/","sqlDatabaseDnsSuffix":".database.windows.net","AzureKeyVaultDnsSuffix":"vault.azure.net","AzureKeyVaultServiceEndpointResourceId":"https://vault.azure.net","StorageEndpointSuffix":"core.windows.net","EnableAdfsAuthentication":"false"}'
2019-09-05T12:19:41.9284444Z ##[debug]AuthScheme ServicePrincipal
...

我需要将 Azure DevOps 连接的 SPN 添加到资源。更改订阅或管道时,SPN 也会更改,我不想对该值进行硬编码。 由于该值打印在 system.debug=true 输出中,我想知道如何在管道任务中访问我自己的 SPN。是否可以使用 Powershell 以某种方式读出 spnObjectId":"76055cb6-3b75-4191-9309-306b32dad443"

最佳答案

可以使用 Get-AzureRmContext 访问有关服务主体的信息,但信息有限,并且某些信息在日志中被混淆,因此您需要再次调用 Get-AzureRmServicePrincipal 来访问 ObjectId

$Context = Get-AzureRmContext
$AzureDevOpsServicePrincipal = Get-AzureRmADServicePrincipal -ApplicationId $Context.Account.Id
$ObjectId = $AzureDevOpsServicePrincipal.Id

$Context.Account.Id 中公开的 Id 是服务主体 ApplicationId

关于azure - 在 Azure DevOps Powershell 管道任务中获取自己的服务主体名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57805778/

相关文章:

AZURE SQL 登录失败

windows - powershell中#的含义

python - 在不打开单独的 shell 的情况下运行 python 程序

c# - 解决方案的本地构建适用于 .NET 7,但不适用于管道

git - Azure DevOps : Set Git branch permission for all repos in all projects

java - Azure Java SDK - 类去哪儿了

Azure AD 为应用程序创建用户组

c# - Azure 服务证书限制

powershell - 与 Chocolatey 一起使用什么安装程序类型来安装 powershell 模块?

azure - 如何检查代理上运行管道的能力?