实际上,我不建议您将 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