Last active
July 9, 2020 20:06
-
-
Save juanesech/76a36933d83ac71f395d8b46a0c253ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Set IAM role assigments to specific resources. | |
.DESCRIPTION | |
The scripts gets the identity object id from the resource group so you need to ne added it before | |
.EXAMPLE | |
.\roleAssigment.ps1 -SubscriptionName CTSBP-PROD-SUB -ResourceGroupName UWUPIERSG01 -IdentityList IT-Team,CT-USER -ResourceList WEBAPPNAME01,STORAGEACC01 -RoleName Contributor | |
.\roleAssigment.ps1 -SubscriptionName CTSBP-TEST-SUB -ResourceGroupName UWUPIERSG01 -IdentityList CT-USER -ResourceList WEBAPPNAME01 -RoleName Reader | |
#> | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$SubscriptionName, | |
[string]$ResourceGroupName, | |
[string[]]$IdentityList, | |
[string[]]$ResourceList, | |
[string]$RoleName | |
) | |
function Set-RoleAssigment { | |
param ( | |
$resources, | |
$identity, | |
$role | |
) | |
foreach ($resource in $resources) { | |
$resourceId = (Get-AzResource -ResourceGroupName $ResourceGroupName -ResourceName $Resource).ResourceId | |
New-AzRoleAssignment -ObjectId $identity -RoleDefinitionName $role -Scope $resourceId | Out-Null | |
Write-Host "Role $role assigned to $resource" | |
} | |
} | |
Set-AzContext -SubscriptionName $SubscriptionName | Out-Null | |
foreach ($identity in $IdentityList) { | |
Write-Host "Setting role assigment for $identity" | |
$identityId = (Get-AzRoleAssignment -ResourceGroupName $ResourceGroupName | Where-Object {$_.DisplayName -match $identity}).ObjectId | Select-Object -First 1 | |
Set-RoleAssigment -resources $ResourceList -identity $identityId -role $RoleName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment