powershell - 用于构建服务器的 Azure 凭据

标签 powershell azure

我在使用 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/

相关文章:

powershell - Exchange Remote Powershell 出现零星 'Broken' 状态

.net - 如何以编程方式获取 WebJob 的日志?

azure - 具有 blob 触发器的可扩展 Azure 函数

azure - 使用 powershell 使用访问 token 连接到 azure ad

class - PowerShell中的构造函数链接-调用同一类中的其他构造函数

powershell - 仅替换文件名中第一次出现的字符

visual-studio - Visual Studio 2010:在PowerShell中动态构建工具窗口

asp.net - 尝试将图像上传到 Azure 但收到 404

Azure Devops 运行结果步骤和运行摘要详细信息附件 API

c# - 在 Windows Azure 和本地服务器上运行 ASP.NET 应用程序