Skip to content

Instantly share code, notes, and snippets.

@DmitriiBobreshev
Created May 22, 2024 14:35
Show Gist options
  • Save DmitriiBobreshev/221c9dc5ac7d8348b7ff50c4b1347990 to your computer and use it in GitHub Desktop.
Save DmitriiBobreshev/221c9dc5ac7d8348b7ff50c4b1347990 to your computer and use it in GitHub Desktop.
[CmdletBinding()]
param(
$subscription_id,
$rg_groupname,
$rg_location,
$aad_group
)
Write-Host $subscription_id
Write-Host $rg_groupname
Write-Host $rg_location
Write-Host $aad_group
az account set --subscription $subscription_id
az account show
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
# Install Az.Resources for Get-AzADGroup & Get-AzRoleAssignment CmdLets https://learn.microsoft.com/en-us/powershell/module/az.resources/get-azroleassignment?view=azps-11.6.0
Install-Module -Name Az.Resources -Force -AllowClobber
# Install Az.Acounts for Connect-AzAccount CmdLet https://learn.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-11.6.0#description
Install-Module -Name Az.Accounts -Force -AllowClobber
# Call Connect-AzAccount to authenticate and call Get-AzADGroup & Get-AzRoleAssignment
Connect-AzAccount
# check if resource group exists
$rgExists = (az group exists --name $rg_groupname)
# create if not exists
if ($rgExists -eq 'false') {
az group create --name $rg_groupname --location $rg_location
Start-Sleep -s 30
}
# set databricks resource group scope
$workspaceScope="/subscriptions/$subscription_id/resourceGroups/$rg_groupname"
# grant rg contributor permissions to aad group
$group = Get-AzADGroup -DisplayName $aad_group
$groupId = $group.Id
$roleDefinitionName = "Contributor"
# Check if the role assignment already exists
$existingAssignment = Get-AzRoleAssignment -ObjectId $groupId -Scope $workspaceScope -RoleDefinitionName $roleDefinitionName
if ($existingAssignment) {
Write-Host "The role assignment already exists."
}
else {
# Create the role assignment
New-AzRoleAssignment -ObjectId $groupId -RoleDefinitionName $roleDefinitionName -Scope $workspaceScope
Write-Host "The role assignment has been created."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment