Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rcabr/6253ba137f282372f31f0c2cae9ad014 to your computer and use it in GitHub Desktop.
Save rcabr/6253ba137f282372f31f0c2cae9ad014 to your computer and use it in GitHub Desktop.
Azure PowerShell to assign the specified initiative (policy set) to the current subscription
#
# Assigns the specified initiative to the currently selected subscription
#
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true, HelpMessage="The name of the initiative to assign. Example: 'Data Classification'")]
[string]$InitiativeName
)
Get-AzureRmContext # print out current context to inform operator
$subscription = ((Get-AzureRmContext).Subscription)
# find the initiative
$policySet = Get-AzureRmPolicySetDefinition `
| Select-Object -Property ResourceId -ExpandProperty Properties `
| Where-Object -Property displayName -Value $InitiativeName -EQ `
| Select-Object -Property @{Name="Id"; Expression={$_.ResourceId} } `
| Get-AzureRmPolicySetDefinition
# construct request
$assignmentRequests = $subscription | Select-Object @(`
@{Name="Name"; Expression={$policySet.Name + $_.Name } }, `
@{Name="Scope"; Expression={"/subscriptions/" + $_.SubscriptionId} }, `
@{Name="PolicySetDefinition"; Expression={$policySet} }, `
@{Name="PolicyParameterObject"; Expression={@{}} }, `
@{Name="DisplayName"; Expression={$policySet.Name + " " + $_.Name} }, `
@{Name="Sku"; Expression={@{Name = 'A1'; Tier = 'Standard'}} } `
)
# assign
$assignmentRequests | New-AzureRmPolicyAssignment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment