0

我正在編寫一個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://github.com/Azure/azure-powershell/blob/f803b991daa7eeeea1217238ab071c8d83de34be/src/ResourceManager/Resources/Commands.Resources/ActiveDirectory/NewAzureADApplicationCommand.cs

https://github.com/Azure/azure-powershell/blob/956d0ca795acfce67d8f142bf059ab2b8ab2c67b/src/ResourceManager/Resources/Commands.Resources/Models.ActiveDirectory/ActiveDirectoryClient.cs

https://www.symbolsource.org/Public/Metadata/NuGet/Project/Microsoft.Azure.Graph.RBAC/1.6.0-preview/Release/.NETFramework,Version%3Dv4.0/Microsoft.Azure.Graph.RBAC/Microsoft.Azure.Graph.RBAC/Generated/GraphRbacManagementClient.cs?ImageName=Microsoft.Azure.Graph.RBAC

我只能找一個人誰在這個鏈接遇到這個相同的錯誤在這裏: https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/

但是,那裏的解決方案對我來說沒有意義,因爲我沒有使用管理證書進行身份驗證,並且我沒有在manage.windowsazure.com站點上列出任何管理證書。

+0

Visual Studio中的哪個版本?我複製你的代碼並使用'Login-AzureRmAccount'運行,它對我來說非常合適。我正在使用Visual Studio 2015和Azure PowerShell 1.2.1 –

+0

我正在使用與Azure PowerShell相同的Visual Studio 2015 - 2016年2月(1.2.1)。 –

+0

你使用什麼樣的登錄策略? Visual Studio自動生成的腳本使用'Microsoft.Azure.Common.Authentication.AzureSession',該腳本使用Azure PowerShell 0.9樣式命令,該命令不適用於Azure PowerShell 1.2.1。我想弄清楚如何使用Visual Studio的Azure會話進行登錄。如果你分享你的代碼,這將是非常有幫助的。 –

回答

2

當AzureRMAD * cmdlet使用基於令牌的身份驗證時,這是一個問題(即錯誤)。當您從VS運行腳本時,VS會使用您從VS登錄中獲得的令牌來避免提示進行身份驗證。要解決它,你必須使用憑據在VS之外運行它。

有一個內部工作項跟蹤這一點,但如果你要監控進展情況,你可以在這裏提交一個問題:您正在使用https://github.com/Azure/azure-powershell/issues/

+0

是的,我想到了很多,我採用從PowerShell手動調用部署腳本的方法,現在一切正常。 –

+0

我按照建議添加了該問題https://github.com/Azure/azure-powershell/issues/2273 – antmeehan

相關問題