Sur mon abonnement MSDN Azure, connecté après l'exécution de Login-AzureRMAccount, je peux exécuter Get-AzureRmRoleAssignment
sans problème.Exécuter Get-AzureRmRoleAssignment en tant que service principal dans Azure Powershell
J'ai créé une application et un service principal avec un rôle dans Azure avec Powershell (Nouveau-AzureRmADApplication, Nouvelle-AzureRmADServicePrincipal & Nouveau-AzureRmRoleAssignment) et après la connexion avec ces informations d'identification avec ce powershell:
$psCredential = New-Object System.Management.Automation.PSCredential("98349834-8494-4813-9282-4343434", (ConvertTo-SecureString "myPassword" -AsPlainText -Force))
Add-AzureRMAccount -ServicePrincipal -Tenant "123456-d5bb-44f8-a283-34534434" -Credential $psCredential
la suivant peut être exécuté avec succès:
Get-AzureRmADApplication -IdentifierUri "http://SP.5656645-408c-4980-950e-898989"
mais lors de l'exécution
Get-AzureRmRoleAssignment -debug
Je reçois l'exception suivante:
Microsoft.Rest.Azure.CloudException: Accès refusé à la version de l'API spécifiée
On dirait que beaucoup de gens ont cette exception et je l'ai lu beaucoup de solutions comme donnant accès au portail d'azur, (mais rien ne semble fonctionner): également après avoir donné l'application des autorisations suivantes, l'exception est soulevée encore:
- Windows Azure Active Directory 6 autorisations d'application - 6 autorisations délégués (pas en mesure de les sélectionner, uniquement lorsque requis Administrateur = Oui)
- Microsoft Graph - 20 autorisations d'application - (également 20 autorisations délégués ne peuvent pas les sélectionner all)
Que dois-je faire pour laisser le service principal exécuter Get-AzureRmRoleAssignment?
modifier: L'application a le rôle Propriétaire. J'ai utilisé le script ps suivant pour créer l'application, le service principal et le rôle.
param
(
[Parameter(Mandatory=$true, HelpMessage="Enter Azure Subscription name. You need to be Subscription Admin to execute the script")]
[string] $subscriptionName,
[Parameter(Mandatory=$true, HelpMessage="Provide a password for SPN application that you would create")]
[string] $password,
Mandatory=$false, HelpMessage="Provide a SPN role assignment")]
[string] $spnRole = "owner"
)
#Initialize
$ErrorActionPreference = "Stop"
$VerbosePreference = "SilentlyContinue"
$userName = $env:USERNAME
$newguid = [guid]::NewGuid()
$displayName = [String]::Format("SP.{0}.{1}", $resourceGroupName, $newguid)
$homePage = "http://" + $displayName
$identifierUri = $homePage
#Initialize subscription
$isAzureModulePresent = Get-Module -Name AzureRM* -ListAvailable
if ([String]::IsNullOrEmpty($isAzureModulePresent) -eq $true)
{
Write-Output "Script requires AzureRM modules to be present. Obtain AzureRM from https://github.com/Azure/azure-powershell/releases. Please refer https://github.com/Microsoft/vsts-tasks/blob/master/Tasks/DeployAzureResourceGroup/README.md for recommended AzureRM versions." -Verbose
return
}
Import-Module -Name AzureRM.Profile
Write-Output "Provide your credentials to access Azure subscription $subscriptionName" -Verbose
Login-AzureRmAccount -SubscriptionName $subscriptionName
$azureSubscription = Get-AzureRmSubscription -SubscriptionName $subscriptionName
$connectionName = $azureSubscription.SubscriptionName
$tenantId = $azureSubscription.TenantId
$id = $azureSubscription.SubscriptionId
#Create a new AD Application
Write-Output "Creating a new Application in AAD (App URI - $identifierUri)" -Verbose
$azureAdApplication = New-AzureRmADApplication -DisplayName $displayName -HomePage $homePage -IdentifierUris $identifierUri -Password $password -Verbose
$appId = $azureAdApplication.ApplicationId
Write-Output "Azure AAD Application creation completed successfully (Application Id: $appId)" -Verbose
#Create new SPN
Write-Output "Creating a new SPN" -Verbose
$spn = New-AzureRmADServicePrincipal -ApplicationId $appId
$spnName = $spn.ServicePrincipalNames
Write-Output "SPN creation completed successfully (SPN Name: $spnName)" -Verbose
#Assign role to SPN
Write-Output "Waiting for SPN creation to reflect in Directory before Role assignment"
Start-Sleep 20
Write-Output "Assigning role ($spnRole) to SPN App ($appId)" -Verbose
New-AzureRmRoleAssignment -RoleDefinitionName $spnRole -ServicePrincipalName $appId
Write-Output "SPN role assignment completed successfully" -Verbose
#Print the values
Write-Output "`nCopy and Paste below values for Service Connection" -Verbose
Write-Output "***************************************************************************"
Write-Output "Connection Name: $connectionName(SPN)"
Write-Output "Subscription Id: $id"
Write-Output "Subscription Name: $connectionName"
Write-Output "Service Principal Id: $appId"
Write-Output "Service Principal key: <Password that you typed in>"
Write-Output "Tenant Id: $tenantId"
Write-Output "***************************************************************************"
Quelle version d'Azure PowerShell utilisez-vous? Je suis en mesure d'exécuter Get-AzureRmRoleAssignment -debug sans problème – juvchan
Version 5.1.14393.693. La version AzureRM est 3.6 –
Je l'ai essayé sur plusieurs PC de développement sans succès. Êtes-vous capable d'exécuter Get-AzureRmRoleAssignment -debug après vous être connecté en tant que Service Principal? (comme le powershell ci-dessus?) –