我在使用 Azure 时遇到身份验证问题。我有一个运行 powershell 脚本的连续构建服务器,我收到如下消息:
Your Azure credentials have not been set up or have expired, please run Login-AzureRMAccount to set up your Azure credentials.
我不喜欢在构建服务器上使用我的帐户登录。我想我可以创建另一个帐户只是为了构建,但它也会过期。有没有更好的方法来处理这个问题?
最佳答案
您不应在构建/部署脚本中使用 Login-AzureRmAccount,因为这是交互式登录,而应该使用 Add-AzureRmAccount
相反。
Add-AzureRmAccount 需要您create a service principal (应用程序)在 Azure AD 中并使用其客户端 ID 和 key / key 进行身份验证。
以下是您可以使用的代码片段:
$clientID = "<the client id of your AD Application>"
$key = "<the key of your AD Application>"
$SecurePassword = $key | ConvertTo-SecureString -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $clientID, $SecurePassword
Add-AzureRmAccount -Credential $cred -Tenant "xxxx-xxxx-xxxx-xxxx" -ServicePrincipal
找出您的租户 ID(单一订阅)
$tenant = (Get-AzureRmSubscription).TenantId
查找租户 ID(多个订阅)
$tenant = (Get-AzureRmSubscription -SubscriptionName "My Subscription").TenantId
关于powershell - 用于构建服务器的 Azure 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42911206/