Script Sample - Creating Multiple Resource Groups with RBAC Role Assignment
@20aman Nov 15, 2018You can automate the creation of the Resource Groups and also assign RBAC roles to these Resource Groups. This script sample shows you how you can accomplish this. It serves as a starting point for the automation of the resource group creation. You can modify the script as per your requirements.
How the Script works
The script takes the names of the Resource groups to be created as an array. You can modify the script to take this input from a CSV file along with other parameters like the location of the Resource Group and the related role assignments.
The script uses the below command to create a Resource Group.
New-AzureRmResourceGroup -Name $rg -Location eastus
Note that the location is hard-coded in the above command. This can also be parameterized.
It then uses the below command to make the role assignments on this Resource Group.
New-AzureRmRoleAssignment -ObjectId $ADGroup01.Id -RoleDefinitionName "Contributor" -ResourceGroupName $rg
Note that you can use the same command to make the assignments for a group, user or an app registration. You just need to provide the appropriate Id for the ObjectId parameter.
Location of the Script
You can find this script in GitHub at this location: Create-ResourceGroupsWithRBACAssignments.ps1