Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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