-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeployAzureArcServices.ps1
777 lines (667 loc) · 41.1 KB
/
DeployAzureArcServices.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
# Deployment Variables to choose what to deploy
$deployLAW = $true
$deployAMAagents = $true
$deployDataCollectionPerfEvents = $true
$deployVMInsightsPerfAndMap = $true
$deployVMInsightsPerfOnly = $true
$deployChangeTrackingAndInventory = $true
$deployUpdateManager = $true
$deployActionGroup = $true
$deployAlerts = $true
$deployWorkbooks = $true
$deployDashboard = $true
$deploySQLBPA = $true
$deployAutomationAccount = $true
# Deploys for Azure VMs the same services selected for Azure Arc-enabled servers
$deployForAzureVMs = $true
# Global variables
$parametersFilePath = ".\Parameters.csv"
$parametersFileInput = $(Import-Csv $parametersFilePath)
$subscriptionName = $parametersFileInput.Subscription
$resourceGroup = $parametersFileInput.ResourceGroup
$namingPrefix = $parametersFileInput.NamingPrefix
$location = $parametersFileInput.Location
$policiesScope = $parametersFileInput.Scope
$emailAddress = $parametersFileInput.Email
$optionalEmailAddress = $parametersFileInput.OptionalEmail
$optionalWebhook = $parametersFileInput.OptionalWebhook
$MonitorWSName = $namingPrefix + "-la-monitor"
$ActionGroupName = $namingPrefix + "-ag"
$ActionGroupShortName = $namingPrefix + "-ag"
$AutomationAccountName = $namingPrefix + "-aa"
# Option to interrupt the deployment
Write-Host -ForegroundColor Green "STARTING THE DEPLOYMENT"
Write-Host -ForegroundColor Red "NOTE: Press CTRL+C within 10 seconds to cancel the deployment"
Start-Sleep -Seconds 10
# Login to Azure
if ($(Get-AzContext).Name -eq "Default") {
Login-AzAccount
}
# Switch Context to designated subscription if needed
if ((Get-AzContext).Subscription.Name -ne $subscriptionName) {
Select-AzSubscription -SubscriptionName $subscriptionName | Out-Null
}
# Create the ResourceGroup if needed
Get-AzResourceGroup -Name $resourceGroup -ErrorVariable notPresent -ErrorAction SilentlyContinue | Out-Null
if ($notPresent) {
Write-Host "Creating resource group $resourceGroup"
New-AzResourceGroup -Name $resourceGroup -Location $location | Out-Null
}
#region ### Deploy the Monitor Log Analytics workspace
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying the Monitor Log Analytics Workspace"
if ($deployLAW -eq $true) {
$deploymentName = "deploy_monitor_loganalytics_workspace"
$templateFile = ".\LogAnalyticsWorkspace\logAnalyticsWorkspace.json"
# Deploy the workspace
Write-Host "Deploying log analytics workspace $MonitorWSName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location | Out-Null
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy Azure Monitor Agent
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Azure Monitor Agent Policies"
if ($deployAMAagents -eq $true) {
# Assign the policies
$templateBasePath = ".\AzureMonitorAgent\Policies"
# Get the AzurePolicies ARM template files
$azurePoliciesCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
## Per each policy
foreach ($azurePolicyItem in $azurePoliciesCollection) {
# Skip Azure VMs Policies if deployment for AzureVMs is not required
if (($deployForAzureVMs -eq $false) -And ($azurePolicyItem -notlike "*Arc*" -eq $true)) {
continue
}
$azurePolicyName = "[MON][" + $namingPrefix + "] " + $($azurePolicyItem.Name).Split(".")[0]
$azurePolicyName = $azurePolicyName.substring(0, [System.Math]::Min(125, $azurePolicyName.Length))
$templateFile = "$templateBasePath\$($azurePolicyItem.Name)"
$deploymentName = "assign_policy_$($azurePolicyName)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$deploymentName = $deploymentName.substring(0, [System.Math]::Min(63, $deploymentName.Length))
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $azurePolicyName"
if ($policiesScope -eq "subscription") {
# Azure Arc-enabled servers
New-AzDeployment -Name $deploymentName -location $location -TemplateFile $templateFile `
-policyAssignmentName $azurePolicyName -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
elseif ($policiesScope -eq "resourcegroup") {
# Azure Arc-enabled servers
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile -location $location -policyAssignmentName $azurePolicyName `
-resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy Data Collection Rules and Assign Azure Policies to associate DCRs
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Perf and Events Data Collection Rules"
if ($deployDataCollectionPerfEvents -eq $true) {
## Deploy Data Collection Rules
$templateBasePath = ".\DataCollection-PerfEvents\DCRs"
# Get the DCRs ARM template files
$DCRsCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Deploy all the DCRs
foreach ($DCR in $DCRsCollection) {
$DCRName = $($DCR.Name).Split(".")[0]
$templateFile = "$templateBasePath\$($DCR.Name)"
$deploymentName = $("deploy_DCR_$DCRName").ToLower()
# Deploy this DCR
Write-Host "Deploying DCR: $DCRName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location -prefix $namingPrefix | Out-Null
}
## Assign Azure Policies to associate DCRs
$templateBasePath = ".\Policies"
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
## Get the Data Collection Rules previously created
$DCRs = Get-AzDataCollectionRule -ResourceGroupName $resourceGroup | Where-Object { $_.Name -like '*DCR-AMA-*' }
# Assign the policies
foreach ($DCR in $DCRs) {
if ($DCR.Name -like "*Windows*") {
$arcAzurePolicyName = "[MON][" + $namingPrefix + "] Configure Windows Arc Machine to be associated with " + $DCR.Name
$arcTemplateFile = "$templateBasePath\Configure Windows Arc Machine to be associated with a DCR.json"
# Deployment for Azure Windows VMs
if ($deployForAzureVMs -eq $true) {
$azAzurePolicyName = "[MON][" + $namingPrefix + "] Configure Windows Machine to be associated with " + $DCR.Name
$azTemplateFile = "$templateBasePath\Configure Windows Machine to be associated with a DCR.json"
}
}
elseif ($DCR.Name -like "*Linux*") {
$arcAzurePolicyName = "[MON][" + $namingPrefix + "] Configure Linux Arc Machine to be associated with " + $DCR.Name
$arcTemplateFile = "$templateBasePath\Configure Linux Arc Machine to be associated with a DCR.json"
# Deployment for Azure Linux VMs
if ($deployForAzureVMs -eq $true) {
$azAzurePolicyName = "[MON][" + $namingPrefix + "] Configure Linux Machine to be associated with " + $DCR.Name
$azTemplateFile = "$templateBasePath\Configure Linux Machine to be associated with a DCR.json"
}
}
# Deployment for Azure Arc VMs
$arcDeploymentName = "assign_policy_$($arcAzurePolicyName)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$arcDeploymentName = $arcDeploymentName.substring(0, [System.Math]::Min(63, $arcDeploymentName.Length))
# Deployment for Azure VMs
if ($deployForAzureVMs -eq $true) {
$azDeploymentName = "assign_policy_$($azAzurePolicyName)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$azDeploymentName = $azDeploymentName.substring(0, [System.Math]::Min(63, $azDeploymentName.Length))
}
# Assign the policy at resource group/subscription scope
$arcAzurePolicyName = $arcAzurePolicyName.substring(0, [System.Math]::Min(125, $arcAzurePolicyName.Length))
$azAzurePolicyName = $azAzurePolicyName.substring(0, [System.Math]::Min(125, $azAzurePolicyName.Length))
Write-Host "Assigning Azure Policy: $arcAzurePolicyName"
if ($policiesScope -eq "subscription") {
# Azure Arc
New-AzDeployment -Name $arcDeploymentName -location $location -TemplateFile $arcTemplateFile `
-policyAssignmentName $arcAzurePolicyName -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azAzurePolicyName"
New-AzDeployment -Name $azDeploymentName -location $location -TemplateFile $azTemplateFile `
-policyAssignmentName $azAzurePolicyName -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
elseif ($policiesScope -eq "resourcegroup") {
# Azure Arc
New-AzResourceGroupDeployment -Name $arcDeploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $arcTemplateFile -location $location -policyAssignmentName $arcAzurePolicyName `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azAzurePolicyName"
New-AzResourceGroupDeployment -Name $azDeploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $azTemplateFile -location $location -policyAssignmentName $azAzurePolicyName `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy VMInsights
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying VMInsights DCRs and related policies"
if ($deployVMInsightsPerfAndMap -eq $true -or $deployVMInsightsPerfOnly -eq $true) {
# Control: only one variable can be true
if ($deployVMInsightsPerfAndMap -eq $true -and $deployVMInsightsPerfOnly -eq $true) {
$deployVMInsightsPerfOnly = $false
}
## PART 1. Dependency Agent Policies (only needed for Map)
if ($deployVMInsightsPerfAndMap -eq $true) {
# Assign the policies
$templateBasePath = ".\DataCollection-VMInsights\Policies"
# Get the AzurePolicies ARM template files
$azurePoliciesCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
## Per each policy
foreach ($azurePolicyItem in $azurePoliciesCollection) {
# Skip Azure VMs Policies if deployment for AzureVMs is not required
if (($deployForAzureVMs -eq $false) -And ($azurePolicyItem -notlike "*Arc*" -eq $true)) {
continue
}
$azurePolicyName = "[MON][" + $namingPrefix + "] " + $($azurePolicyItem.Name).Split(".")[0]
$azurePolicyName = $azurePolicyName.substring(0, [System.Math]::Min(125, $azurePolicyName.Length))
$templateFile = "$templateBasePath\$($azurePolicyItem.Name)"
$deploymentName = "assign_policy_$($azurePolicyName)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$deploymentName = $deploymentName.substring(0, [System.Math]::Min(63, $deploymentName.Length))
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $azurePolicyName"
if ($policiesScope -eq "subscription") {
# Azure Arc-enabled servers
New-AzDeployment -Name $deploymentName -location $location -TemplateFile $templateFile `
-policyAssignmentName $azurePolicyName -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
elseif ($policiesScope -eq "resourcegroup") {
# Azure Arc-enabled servers
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile -location $location -policyAssignmentName $azurePolicyName `
-resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
## PART 2. Deploy Data Collection Rules
$templateBasePath = ".\DataCollection-VMInsights\DCRs"
# Get the DCRs ARM template files
$DCRsCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Deploy all the DCRs
foreach ($DCR in $DCRsCollection) {
if (($deployVMInsightsPerfAndMap -eq $true -and $DCR.Name -like "*PerfAndMap*") -or ($deployVMInsightsPerfOnly -eq $true -and $DCR.Name -like "*PerfOnly*")) {
$DCRName = $($DCR.Name).Split(".")[0]
$templateFile = "$templateBasePath\$($DCR.Name)"
$deploymentName = $("deploy_DCR_$DCRName").ToLower()
# Deploy this DCR
Write-Host "Deploying DCR: $DCRName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location -prefix $namingPrefix | Out-Null
}
}
## PART 3. Assign Azure Policies to associate DCRs
## Get the Data Collection Rules previously created
$DCRs = Get-AzDataCollectionRule -ResourceGroupName $resourceGroup | Where-Object { $_.Name -like '*DCR-VMI-*' }
# Assign the policies. There is a single DCR for VMInsights
foreach ($DCR in $DCRs) {
# Associate Arc Windows VMInsights DCR via Azure Policy
$arcAzurePolicyNameWindows = "[MON][" + $namingPrefix + "] Configure Windows Arc Machine to be associated with " + $DCR.Name
$arcAzurePolicyNameWindows = $arcAzurePolicyNameWindows.substring(0, [System.Math]::Min(125, $arcAzurePolicyNameWindows.Length))
$arcTemplateFileWindows = ".\Policies\Configure Windows Arc Machine to be associated with a DCR.json"
$arcDeploymentNameWindows = "assign_policy_$($arcAzurePolicyNameWindows)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$arcDeploymentNameWindows = $arcDeploymentNameWindows.substring(0, [System.Math]::Min(63, $arcDeploymentNameWindows.Length))
# Associate Arc Linux VMInsights DCR via Azure Policy
$arcAzurePolicyNameLinux = "[MON][" + $namingPrefix + "] Configure Linux Arc Machine to be associated with " + $DCR.Name
$arcAzurePolicyNameLinux = $arcAzurePolicyNameLinux.substring(0, [System.Math]::Min(125, $arcAzurePolicyNameLinux.Length))
$arcTemplateFileLinux = ".\Policies\Configure Linux Arc Machine to be associated with a DCR.json"
$arcDeploymentNameLinux = "assign_policy_$($arcAzurePolicyNameLinux)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$arcDeploymentNameLinux = $arcDeploymentNameLinux.substring(0, [System.Math]::Min(63, $arcDeploymentNameLinux.Length))
# Azure VMs
if ($deployForAzureVMs -eq $true) {
# Associate Azure Windows VMInsights DCR via Azure Policy
$azAzurePolicyNameWindows = "[MON][" + $namingPrefix + "] Configure Windows Machine to be associated with " + $DCR.Name
$azAzurePolicyNameWindows = $azAzurePolicyNameWindows.substring(0, [System.Math]::Min(125, $azAzurePolicyNameWindows.Length))
$azTemplateFileWindows = ".\Policies\Configure Windows Machine to be associated with a DCR.json"
$azDeploymentNameWindows = "assign_policy_$($azAzurePolicyNameWindows)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$azDeploymentNameWindows = $azDeploymentNameWindows.substring(0, [System.Math]::Min(63, $azDeploymentNameWindows.Length))
# Associate Azure Linux VMInsights DCR via Azure Policy
$azAzurePolicyNameLinux = "[MON][" + $namingPrefix + "] Configure Linux Machine to be associated with " + $DCR.Name
$azAzurePolicyNameLinux = $azAzurePolicyNameLinux.substring(0, [System.Math]::Min(125, $azAzurePolicyNameLinux.Length))
$azTemplateFileLinux = ".\Policies\Configure Linux Machine to be associated with a DCR.json"
$azDeploymentNameLinux = "assign_policy_$($azAzurePolicyNameLinux)".Replace(' ', '').Replace("[MON][" + $namingPrefix + "]", '')
$azDeploymentNameLinux = $azDeploymentNameLinux.substring(0, [System.Math]::Min(63, $azDeploymentNameLinux.Length))
}
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $arcAzurePolicyNameWindows"
Write-Host "Assigning Azure Policy: $arcAzurePolicyNameLinux"
if ($policiesScope -eq "subscription") {
# Arc Windows
New-AzDeployment -Name $arcDeploymentNameWindows -location $location -TemplateFile $arcTemplateFileWindows `
-policyAssignmentName $arcAzurePolicyNameWindows -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Arc Linux
New-AzDeployment -Name $arcDeploymentNameLinux -location $location -TemplateFile $arcTemplateFileLinux `
-policyAssignmentName $arcAzurePolicyNameLinux -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azAzurePolicyNameWindows"
Write-Host "Assigning Azure Policy: $azAzurePolicyNameLinux"
# Azure Windows
New-AzDeployment -Name $azDeploymentNameWindows -location $location -TemplateFile $azTemplateFileWindows `
-policyAssignmentName $azAzurePolicyNameWindows -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Linux
New-AzDeployment -Name $azDeploymentNameLinux -location $location -TemplateFile $azTemplateFileLinux `
-policyAssignmentName $azAzurePolicyNameLinux -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
elseif ($policiesScope -eq "resourcegroup") {
# Arc Windows
New-AzResourceGroupDeployment -Name $arcDeploymentNameWindows -ResourceGroupName $resourceGroup `
-TemplateFile $arcTemplateFileWindows -location $location -policyAssignmentName $arcAzurePolicyNameWindows `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Arc Linux
New-AzResourceGroupDeployment -Name $arcDeploymentNameLinux -ResourceGroupName $resourceGroup `
-TemplateFile $arcTemplateFileLinux -location $location -policyAssignmentName $arcAzurePolicyNameLinux `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azAzurePolicyNameWindows"
Write-Host "Assigning Azure Policy: $azAzurePolicyNameLinux"
# Azure Windows
New-AzResourceGroupDeployment -Name $azDeploymentNameWindows -ResourceGroupName $resourceGroup `
-TemplateFile $azTemplateFileWindows -location $location -policyAssignmentName $azAzurePolicyNameWindows `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Linux
New-AzResourceGroupDeployment -Name $azDeploymentNameLinux -ResourceGroupName $resourceGroup `
-TemplateFile $azTemplateFileLinux -location $location -policyAssignmentName $azAzurePolicyNameLinux `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy Change Tracking and Inventory
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Change Tracking and Inventory"
if ($deployChangeTrackingAndInventory -eq $true) {
## Deploy Data Collection Rules
$templateBasePath = ".\ChangeTrackingAndInventory\DCRs"
# Get the DCRs ARM template files
$DCRsCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Deploy all the DCRs
foreach ($DCR in $DCRsCollection) {
$DCRName = $($DCR.Name).Split(".")[0]
$templateFile = "$templateBasePath\$($DCR.Name)"
$deploymentName = $("deploy_DCR_$DCRName").ToLower()
# Deploy this DCR
Write-Host "Deploying DCR: $DCRName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location -prefix $namingPrefix | Out-Null
}
# Assign the policies
$templateBasePath = ".\ChangeTrackingAndInventory\Policies"
# Get the AzurePolicies ARM template files
$azurePoliciesCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
## Get the Data Collection Rule previously created
$DCR = Get-AzDataCollectionRule -ResourceGroupName $resourceGroup | Where-Object { $_.Name -like '*DCR-ChangeTracking*' }
## Per each policy
foreach ($azurePolicyItem in $azurePoliciesCollection) {
# Skip Azure VMs Policies if deployment for AzureVMs is not required
if (($deployForAzureVMs -eq $false) -And ($azurePolicyItem -notlike "*Arc*" -eq $true)) {
continue
}
$azurePolicyName = "[CT][" + $namingPrefix + "] " + $($azurePolicyItem.Name).Split(".")[0]
$azurePolicyName = $azurePolicyName.substring(0, [System.Math]::Min(125, $azurePolicyName.Length))
$templateFile = "$templateBasePath\$($azurePolicyItem.Name)"
$deploymentName = "assign_policy_$($azurePolicyName)".Replace(' ', '').Replace("[CT][" + $namingPrefix + "]", '')
$deploymentName = $deploymentName.substring(0, [System.Math]::Min(63, $deploymentName.Length))
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $azurePolicyName"
if ($policiesScope -eq "subscription") {
# Azure Arc-enabled servers
New-AzDeployment -Name $deploymentName -location $location -TemplateFile $templateFile `
-policyAssignmentName $azurePolicyName -dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
elseif ($policiesScope -eq "resourcegroup") {
# Azure Arc-enabled servers
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile -location $location -policyAssignmentName $azurePolicyName `
-dcrResourceId $DCR.Id -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy Azure Update Manager
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Azure Update Manager Policies"
if ($deployUpdateManager -eq $true) {
$templateBasePath = ".\UpdateManager\Policies"
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
# Azure Arc-enabled servers
$arcTemplateFile = "$templateBasePath\Configure periodic checking for missing system updates on azure Arc-enabled servers.json"
# Windows Update Assessment Policy Assignment
$arcPolicyNameWindows = "[UM][" + $namingPrefix + "] Configure periodic checking for missing system updates on Windows Arc-enabled servers"
$arcPolicyNameWindows = $arcPolicyNameWindows.substring(0, [System.Math]::Min(125, $arcPolicyNameWindows.Length))
$arcDeploymentNameWindows = "assign_policy_windows$($arcPolicyNameWindows)".Replace(' ', '').Replace("[UM][" + $namingPrefix + "]", '')
$arcDeploymentNameWindows = $arcDeploymentNameWindows.substring(0, [System.Math]::Min(63, $arcDeploymentNameWindows.Length))
# Linux Update Assessment Policy Assignment
$arcPolicyNameLinux = "[UM][" + $namingPrefix + "] Configure periodic checking for missing system updates on Linux Arc-enabled servers"
$arcPolicyNameLinux = $arcPolicyNameLinux.substring(0, [System.Math]::Min(125, $arcPolicyNameLinux.Length))
$arcDeploymentNameLinux = "assign_policy_linux$($arcPolicyNameLinux)".Replace(' ', '').Replace("[UM][" + $namingPrefix + "]", '')
$arcDeploymentNameLinux = $arcDeploymentNameLinux.substring(0, [System.Math]::Min(63, $arcDeploymentNameLinux.Length))
# Azure VMs
if ($deployForAzureVMs -eq $true) {
# Azure servers
$azTemplateFile = "$templateBasePath\Configure periodic checking for missing system updates on azure virtual machines.json"
# Windows Update Assessment Policy Assignment
$azPolicyNameWindows = "[UM][" + $namingPrefix + "] Configure periodic checking for missing system updates on Windows servers"
$azPolicyNameWindows = $azPolicyNameWindows.substring(0, [System.Math]::Min(125, $azPolicyNameWindows.Length))
$azDeploymentNameWindows = "assign_policy_windows$($azPolicyNameWindows)".Replace(' ', '').Replace("[UM][" + $namingPrefix + "]", '')
$azDeploymentNameWindows = $azDeploymentNameWindows.substring(0, [System.Math]::Min(63, $azDeploymentNameWindows.Length))
# Linux Update Assessment Policy Assignment
$azPolicyNameLinux = "[UM][" + $namingPrefix + "] Configure periodic checking for missing system updates on Linux servers"
$azPolicyNameLinux = $azPolicyNameLinux.substring(0, [System.Math]::Min(125, $azPolicyNameLinux.Length))
$azDeploymentNameLinux = "assign_policy_linux$($azPolicyNameLinux)".Replace(' ', '').Replace("[UM][" + $namingPrefix + "]", '')
$azDeploymentNameLinux = $azDeploymentNameLinux.substring(0, [System.Math]::Min(63, $azDeploymentNameLinux.Length))
}
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $arcPolicyNameWindows"
Write-Host "Assigning Azure Policy: $arcPolicyNameLinux"
if ($policiesScope -eq "subscription") {
# Azure Arc Windows
New-AzDeployment -Name $arcDeploymentNameWindows -location $location -TemplateFile $arcTemplateFile `
-policyAssignmentName $arcPolicyNameWindows -osType "Windows" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Arc Linux
New-AzDeployment -Name $arcDeploymentNameLinux -location $location -TemplateFile $arcTemplateFile `
-policyAssignmentName $arcPolicyNameLinux -osType "Linux" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azPolicyNameWindows"
Write-Host "Assigning Azure Policy: $azPolicyNameLinux"
# Azure Windows
New-AzDeployment -Name $azDeploymentNameWindows -location $location -TemplateFile $azTemplateFile `
-policyAssignmentName $azPolicyNameWindows -osType "Windows" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Linux
New-AzDeployment -Name $azDeploymentNameLinux -location $location -TemplateFile $azTemplateFile `
-policyAssignmentName $azPolicyNameLinux -osType "Linux" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
elseif ($policiesScope -eq "resourcegroup") {
# Azure Arc Windows
New-AzResourceGroupDeployment -Name $arcDeploymentNameWindows -ResourceGroupName $resourceGroup `
-TemplateFile $arcTemplateFile -location $location -policyAssignmentName $arcPolicyNameWindows `
-osType "Windows" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Arc Linux
New-AzResourceGroupDeployment -Name $arcDeploymentNameLinux -ResourceGroupName $resourceGroup `
-TemplateFile $arcTemplateFile -location $location -policyAssignmentName $arcPolicyNameLinux `
-osType "Linux" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure VMs
if ($deployForAzureVMs -eq $true) {
Write-Host "Assigning Azure Policy: $azPolicyNameWindows"
Write-Host "Assigning Azure Policy: $azPolicyNameLinux"
# Azure Windows
New-AzResourceGroupDeployment -Name $azDeploymentNameWindows -ResourceGroupName $resourceGroup `
-TemplateFile $azTemplateFile -location $location -policyAssignmentName $azPolicyNameWindows `
-osType "Windows" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
# Azure Linux
New-AzResourceGroupDeployment -Name $azDeploymentNameLinux -ResourceGroupName $resourceGroup `
-TemplateFile $azTemplateFile -location $location -policyAssignmentName $azPolicyNameLinux `
-osType "Linux" -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy SQL BPA
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying SQL BPA Policy"
if ($deploySQLBPA -eq $true) {
$templateBasePath = ".\SQLServerBPA\Policies"
$templateFile = "$templateBasePath\Configure Arc-enabled Servers with SQL Server extension installed to enable SQL best practices assessment.json"
# Parameter to make unique Microsoft.Authorization/roleAssignments name at tenant level
$resourceGroupID = (Get-AzResourceGroup -Name $resourceGroup).ResourceId
# Windows Update Assessment Policy Assignment
$azurePolicyName = "[SQL][" + $namingPrefix + "] Configure Arc-enabled Servers with SQL Server extension installed to enable SQL best practices assessment"
$azurePolicyName = $azurePolicyName.substring(0, [System.Math]::Min(125, $azurePolicyName.Length))
$deploymentName = "assign_policy_$($azurePolicyName)".Replace(' ', '').Replace("[SQL][" + $namingPrefix + "]", '')
$deploymentName = $deploymentName.substring(0, [System.Math]::Min(63, $deploymentName.Length))
# Assign the policy at resource group/subscription scope
Write-Host "Assigning Azure Policy: $azurePolicyName"
if ($policiesScope -eq "subscription") {
New-AzDeployment -Name $deploymentName -location $location -TemplateFile $templateFile `
-policyAssignmentName $azurePolicyName -workspaceName $MonitorWSName -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
elseif ($policiesScope -eq "resourcegroup") {
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup `
-TemplateFile $templateFile -location $location -policyAssignmentName $azurePolicyName `
-workspaceName $MonitorWSName -resourceGroupID $resourceGroupID -prefixName $namingPrefix | Out-Null
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Create Azure Monitor action group with an email address
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Azure Monitor Action Group"
if ($deployActionGroup -eq $true) {
# Deploy email address and optional email address and webhook
if ($optionalEmailAddress -ne "" -and $optionalWebhook -ne "") {
"Deploying Azure Monitor Action Group with email address, optional email address and webhook"
$deploymentName = "deploy_action_group_optionalEmailWebhook"
$templateFile = ".\ActionGroup\actionGroupOptionalEmailWebhook.json"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-actionGroupName $actionGroupName -actionGroupShortName $actionGroupShortName -emailAddress $emailAddress `
-optionalEmailAddress $optionalEmailAddress -webhookURL $optionalWebhook | Out-Null
}
# Deploy email address and optional email address
elseif ($optionalEmailAddress -ne "" -and $optionalWebhook -eq "") {
"Deploying Azure Monitor Action Group with email address and optional email address"
$deploymentName = "deploy_action_group_optionalEmail"
$templateFile = ".\ActionGroup\actionGroupOptionalEmail.json"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-actionGroupName $actionGroupName -actionGroupShortName $actionGroupShortName -emailAddress $emailAddress `
-optionalEmailAddress $optionalEmailAddress | Out-Null
}
# Deploy email address and optional webhook
elseif ($optionalEmailAddress -eq "" -and $optionalWebhook -ne "") {
"Deploying Azure Monitor Action Group with email address and webhook"
$deploymentName = "deploy_action_group_optionalWebhook"
$templateFile = ".\ActionGroup\actionGroupOptionalWebhook.json"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-actionGroupName $actionGroupName -actionGroupShortName $actionGroupShortName -emailAddress $emailAddress `
-webhookURL $optionalWebhook | Out-Null
}
# Deploy only email address
elseif ($optionalEmailAddress -eq "" -and $optionalWebhook -eq "") {
"Deploying Azure Monitor Action Group with email address"
$deploymentName = "deploy_action_group"
$templateFile = ".\ActionGroup\actionGroup.json"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-actionGroupName $actionGroupName -actionGroupShortName $actionGroupShortName -emailAddress $emailAddress | Out-Null
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Set up the alert baseline
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Azure Monitor alerts"
if ($deployAlerts -eq $true) {
$templateBasePath = ".\Alerts"
# Get the alerts ARM template files
$alertCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Deploy all the alerts
foreach ($alert in $alertCollection) {
$alertName = $($alert.Name).Split(".")[0]
$templateFile = "$templateBasePath\$($alert.Name)"
$deploymentName = $("deploy_alert_$alertName").ToLower()
# Deploy this alert
Write-Host "Deploying alert: $alertName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location -actionGroupName $actionGroupName -prefix $namingPrefix | Out-Null
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy Azure Workbooks
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying Azure workbooks"
if ($deployWorkbooks -eq $true) {
$templateBasePath = ".\Workbooks"
# Get the workbooks ARM template files
$workbookCollection = $(Get-ChildItem -Path $templateBasePath | Where-Object { $_.name -like "*.json" })
# Deploy all workbooks
foreach ($workbook in $workbookCollection) {
$workbookName = $($workbook.Name).Split(".")[0]
$templateFile = "$templateBasePath\$($workbook.Name)"
$deploymentName = $("Deploy_Workbook_$workbookName").ToLower()
# Deploy this workbook
Write-Host "Deploying workbook $workbookName"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -prefixName $namingPrefix | Out-Null
}
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Deploy the Azure dashboard
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying the Azure Dashboard"
if ($deployDashboard -eq $true) {
$deploymentName = "deploy_azure_dashboard"
$templateFile = ".\Dashboard\dashboard.json"
$dashboardName = "Azure Arc Dashboard - " + $namingPrefix
# Deploy the Azure Dashboard
Write-Host "Deploying the Azure Dashboard"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-workspaceName $MonitorWSName -location $location -dashboardName $dashboardName -prefixName $namingPrefix | Out-Null
}
else {
Write-Host "Skipped"
}
#endregion
#region ### Automation account related resources
Write-Host ""
Write-Host -ForegroundColor Cyan "Deploying the Automation Account and related resources"
if ($deployAutomationAccount -eq $true) {
$deploymentName = "deploy_automation_account"
$templateFile = ".\AutomationAccount\automationAccount.json"
$managedIdentityScope = $parametersFileInput.Scope
# Deploy and link the automation account
Write-Host "Deploying the automation account"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName $resourceGroup -TemplateFile $templateFile `
-automationAccountName $automationAccountName -location $location | Out-Null
# Create and publish the runbook
$runbookName = "AutoRemediateFrameworkPolicies"
$runbookType = "PowerShell72"
$runbookCodePath = ".\AutomationAccount\Runbook\AutoRemediateFrameworkPolicies.ps1"
Write-Host "Importing the required runbooks"
Import-AzAutomationRunbook -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroup -Name $runbookName `
-Type $runbookType -Path $runbookCodePath | Out-null
Write-Host "Publishing the required runbooks"
Publish-AzAutomationRunbook -AutomationAccountName $automationAccountName -ResourceGroupName $resourceGroup -Name $runbookName | Out-null
# Create and link the schedule
Write-Host "Creating the daily schedule and linking it to the runbook"
$StartTime = Get-Date "23:00:00"
if ($StartTime -lt (Get-Date)) { $StartTime = $StartTime.AddDays(1) }
$EndTime = $StartTime.AddYears(99)
$scheduleName = "dailyschedule11pm"
$TimeZone = ([System.TimeZoneInfo]::Local).Id
New-AzAutomationSchedule -AutomationAccountName $automationAccountName -Name $scheduleName -StartTime $StartTime -ExpiryTime `
$EndTime -DayInterval 1 -ResourceGroupName $resourceGroup -TimeZone $TimeZone | Out-null
# Policy remedation will happen at subscription or resource group level, depending on the Scope parameter
if ($managedIdentityScope -eq "subscription") {
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccountName `
-Name $runbookName -ScheduleName $scheduleName -ResourceGroupName $resourceGroup `
-Parameters @{"prefixName" = $namingPrefix } | Out-null
}
elseif ($managedIdentityScope -eq "resourcegroup") {
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccountName `
-Name $runbookName -ScheduleName $scheduleName -ResourceGroupName $resourceGroup `
-Parameters @{"resourceGroup" = $resourceGroup; "prefixName" = $namingPrefix } | Out-null
}
# Get automation account managed identity and assign permissions to remediate policies at subscription/resource group level
#Wait for the system managed identity to be available
Write-Host "Waiting for the automation account system managed identity... " -NoNewline
while ($null -eq (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccountName -ErrorAction SilentlyContinue).Identity.PrincipalId) {
Write-Host "." -NoNewline
Start-Sleep -Seconds 5
}
Write-Host "."
$principalId = (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccountName).Identity.PrincipalId
if ($managedIdentityScope -eq "subscription") {
Write-Host "Assigning Resource Policy Contributor role at subscription level"
New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName "Resource Policy Contributor" | Out-null
}
elseif ($managedIdentityScope -eq "resourcegroup") {
Write-Host "Assigning Resource Policy Contributor role at resource group level"
New-AzRoleAssignment -ObjectId $principalId -RoleDefinitionName "Resource Policy Contributor" `
-ResourceGroupName $resourceGroup | Out-null
}
}
else {
Write-Host "Skipped"
}
#endregion