Created
June 11, 2018 15:14
-
-
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
This file contains 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
# | |
# 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