如何在Azure DevOps发布管道中连接az账户

3

enter image description here

在发布流水线中,我正在尝试使用Connect-Azaccount连接到Azure AD,以便可以运行Get-AzADgroup来检索一些Az AD组名称及其GUID,并将其输出到变量中。
我创建了Azure Powershell任务,并使用以下内联脚本:
(Get-AzADGroup -DisplayName "group-name").origin

enter image description here

enter image description here

enter image description here


如果我的回复有帮助,请将其标记为答案(在我的回复左侧有一个标记选项),谢谢。 - Joy Wang
嗨Joy,我还没有尝试过,因为我没有创建AD应用程序和服务主体的权限。一旦我获得权限,我会立即更新您。 - wonderfulworldwithcharity
如果您创建一个devops项目,可以使用现有的AD应用程序,它将自动在租户的应用程序注册表中创建一个名为“organizationname-projectname-513f22f1-befd-xxxxxxcfe90f”的AD应用程序。但是您还需要具有“授予xxx的管理员同意”权限。也许您可以请求管理员为您授予API权限。:-) - Joy Wang
我正在使用任务版本4.*(预览版) - wonderfulworldwithcharity
嗨Joy,你知道这里的问题是什么吗? - wonderfulworldwithcharity
显示剩余7条评论
1个回答

4

看起来你需要使用非交互式登录,请按以下步骤操作。

  1. Create an Azure Active Directory application and create a secret for the app, save the secret and get values for signing in.

  2. In your AD App -> API permissions -> Add a permission -> select Azure Active Directory Graph -> Application permissions -> Directory.Read.All -> click Add permissions -> click Grant admin consent for xxx, refer to the screenshot.

    enter image description here

    enter image description here

  3. Try the script as below, use the values which you get in step 1, it works fine on my side.

    Note: You need to use the Task version with 4.*(preview) when you use Az powershell module.

    $azureAplicationId ="<your ad app application id>"
    $azureTenantId= "<your tenant id>"
    $azurePassword = ConvertTo-SecureString "<the secret of your ad app>" -AsPlainText -Force
    $psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
    Connect-AzAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal 
    #I just test to get all groups, you could do other operations
    Get-AzADGroup 
    

    enter image description here


Joy Wang, 我按照您提供的方法尝试登录,但是没有成功。 请查看以下错误信息: Connect-AzAccount:ClientSecretCredential身份验证失败:配置问题导致身份验证失败-请检查服务器返回的错误消息以获取详细信息。您可以在应用程序注册门户中修改配置。有关详细信息,请参见https://aka.ms/msal-net-invalid-client。原始异常:AADSTS7000215:提供了无效的客户端密钥。 - microset

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接