Skip to content

Instantly share code, notes, and snippets.

@averkinderen
Last active August 10, 2017 18:30
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 averkinderen/df7bb438bd9908af73c0deb25879d654 to your computer and use it in GitHub Desktop.
Save averkinderen/df7bb438bd9908af73c0deb25879d654 to your computer and use it in GitHub Desktop.
Azure Resource Policy deployment
$policyName = Read-Host "Specify the name of the policy";
$policyDescription = Read-Host "Specify the description of the policy"
$policyFile = Read-Host "Path to json policy file";
#Login to the Azure Resource Management Account
Login-AzureRmAccount
#region Get Azure Subscriptions
$subscriptions = Get-AzureRmSubscription
$menu = @{}
for ($i = 1;$i -le $subscriptions.count; $i++)
{
Write-Host -Object "$i. $($subscriptions[$i-1].Name)"
$menu.Add($i,($subscriptions[$i-1].Id))
}
[int]$ans = Read-Host -Prompt 'Enter selection'
$subscriptionID = $menu.Item($ans)
$subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionID
Set-AzureRmContext -SubscriptionName $subscription.Name
#endregion
$subId = (Get-AzureRmContext).Subscription.Id
$subName = (Get-AzureRmContext).Subscription.Name
Write-host "Policy is applied to the subscription: $subName" -ForegroundColor Green
$policy = New-AzureRmPolicyDefinition -Name $policyName -Description $policyDescription -Policy $policyFile;
Write-host "Sleeping for 10 seconds" -ForegroundColor Yellow
Start-Sleep -s 10
#Assign the Azure Policy
New-AzureRmPolicyAssignment -Name $policyName -PolicyDefinition $policy -Scope "/subscriptions/$subid"
Write-host "Policy is assigned to the subscription: $subName" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment