12

尝试az cli使用服务主体执行登录并抛出错误说明No subscriptions found for <Service_Principal_AppId>. If this is expected, use '--allow-no-subscriptions'。此代码以前运行良好,但现在似乎不再运行。正在使用的命令行如下:

$sp_appid = (Get-AzureRmADServicePrincipal -DisplayName $spDisplayName).ApplicationId.Guid
$sp_secret = (Get-AzureKeyVaultSecret -VaultName $kvName -Name $appKeySecretName).SecretValueText
az login --service-principal --username $sp_appid --password $sp_secret --tenant $tenant_Id

我验证了服务主体Contributor在订阅级别分配了角色。

4

5 回答 5

4

After creating a service principal in the Azure Active Directory you need to give this new user some roles within a subscription:

  • go to your subscription
  • go to Access Control (IAM)
  • Add a roles assignment (for instance make your service principal contributor)

Then az login should work.

于 2022-01-08T17:33:27.920 回答
3

实际上,我不建议您将 Azure Powershell 和 CLI 混合在一起。如果您坚持这样做,我已经尝试过您的脚本,我无法重现您的问题,它工作正常。根据错误,您可以尝试传递 a --subscription,它也可以。

$sp_appid = (Get-AzADServicePrincipal -DisplayName joywebapp2).ApplicationId.Guid
$sp_secret = (Get-AzKeyVaultSecret -VaultName joykeyvault1 -Name joywebapp2).SecretValueText
$tenant_Id = "xxxxxxxxxxxx"
$subscription_Id = "xxxxxxxxxxx"
az login --service-principal --username $sp_appid --password $sp_secret --tenant $tenant_Id --subscription $subscription_Id

在此处输入图像描述

注意:由于AzureRMpowershell 模块已被弃用,我使用新的Azpowershell 模块,如果要升级到 Az,请参阅此链接。(这可能不是问题的原因,但我建议您升级它。)

更新

我们必须仅将 AZ CLI 用于我们试图获取的属性……没有 PowerShell 等效项。

实际上,您可以通过 powershell 使用服务主体登录,这strong password是秘密,更多详细信息请参阅这篇文章

$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Add-AzureRmAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal
于 2019-04-02T01:30:13.107 回答
1

最初的问题似乎是暂时的平台问题。昨天回到相同的代码,它没有问题。

于 2019-04-04T11:40:43.333 回答
0

对我来说,运行缓存清除工作:

az cache purge

此外,如果它仍然不起作用,请尝试使用以下方法打印详细信息:

az login --verbose

于 2021-12-08T11:55:15.417 回答
0

我遇到了同样的问题,突然没有订阅显示我的服务主体(在我最初同时安装的 2 个不同的构建服务器上)。

更新 Azure CLI 似乎可以解决问题。

于 2020-06-05T18:09:14.197 回答