【发布时间】:2016-03-15 06:56:46
【问题描述】:
我正在编写一个 PowerShell 部署脚本,它可以自动创建我的 Azure 资源和随附的 ServicePrincipal。
这是我正在使用的代码,我已经测试并在使用最新的 Azure 1.0.4 SDK 模块直接从 PowerShell 运行时工作:
$ResourceGroupName = "my-resource-group"
$ADAppIdentifierUri = [string]::Concat("https://", $ResourceGroupName, ".azurewebsites.net")
# Generate a password for the AD application
$ServicePrincipalPassword = [Guid]::NewGuid().ToString().Replace("-", "")
# Create the Azure AD Application and service principal, and only assign access to our resource group
$AzureADApplication = New-AzureRmADApplication -DisplayName $ResourceGroupName -HomePage $ADAppIdentifierUri -IdentifierUris $ADAppIdentifierUri -Password $ServicePrincipalPassword
当我在 Visual Studio 中使用我的 ResourceGroup 项目部署脚本运行此代码时,我收到以下错误:
New-AzureRmADApplication:无法将“Microsoft.Azure.TokenCloudCredentials”类型的对象转换为“Microsoft.Azure.Common.Authentication.AccessTokenCredential”类型。
根据堆栈跟踪,异常是在命令 New-AzureRmADApplication 开始时引发的,因此很遗憾,异常是在 Azure SDK 代码内部发生的。
我在以下文件中浏览了 SDK 的源代码,但没有找到任何见解:
我只能在此链接中找到一个遇到同样错误的人: https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/
但是,那里的解决方案对我没有意义,因为我没有使用管理证书进行身份验证,并且在 manage.windowsazure.com 站点上没有列出任何管理证书。
【问题讨论】:
-
您使用的是哪个版本的 Visual Studio?我复制了您的代码并使用
Login-AzureRmAccount运行,它对我来说非常有效。我正在使用 Visual Studio 2015 和 Azure PowerShell 1.2.1 -
我使用的是相同的 Visual Studio 2015 和 Azure PowerShell - 2016 年 2 月 (1.2.1)。
-
你使用什么样的登录策略? Visual Studio 自动生成的脚本使用
Microsoft.Azure.Common.Authentication.AzureSession,并且该脚本使用 Azure PowerShell 0.9 样式的命令,它不适用于 Azure PowerShell 1.2.1。我想弄清楚如何使用 Visual Studio 的 Azure Session 登录。如果您分享您的代码,将会非常有帮助。 -
我正在使用项目模板 Cloud -> Resource Group 附带的样板 Deploy-AzureResourceGroup.ps1 脚本。我正在使用项目文件的实际部署选项运行脚本。我上面链接的代码的sn-p可以放在这个文件里测试一下情况。
-
通过使用 Get-AzureRmContext cmdlet,我能够确定在 Visual Studio 中运行 Azure PowerShell 时,帐户的身份验证是使用 AccessToken 完成的。但是,当我在 PowerShell 中使用 Login-AzureRmAccount 进行身份验证时,它不使用这样的令牌。我猜这是导致潜在问题的原因。
标签: azure azure-powershell azure-resource-manager service-principal