我正在編寫一個PowerShell部署腳本,它可以自動創建我的Azure資源和附帶的ServicePrincipal。無法在調用New-AzureRmADApplication時將TokenCloudCredentials強制轉換爲AccessTokenCredential
這裏是我使用的代碼,我已經測試和工程時,直接從PowerShell將使用最新的Azure的1.0.4 SDK模塊運行:
$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中,我得到以下錯誤:
New-AzureRmADApplication : Unable to cast object of type 'Microsoft.Azure.TokenCloudCredentials' to type 'Microsoft.Azure.Common.Authentication.AccessTokenCredential'.
根據堆棧跟蹤異常在命令新建-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 –
我正在使用與Azure PowerShell相同的Visual Studio 2015 - 2016年2月(1.2.1)。 –
你使用什麼樣的登錄策略? Visual Studio自動生成的腳本使用'Microsoft.Azure.Common.Authentication.AzureSession',該腳本使用Azure PowerShell 0.9樣式命令,該命令不適用於Azure PowerShell 1.2.1。我想弄清楚如何使用Visual Studio的Azure會話進行登錄。如果你分享你的代碼,這將是非常有幫助的。 –