I had this question from a customer recently, and when I searched the net I wouldn’t find any specific examples.
This example assigns a user as a Contributor to the subscription.
When you assign roles to resources, all you need is the URL for the resource and provide it to this cmdlet.
Here you go:
Login-AzureRMAccount $userEmail = "[email protected]" $SubscriptionName = "Test Subscription" Get-AzureRmSubscription -Subscriptionname $SubscriptionName New-AzureRmRoleAssignment -SignInName $userEmail -Scope "/subscriptions/$($sub.SubscriptionId)" -RoleDefinitionName Contributor
That’s all for today!
You have forgot to assign the 4th line in $sub variable 🙂
Is there a way to update all subscriptions with a role?
To update for all subscriptions, you can use the below code :
Login-AzureRMAccount
$SubscriptionName = Get-Content “C:\temp\subscriptions.txt”
foreach ($Subscription in $SubscriptionName)
{
$sub = Get-AzureRmSubscription -Subscriptionname $Subscription
}
$groupID = (Get-AzureRmADGroup -SearchString “SG-Subscription-Contributor”).Id
New-AzureRmRoleAssignment -ObjectId $groupID -Scope “/subscriptions/$($sub.SubscriptionId)” -RoleDefinitionName “Contributor”
Correction –
Login-AzureRMAccount
#import Subscription Names from the text file
$SubscriptionName = Get-Content “C:\temp\subscriptions.txt”
foreach ($Subscription in $SubscriptionName) {
$sub = Get-AzureRmSubscription -Subscriptionname $Subscription
$groupID = (Get-AzureRmADGroup -SearchString “SG-Names-Contributors”).Id
New-AzureRmRoleAssignment -ObjectId $groupID -Scope “/subscriptions/$($sub.SubscriptionId)” -RoleDefinitionName “Contributor”
}