用于构建服务器的Azure凭据

时间:2017-03-20 18:23:26

标签: powershell azure

我正在使用Azure进行身份验证问题。我有一个运行powershell脚本的连续构建服务器,我收到的信息如下:

  

您的Azure凭据尚未设置或已过期,请运行Login-AzureRMAccount以设置Azure凭据。

我不喜欢在构建服务器上使用我的帐户登录。我想我可以创建另一个仅用于构建的帐户,但这也将过期。有没有更好的方法来处理这个?

1 个答案:

答案 0 :(得分:3)

您不应在构建/部署脚本中使用Login-AzureRmAccount,因为这是一个交互式登录,而是Add-AzureRmAccount

Add-AzureRmAccount在Azure AD中需要create a service principal(应用程序),并使用其客户端ID和密钥/密钥进行身份验证。

以下是您可以使用的代码段:

$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