-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy-PolicyExemptions.ps1
51 lines (45 loc) · 2.17 KB
/
deploy-PolicyExemptions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
. .\utilities\functions.ps1
$globals = Get-Content -Path .\globals.json | ConvertFrom-Json
$requiredValues = @("defaultLocation")
$requiredValues | ForEach-Object {
if ($globals[$_] -eq "") {
Write-Error "$_ contains no value in globals.json"
exit
}
}
foreach ($folder in Get-ChildItem -Path .\policies\assignments -Directory -Recurse) {
foreach ($file in Get-ChildItem $folder -File | Where-Object Name -Match "^EX_") {
Write-Output "Deploying $($file.BaseName)"
$deploymentName = "$((Split-Path -Path $file.FullName).Split('/')[-1])-$($file.BaseName)"
if ($deploymentName.Length -ge 64) {
Write-Output "Trimming the deployment name - $deploymentName"
$deploymentName = $deploymentName -replace ".{25}$"
}
if ($folder.Name -match "^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$") {
Select-AzSubscription -Subscription $folder.Name
New-AzSubscriptionDeployment -Name $deploymentName `
-Location $globals.defaultLocation `
-TemplateFile .\policies\exemptions\exemption_template_subscription.json `
-TemplateParameterFile $file.FullName `
-Verbose
}
elseif ($folder.Name -match "^rg_") {
$resourceGroupName = ($folder.Name -split "rg_")[1]
$subscriptionName = ($folder.Parent).Split("/")[-1]
Select-AzSubscription -Subscription $subscriptionName
New-AzResourceGroupDeployment -Name $deploymentName `
-ResourceGroupName $resourceGroupName `
-TemplateFile .\policies\exemptions\exemption_template_resourcegroup.json `
-TemplateParameterFile $file.FullName `
-Verbose
}
else {
New-AzManagementGroupDeployment -ManagementGroupId $folder.BaseName `
-Name $deploymentName `
-TemplateFile .\policies\exemptions\exemption_template_managementgroup.json `
-TemplateParameterFile $file.FullName `
-Location $globals.defaultLocation `
-Verbose
}
}
}