-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUpdateWorkbooksDashboards.ps1
507 lines (399 loc) · 25 KB
/
UpdateWorkbooksDashboards.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
#Requires -Version 5.0
<#
.NAME
Update Workboks & Dashboards - ClientInspector
.AUTHOR
Morten Knudsen, Microsoft MVP - https://mortenknudsen.net
.LICENSE
Licensed under the MIT license.
.PROJECTURI
https://github.com/KnudsenMorten/ClientInspectorV2-DeploymentKit
.WARRANTY
Use at your own risk, no warranty given!
#>
Write-Output "ClientInspector-DeploymentKit | Inventory of Operational & Security-related information"
Write-Output ""
Write-Output "Developed by Morten Knudsen, Microsoft MVP"
Write-Output ""
Write-Output "More information:"
Write-Output "https://github.com/KnudsenMorten/ClientInspectorV2-DeploymentKit"
Write-Output ""
#------------------------------------------------------------------------------------------------------------
# Variables
#------------------------------------------------------------------------------------------------------------
# put in your path where ClientInspector, AzLogDcrIngestPS and workbooks/dashboards will be downloaded to !
$FolderRoot = (Get-location).Path
# Azure Active Directory (AAD)
$TenantId = ""
# Azure LogAnalytics
$LogAnalyticsSubscription = ""
$LogAnalyticsResourceGroup = ""
$LoganalyticsWorkspaceName = ""
$LoganalyticsLocation = "westeurope"
# Azure Workbooks & Dashboards
$TemplateCategory = "xxx IT Operation Security Templates"
$WorkbookDashboardResourceGroup = ""
$WorkBook_Repository_Path = "AZURE_WORKBOOKS_LATEST_RELEASE_V2"
$Dashboard_Repository_Path = "AZURE_DASHBOARDS_LATEST_RELEASE_V2"
$Workbooks = @("ANTIVIRUS SECURITY CENTER - CLIENTS - V2.json", `
"APPLICATIONS - CLIENTS - V2.json", `
"BITLOCKER - CLIENTS - V2.json", `
"DEFENDER AV - CLIENTS - V2.json", `
"GROUP POLICY REFRESH - CLIENTS - V2.json", `
"INVENTORY - CLIENTS - V2.json", `
"INVENTORY COLLECTION ISSUES - CLIENTS - V2.json", `
"LAPS - CLIENTS - V2.json", `
"LOCAL ADMINS - CLIENTS - V2.json",`
"NETWORK INFORMATION - CLIENTS - V2.json", `
"OFFICE - CLIENTS - V2.json", `
"UNEXPECTED SHUTDOWNS - CLIENTS - V2.json", `
"WINDOWS FIREWALL - CLIENTS - V2.json", `
"WINDOWS UPDATE - CLIENTS - V2.json"
)
$Dashboards = @("ANTIVIRUS SECURITY CENTER - CLIENTS - V2.json", `
"APPLICATIONS - CLIENTS - V2.json", `
"BITLOCKER - CLIENTS - V2.json", `
"CLIENT KPI STATUS - V2.json", `
"DEFENDER AV - CLIENTS - V2.json", `
"GROUP POLICY REFRESH - CLIENTS - V2.json", `
"INVENTORY - CLIENTS - V2.json", `
"INVENTORY COLLECTION ISSUES - CLIENTS - V2.json", `
"LAPS - CLIENTS - V2.json", `
"LOCAL ADMINS GROUP - CLIENTS - V2.json",`
"NETWORK INFORMATION - CLIENTS - V2.json", `
"OFFICE - CLIENTS - V2.json", `
"UNEXPECTED SHUTDOWNS - CLIENTS - V2.json", `
"WINDOWS FIREWALL - CLIENTS - V2.json", `
"WINDOWS UPDATE - CLIENTS - V2.json"
)
#------------------------------------------------------------------------------------------------------------
# Verification download path
#------------------------------------------------------------------------------------------------------------
# put in your path where ClientInspector, AzLogDcrIngestPS and workbooks/dashboards will be downloaded to !
$FolderRoot = (Get-location).Path + "\" + $LoganalyticsWorkspaceName
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Delete"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Cancel"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$heading = "Download path"
$message = "This deployment kit will download latest files into current directory. Do you want to continue with this path? `n`n $($FolderRoot) "
$Prompt = $host.ui.PromptForChoice($heading, $message, $options, 1)
switch ($prompt) {
0
{
# Continuing
}
1
{
Write-Host "No" -ForegroundColor Red
Exit
}
}
MD $FolderRoot -ErrorAction SilentlyContinue -Force | Out-Null
CD $FolderRoot | Out-Null
#------------------------------------------------------------------------------------------------------------
# Downloading newest versions of workbooks fra ClientInspector-DeploymentKit Github
#------------------------------------------------------------------------------------------------------------
# path to store files
$TempPath = $FolderRoot + "\" + $WorkBook_Repository_Path
# Checking if existing workbook files are found. If $true, then they will be deleted
$ExistFilesCheck = Get-ChildItem "$($TempPath)\*.json" -ErrorAction SilentlyContinue
If ($ExistFilesCheck)
{
Write-Output "Existing files found .... removing ensuring latest are used !"
Remove-Item -Path $ExistFilesCheck -Force
}
# Creating
MD $TempPath -ErrorAction SilentlyContinue -force | Out-Null
# Downloading
ForEach ($Workbook in $Workbooks)
{
$SourceFile = $Workbook.replace(" ","%20")
$SourcePath = "https://raw.githubusercontent.com/KnudsenMorten/ClientInspectorV2-DeploymentKit/main/" + $WorkBook_Repository_Path + "/" + $SourceFile
$DestinationPath = $TempPath + "\" + $Workbook
Write-Output ""
Write-Output "Downloading latest version of Azure Workbooks [ $($Workbook) ]"
Write-Output " from https://github.com/KnudsenMorten/ClientInspectorV2-DeploymentKit"
Write-Output " into local path $($TempPath)"
# download newest version
$Download = (New-Object System.Net.WebClient).DownloadFile($SourcePath, $DestinationPath)
}
#------------------------------------------------------------------------------------------------------------
# Downloading newest versions of dashboards fra ClientInspector-DeploymentKit Github
#------------------------------------------------------------------------------------------------------------
# path to store files
$TempPath = $FolderRoot + "\" + $Dashboard_Repository_Path
# Checking if existing dashboards files are found. If $true, then they will be deleted
$ExistFilesCheck = Get-ChildItem "$($TempPath)\*.json" -ErrorAction SilentlyContinue
If ($ExistFilesCheck)
{
Write-Output "Existing files found .... removing ensuring latest are used !"
Remove-Item -Path $ExistFilesCheck -Force
}
# Creating
MD $TempPath -ErrorAction SilentlyContinue -Force | Out-Null
ForEach ($Dashboard in $Dashboards)
{
$SourceFile = $Dashboard.replace(" ","%20")
$SourcePath = "https://raw.githubusercontent.com/KnudsenMorten/ClientInspectorV2-DeploymentKit/main/" + $Dashboard_Repository_Path + "/" + $SourceFile
$DestinationPath = $TempPath + "\" + $Dashboard
Write-Output ""
Write-Output "Downloading latest version of Azure Dashboards [ $($Dashboard) ]"
Write-Output " from https://github.com/KnudsenMorten/ClientInspectorV2-DeploymentKit"
Write-Output " into $($TempPath)"
# download newest version
$Download = (New-Object System.Net.WebClient).DownloadFile($SourcePath, $DestinationPath)
}
#------------------------------------------------------------------------------------------------------------
# Functions
#------------------------------------------------------------------------------------------------------------
Write-Output "Checking needed functions ... Please Wait !"
$ModuleCheck = Get-Module -Name Az.Resources -ListAvailable -ErrorAction SilentlyContinue
If (!($ModuleCheck))
{
Write-Output "Installing Az-module in CurrentUser scope ... Please Wait !"
Install-module -Name Az -Force -Scope CurrentUser
}
$ModuleCheck = Get-Module -Name Az.Portal -ListAvailable -ErrorAction SilentlyContinue
If (!($ModuleCheck))
{
Write-Output "Installing Az.Portal in CurrentUser scope ... Please Wait !"
Install-module -Name Az.Portal -Force -Scope CurrentUser
}
$ModuleCheck = Get-Module -Name Microsoft.Graph -ListAvailable -ErrorAction SilentlyContinue
If (!($ModuleCheck))
{
Write-Output "Installing Microsoft.Graph in CurrentUser scope ... Please Wait !"
Install-module -Name Microsoft.Graph -Force -Scope CurrentUser
}
<#
Install-module Az -Scope CurrentUser
Install-module Microsoft.Graph -Scope CurrentUser
install-module Az.portal -Scope CurrentUser
Import-module Az -Scope CurrentUser
Import-module Az.Accounts -Scope CurrentUser
Import-module Az.Resources -Scope CurrentUser
Import-module Microsoft.Graph.Applications -Scope CurrentUser
Import-Module Microsoft.Graph.DeviceManagement.Enrolment -Scope CurrentUser
#>
#------------------------------------------------------------------------------------------------------------
# Connection
#------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------
# Connect to Azure
#---------------------------------------------------------
Connect-AzAccount -Tenant $TenantId -WarningAction SilentlyContinue
#---------------------------------------------------------
# Get Access Token
#---------------------------------------------------------
$AccessToken = Get-AzAccessToken -ResourceUrl https://management.azure.com/
$AccessToken = $AccessToken.Token
#---------------------------------------------------------
# Build Headers for Azure REST API with access token
#---------------------------------------------------------
$Headers = @{
"Authorization"="Bearer $($AccessToken)"
"Content-Type"="application/json"
}
#---------------------------------------------------------
# Connect to Microsoft Graph
#---------------------------------------------------------
<#
Find-MgGraphCommand -command Add-MgApplicationPassword | Select -First 1 -ExpandProperty Permissions
#>
$MgScope = @(
"Application.ReadWrite.All",`
"Directory.Read.All",`
"Directory.AccessAsUser.All",
"RoleManagement.ReadWrite.Directory"
)
Connect-MgGraph -TenantId $TenantId -Scopes $MgScope
#-------------------------------------------------------------------------------------
# Azure Context
#-------------------------------------------------------------------------------------
Write-Output ""
Write-Output "Validating Azure context is subscription [ $($LogAnalyticsSubscription) ]"
$AzContext = Get-AzContext
If ($AzContext.Subscription -ne $LogAnalyticsSubscription )
{
Write-Output ""
Write-Output "Switching Azure context to subscription [ $($LogAnalyticsSubscription) ]"
$AzContext = Set-AzContext -Subscription $LogAnalyticsSubscription -Tenant $TenantId
}
#------------------------------------------------------------------------------------------------------------
# Azure Workbooks & Dashboards
#------------------------------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
# Azure WorkBook Deployment
#-------------------------------------------------------------------------------------
#------------------------------------
# PreReq
#------------------------------------
Write-Output "Building list Azure workbooks to deploy"
$TempPath = (Get-location).Path + "\" + $WorkBook_Repository_Path
$Files = Get-ChildItem -Path $TempPath | %{$_.FullName}
#-------------------------------------------------------------------------------------
# Create the resource group for Azure Workbooks & Dashboards
#-------------------------------------------------------------------------------------
Write-Output ""
Write-Output "Validating Azure resource group exist [ $($WorkbookDashboardResourceGroup) ]"
try {
Get-AzResourceGroup -Name $WorkbookDashboardResourceGroup -ErrorAction Stop
} catch {
Write-Output ""
Write-Output "Creating Azure resource group [ $($WorkbookDashboardResourceGroup) ]"
New-AzResourceGroup -Name $WorkbookDashboardResourceGroup -Location $LoganalyticsLocation
}
#-------------------------------------------------------------------------------------
# Get workspace details
#-------------------------------------------------------------------------------------
$LogWorkspaceInfo = Get-AzOperationalInsightsWorkspace -Name $LoganalyticsWorkspaceName -ResourceGroupName $LogAnalyticsResourceGroup
$LogAnalyticsWorkspaceResourceId = $LogWorkspaceInfo.ResourceId
#-------------------------------------------------------------------------------------
# Deployment of Workbooks (loop)
#-------------------------------------------------------------------------------------
ForEach ($File in $Files)
{
# read ARM file
$TemplateFileName = $File.Split("\")[-1]
$TemplateName = $TemplateFileName.Split(".")[0]
Write-Output ""
Write-Output "Deployment of Azure Workbook [ $($TemplateName) ] in progress .... please wait !"
Write-Output " Adjusting ARM template with LogAnalytics workspace information"
# convert file to JSON
$ArmTemplate = Get-Content $File -Raw -Encoding UTF8
$ArmTemplateJson = $ArmTemplate | ConvertFrom-Json
# Add galleries definition to convert workbook to a workbook template
$ArmTemplateJson.resources.properties | Add-Member -NotePropertyName "galleries" -NotePropertyValue "" -Force
$gallerydefinition = [PSCustomObject]@{
name = $TemplateName
category = $TemplateCategory
order = 100
type = "workbook"
resourceType = "Azure Monitor"
}
# add gallerydefinition to object as array
$ArmTemplateJson.resources.properties.galleries = @($gallerydefinition)
# tell ARM template it should install as workbook templates
$ArmTemplateJson.resources | Add-Member -MemberType NoteProperty -Name "type" -Value "microsoft.insights/workbooktemplates" -Force
# change name for template
$ArmTemplateJson.resources | Add-Member -MemberType NoteProperty -Name "name" -Value $TemplateName -Force
# change API version to support workbook templates
$ArmTemplateJson.resources | Add-Member -MemberType NoteProperty -Name "apiVersion" -Value "2020-11-20" -Force
# convert JSON-formatted data in templates to PSCustomObject
$Data = $ArmTemplateJson.resources.properties.serializedData | ConvertFrom-Json
# Updating LogAnalytics workspaceId
$IndexCount = $Data.Items.Count
$Index = 0
Do
{
$Data.items[$Index].content.crossComponentResources = @("$LogAnalyticsWorkspaceResourceId")
$Index += 1
}
Until ($Index -eq $IndexCount)
# Converting modified PSCustomObject data to compressed JSON
$Data = $Data | ConvertTo-Json -Depth 50 -Compress
# Updating ARM template object with modified data (workbook) - not used if workbook template
# $ArmTemplateJson.resources.properties.serializedData = $Data
# Updating ARM template object with modified data (workbooktemplate)
$ArmTemplateJson.resources.properties | Add-Member -NotePropertyName "templateData" -NotePropertyValue "" -Force
$ArmTemplateJson.resources.properties.templateData = $Data
Write-Output " Building temporary ARM template [ $($TemplateFileName) ] in $($ENV:TEMP) folder"
# Export object to ARM template file
$ArmTemplateExport = $ArmTemplateJson | ConvertTo-Json -Depth 50
$TemplateFile = $Env:TEMP + "\" + $TemplateFileName
$ArmTemplateExport | out-file $TemplateFile -Force
Write-Output " Starting deployment of workbook [ $($TemplateName) ] "
Write-Output ""
# parameters for ARM deployment
$parameters = @{
'Name' = "ArmDeployment" + (Get-Random -Maximum 100000)
'ResourceGroup' = $WorkbookDashboardResourceGroup
'TemplateFile' = $TemplateFile
'workbookType' = "workbook"
'workbookSourceId' = "Azure Monitor"
'workbookdisplayName' = $TemplateName
'Verbose' = $true
}
New-AzResourceGroupDeployment @parameters
}
#-------------------------------------------------------------------------------------
# Azure Dashboard Deployment
#-------------------------------------------------------------------------------------
#------------------------------------
# PreReq
#------------------------------------
Write-Output "Building list Azure dashboards to deploy"
$TempPath = (Get-location).Path + "\" + $Dashboard_Repository_Path
$Files = Get-ChildItem -Path $TempPath | %{$_.FullName}
#-------------------------------------------------------------------------------------
# Create the resource group for Azure Workbooks & Dashboards
#-------------------------------------------------------------------------------------
Write-Output ""
Write-Output "Validating Azure resource group exist [ $($WorkbookDashboardResourceGroup) ]"
try {
Get-AzResourceGroup -Name $WorkbookDashboardResourceGroup -ErrorAction Stop
} catch {
Write-Output ""
Write-Output "Creating Azure resource group [ $($WorkbookDashboardResourceGroup) ]"
New-AzResourceGroup -Name $WorkbookDashboardResourceGroup -Location $LoganalyticsLocation
}
#-------------------------------------------------------------------------------------
# Deployment of Dashboards (loop)
#-------------------------------------------------------------------------------------
ForEach ($File in $Files)
{
# read ARM file
$TemplateFileName = $File.Split("\")[-1]
$TemplateName = $TemplateFileName.Split(".")[0]
$DashboardName = ($TemplateName.replace("-","_"))
$DashboardName = ($TemplateName.replace(" ","_"))
Write-Output ""
Write-Output "Deployment of Azure Dashboard [ $($TemplateName) ] in progress .... please wait !"
Write-Output " Adjusting ARM template with LogAnalytics workspace information"
# convert file to JSON
$ArmTemplate = Get-Content $File -Raw -Encoding UTF8
$ArmTemplateJson = $ArmTemplate | ConvertFrom-Json
# tell ARM template location
$ArmTemplateJson | Add-Member -MemberType NoteProperty -Name "location" -Value $LogAnalyticsLocation -Force
$ArrayLenses = ($ArmTemplateJson.properties.lenses | get-member -MemberType NoteProperty).name
ForEach ($Lense in $ArrayLenses)
{
$indexParts = 0
$ArrayParts = ($ArmTemplateJson.properties.lenses.$Lense.parts | get-member -MemberType NoteProperty).name
ForEach ($Part in $ArrayParts)
{
$ArrayData = $ArmTemplateJson.properties.lenses.$Lense.parts.$Part.metadata.inputs.name
$Index = 0
Foreach ($Entry in $ArrayData)
{
If ($Entry -eq "ConfigurationId")
{
$WorkbookTemplateName = $ArmTemplateJson.properties.lenses.$Lense.parts.$Part.metadata.inputs.value[$index].split("/")[-1]
$ReplaceData = "ArmTemplates-/subscriptions/$($LogAnalyticsSubscription)/resourceGroups/$($WorkbookDashboardResourceGroup)/providers/microsoft.insights/workbooktemplates/" + $WorkbookTemplateName
$ArmTemplateJson.properties.lenses.$Lense.parts.$Part.metadata.inputs[$Index].value = $ReplaceData
}
ElseIf ($Entry -eq "StepSettings")
{
$Data = $ArmTemplateJson.properties.lenses.$Lense.parts.$Part.metadata.inputs.value[$index] | ConvertFrom-Json
$Data.crossComponentResources = @("$LogAnalyticsWorkspaceResourceId")
# Converting modified PSCustomObject data to compressed JSON
$Data = $Data | ConvertTo-Json -Depth 50 -Compress
# Updating ARM template object with modified data
$ArmTemplateJson.properties.lenses.$Lense.parts.$Part.metadata.inputs[$index].value = $Data
}
# index is needed to read the value of the entry
$index += 1
}
}
}
$ArmTemplateExport = $ArmTemplateJson | ConvertTo-Json -Depth 50
$TemplateFile = $Env:TEMP + "\" + $TemplateFileName
$ArmTemplateExport | out-file $TemplateFile -Force
Write-Output " Starting deployment of dashboard [ $($TemplateName) ] "
Write-Output ""
# parameters for ARM deployment
New-AzPortalDashboard `
-DashboardPath $TemplateFile `
-ResourceGroupName $WorkbookDashboardResourceGroup `
-DashboardName $DashboardName
}