Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Created February 17, 2016 23:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjwaight/48c4307668c4d5903efa to your computer and use it in GitHub Desktop.
Save sjwaight/48c4307668c4d5903efa to your computer and use it in GitHub Desktop.
param(
[Parameter(Mandatory=$true)]
[string] $SecurityGroupName,
[Parameter(Mandatory=$true)]
[string] $RequiredAzureRoleName,
[Parameter(Mandatory=$true)]
[string] $ResourceGroupName
)
# Assumes that the context has already been set by:
# - Login-AzureRmAccount
# - Connect-MsolService
# Find the Security Group in Azure AD
$targetGroup = Get-MsolGroup -GroupType Security | Where-Object { $_.DisplayName -like "*$($SecurityGroupName)*" }
if($targetGroup -ne $null)
{
# Locate the Resource Group we want to use as our scope
$rg = Get-AzureRmResourceGroup -Name $ResourceGroupName
if($rg -ne $null)
{
# Get Azure RBAC Role to assign to this scope
$roleDef = Get-AzureRmRoleDefinition -Name $RequiredAzureRoleName
# Assign the Group the Role for the Resource Group Scope specified.
New-AzureRmRoleAssignment -ObjectId $targetGroup.ObjectId -RoleDefinitionId $roleDef.Id -Scope $rg.ResourceId
}
else
{
Write-Host "Couldn't find the specified resource group in the current subscription."
}
}
else
{
Write-Host "Couldn't find the required group in Azure Active Directory.".
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment