-
Notifications
You must be signed in to change notification settings - Fork 71
/
AzuriteExplorer.ps1
executable file
·1468 lines (1156 loc) · 81.7 KB
/
AzuriteExplorer.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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
Azurite Explorer file: AzuriteExplorer.ps1
Author: Apostolos Mastoris (@Lgrec0)
License: BSD 3-Clause
Required Dependencies: Azure PowerShell API
Optional Dependencies: None
#>
<#
Main function to retrieve the configuration of all the components in the given Azure subscription.
The function does not accept any parameters.
The function creates a number of JSON files that provide information about the various components in the Azure subscription.
#>
function Review-AzureRmSubscription {
<#
.SYNOPSIS
Main function to retrieve the configuration of all the components in a given Azure subscription specified by the Subscription Id.
The function does not accept any parameters. The output is a number of JSON files that provide
information about the various components in the Azure subscription.
.EXAMPLE
PS C:\> Review-AzureRmSubscription
Main function to retrieve the configuration of all the components in a given Azure subscription specified by the Subscription Id.
#>
# Print script banner and version information.
Write-Host "
█████╗ ███████╗██╗ ██╗██████╗ ██╗████████╗███████╗
██╔══██╗╚══███╔╝██║ ██║██╔══██╗██║╚══██╔══╝██╔════╝
███████║ ███╔╝ ██║ ██║██████╔╝██║ ██║ █████╗
██╔══██║ ███╔╝ ██║ ██║██╔══██╗██║ ██║ ██╔══╝
██║ ██║███████╗╚██████╔╝██║ ██║██║ ██║ ███████╗
╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝
███████╗██╗ ██╗██████╗ ██╗ ██████╗ ██████╗ ███████╗██████╗
██╔════╝╚██╗██╔╝██╔══██╗██║ ██╔═══██╗██╔══██╗██╔════╝██╔══██╗
█████╗ ╚███╔╝ ██████╔╝██║ ██║ ██║██████╔╝█████╗ ██████╔╝
██╔══╝ ██╔██╗ ██╔═══╝ ██║ ██║ ██║██╔══██╗██╔══╝ ██╔══██╗
███████╗██╔╝ ██╗██║ ███████╗╚██████╔╝██║ ██║███████╗██║ ██║
╚══════╝╚═╝ ╚═╝╚═╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
Version: 0.6 Beta
Author: Apostolos Mastoris (@Lgrec0)
Email: apostolis.mastoris[at]mwrinfosecurity.com
"
# Declaration of $ErrorActionPreference in order to be used across the script, when an exception occurs.
# This settings will supress any error messages that appear in the console.
$ErrorActionPreference = 'SilentlyContinue'
# Login to Azure service using Microsoft or organisation credentials.
# This is the secure option and most preferred.
Login-AzureRmAccount
# Ask for an (optional) TenantId
$tenantId = Read-Host "Please provide a Tenant Id to perform the review (blank to leave to default)"
# Pring information for the Azure subscriptions available for the user
Write-Host "Tenant-Id:" $tenantId
if ($tenantID) {
Get-AzureRmSubscription -TenantId $tenantId
} else {
Get-AzureRmSubscription
}
# Request from the user to input the corresponding subscription Id to use during the review.
$subscriptionId = Read-Host "Please provide the Subscription Id of the subscription to perform the review"
# Get the current state of the subscription. This will assist in determining whether it is a good subscription
# to use in the review.
$subscriptionState = Get-AzureRmSubscription -SubscriptionId $subscriptionId | Select-Object -ExpandProperty State
# If the subscription's state is not 'Enabled', then prompt for an enabled subscription.
# If the subscription's state is 'Warned', then ask user's consent as this may limit some of the functionality of the tool.
# More specificaly, if the status is 'Warned', some information may not be possible to be retrieved.
while ($subscriptionState -ne "Enabled") {
if ($subscriptionState -eq "Warned") {
$statusWarnedEnquiry = Read-Host "The subscription is at 'Warned' state and some features will not be fully functional. Do you want to continue? [Y/n]?"
if ($statusWarnedEnquiry -eq 'Y' -or $statusWarnedEnquiry -eq 'y' ) {
break
}
}
$subscriptionId = Read-Host "Please provide the subscription id of an active subscription to perform the review"
$subscriptionState = Get-AzureRmSubscription -SubscriptionId $subscriptionId | Select-Object -ExpandProperty State
}
# Get the current context for the execution of the script.
$context = Get-AzureRmContext
# Get the current role of the user that has logged in.
$currentUserRole = Get-AzureRmRoleAssignment -IncludeClassicAdministrators | Where-Object { $_.DisplayName -eq $context.Account } | Select -ExpandProperty RoleDefinitionName
# Print current user's role.
Write-Host "[*] Current user's role:" $currentUserRole
# Get all the resource groups in the given subscription.
$resourceGroups = Get-AzureRmResourceGroup
# If a subscription Id has been provided successfully, then retrieve the context of the current subscription.
if ($subscriptionId) {
$currentSubscription = Select-AzureRmSubscription -SubscriptionId $subscriptionId -WarningAction SilentlyContinue
$currentSubscription
# Call to retrieve subscription's network configuration.
$subscriptionConfiguration = Get-CustomAzureRmSubscriptionNetworkConfiguration
# Populate the object that will contain all the information for the subscription's configuration including information for various resources.
# This object will become available as JSON.
$objSubscriptionConfigurationProperties = [ordered] @{}
if ($subscriptionConfiguration) {
$objSubscriptionConfigurationProperties.Add('subscriptionVNETs', $subscriptionConfiguration)
}
# Instantiate arrays for each of the resources that will be retrieved from the helper functions.
$vmInstancesInfo = @()
$sqlServersInfo = @()
$webAppsInfo = @()
$localNetworkGatewaysInfo = @()
$keyVaultsInfo = @()
# Foreach resource group that contains resources perform perform the operations of this script iterativelly on each resource group.
foreach ($resourceGroup in $resourceGroups) {
Write-Host "[+] Retrieve components from Resource Group $($resourceGroup.ResourceGroupName) in subscription $($currentSubscription.SubscriptionName) ($subscriptionId):"
# Retrieve the VM instances for a specific resource group.
$vmInstances = Get-AzureRmVM -ResourceGroupName $resourceGroup.ResourceGroupName
# Check if there are any VM instances in the resource group and then retrieve their configuration.
if ($vmInstances) {
foreach ($vmInstance in $vmInstances) {
if ($vmInstance.StorageProfile.OsDisk.OsType -like '*Windows*') {
# Retrieve configuration of Windows VMs.
$objVMInstancesInfo = Get-CustomAzureRmWindowsVM -vmInstanceName $vmInstance.Name -vmInstanceResourceGroupName $vmInstance.ResourceGroupName
} else {
# Retrieve configuration of Linux VMs.
$objVMInstancesInfo = Get-CustomAzureRmLinuxVM -vmInstanceName $vmInstance.Name -vmInstanceResourceGroupName $vmInstance.ResourceGroupName
}
$vmInstancesInfo += $objVMInstancesInfo
}
} else {
Write-Host "[*] No Virtual Machines were found."
}
# Retrieve configuration for the web applications in the resource group.
$webApps = Get-AzureRmWebApp -ResourceGroupName $resourceGroup.ResourceGroupName
# Populate the array with the configuration of the web applications in the resource group.
if ($webApps) {
Write-Host '[+] Retrieve Web Application configuration.'
foreach ($webApp in $webApps) {
$objWebAppsInfo = Get-CustomAzureRmWebApp -webApp $webApp -ResourceGroupName $resourceGroup.ResourceGroupName
$webAppsInfo += $objWebAppsInfo
}
} else {
Write-Host "[*] No Web Applications were found."
}
# Retrieve configuration for the SQL Servers in the resource group.
$sqlServers = Get-AzureRmSqlServer -ResourceGroupName $resourceGroup.ResourceGroupName
# Populate the array with the configuration of the SQL Servers and SQL Databases in the resource group.
if ($sqlServers) {
Write-Host "[+] Retrieve Azure SQL Server and Azure SQL Database configuration."
foreach ($sqlServer in $sqlServers) {
$objSqlServersInfo = Get-CustomAzureRmSqlServer -sqlServer $sqlServer
$sqlServersInfo += $objSqlServersInfo
}
} else {
Write-Host "[*] No Azure SQL Servers were found."
}
# Retrieve configuration for the Local Network Gateways in each resource group.
$localNetworkGateways = Get-AzureRmLocalNetworkGateway -ResourceGroupName $resourceGroup.ResourceGroupName -WarningAction SilentlyContinue
# Populate the array with the configuration of the Local Network Gateways in the resource group.
if ($localNetworkGateways) {
Write-Host '[+] Retrieve Local Network Gateways configuration.'
foreach ($localNetworkGateway in $localNetworkGateways) {
$objLocalNetworkGatewaysInfo = Get-CustomAzureRmGateway -localNetworkGatewayName $localNetworkGateway.Name -gatewayResourceGroupName $resourceGroup.ResourceGroupName
$localNetworkGatewaysInfo += $objLocalNetworkGatewaysInfo
}
} else {
Write-Host "[*] No Local Network Gateways were found."
}
# Retrieve configuration of the Azure Key Vaults in each resource group.
$keyVaults = Get-AzureRmKeyVault -ResourceGroupName $resourceGroup.ResourceGroupName -WarningAction SilentlyContinue
# Populate the array with the configuration of the Azure Key Vaults in each resource group.
if ($keyVaults) {
Write-Host '[+] Retrieve Key Vault configuration.'
foreach ($keyVault in $keyVaults) {
$objKeyVaultsInfo = Get-CustomAzureRmKeyVault -keyVaultInstanceName $keyVault.VaultName -keyVaultResourceGroupName $resourceGroup.ResourceGroupName
$keyVaultsInfo += $objKeyVaultsInfo
}
} else {
Write-Host '[*] No Key Vaults have been configured.'
}
}
# Export configuration of Virtual Machines to JSON and store in a file.
if ($vmInstancesInfo) {
$vmInstancesInfo | ConvertTo-Json -Depth 6 | Out-File $(".\azure-vms_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
<#
$azureVMContent = $vmInstancesInfo | ConvertTo-Json -Depth 6 | Out-File $()
$azureVMFilePath = ".\azure-vms_" + $subscriptionId + "_" + $context.Account + ".json"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($azureVMFilePath, $azureVMContent, $Utf8NoBomEncoding)
#>
}
# Export configuration of SQL Servers and SQL Databases in JSON and store in a file.
if ($sqlServersInfo) {
$sqlServersInfo | ConvertTo-Json -Depth 6 | Out-File $(".\azure-sqlservers_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
<#
$azureSqlServersContent = $sqlServersInfo | ConvertTo-Json -Depth 6
$azureSqlServersFilePath = ".\azure-sqlservers_" + $subscriptionId + "_" + $context.Account + ".json"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($azureSqlServersFilePath, $azureSqlServersContent, $Utf8NoBomEncoding)
#>
$objSubscriptionConfigurationProperties.Add('subscriptionSqlServers', $sqlServersInfo)
}
# Export configuration of all the Web Applications in JSON and store in a file.
if ($webAppsInfo) {
$webAppsInfo | ConvertTo-Json -Depth 3 | Out-File $(".\azure-websites_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
<#
$azureWebAppsContent = $webAppsInfo | ConvertTo-Json -Depth 6
$azureWebAppsFilePath = ".\azure-websites_" + $subscriptionId + "_" + $context.Account + ".json"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($azureWebAppsFilePath, $azureWebAppsContent, $Utf8NoBomEncoding)
#>
$objSubscriptionConfigurationProperties.Add('subscriptionWebApps', $webAppsInfo)
}
# Add the Local Network Gateways resources to the subscription's configuration.
if ($localNetworkGatewaysInfo) {
$objSubscriptionConfigurationProperties.Add('subscriptionLocalNetworkGateways', $localNetworkGatewaysInfo)
}
# Export cofniguration of the Azure Key Vaults in JSON and store in a file.
if ($keyVaultsInfo) {
$keyVaultsInfo | ConvertTo-Json -Depth 3 | Out-File $(".\azure-key-vaults_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
<#
$azureKeyVaultsContent = $keyVaultsInfo | ConvertTo-Json -Depth 6
$azureKeyVaultsFilePath = ".\azure-key-vaults_" + $subscriptionId + "_" + $context.Account + ".json"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($azureKeyVaultsFilePath, $azureKeyVaultsContent, $Utf8NoBomEncoding)
#>
$objSubscriptionConfigurationProperties.Add('subscriptionKeyVaults', $keyVaultsInfo)
}
# Create the object that contains all the information for the subscription configuration.
$objSubscriptionConfiguration = New-Object -TypeName PSObject -Property $objSubscriptionConfigurationProperties
# Export configuration of the Azure subscription in JSON and store in a file.
$objSubscriptionConfiguration | ConvertTo-Json -Depth 10 | Out-File $(".\azure-subscription_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
<#
$azureSubscriptionConfigurationContent = $objSubscriptionConfiguration | ConvertTo-Json -Depth 6
$azureSubscriptionConfigurationFilePath = ".\azure-subscription_" + $subscriptionId + "_" + $context.Account + ".json"
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($azureSubscriptionConfigurationFilePath, $azureSubscriptionConfigurationContent, $Utf8NoBomEncoding)
#>
# finally create a json file with all resources (as not all resource types are handled in a greater level of detail)
Get-AzureRmResource | ConvertTo-Json -Depth 10 | Out-File $(".\azure-cmdb_" + $subscriptionId + "_" + $context.Account + ".json") -Encoding UTF8
# Present information/statistics about the Azure subscription.
Write-Host "[*] Azure Subscription Information - Subscription Id $subscriptionId"
Write-Host " [-] Resource Groups: $($resourceGroups.Count)"
Write-Host " [-] Virtual Networks (VNets): $($objSubscriptionConfiguration.subscriptionVNETs.Length)"
$subscriptionTotalSubnets = 0
foreach ($subscriptionVNET in $objSubscriptionConfiguration.subscriptionVNETs) {
$subscriptionTotalSubnets = $subscriptionTotalSubnets + $subscriptionVNET.vnetSubnets.Length
}
Write-Host " [-] Subnets: $subscriptionTotalSubnets"
Write-Host " [-] Virtual Machines: $($vmInstancesInfo.Length)"
Write-Host " [-] Azure SQL Servers: $($sqlServersInfo.Length)"
$subscriptionTotalSqlDatabases = 0
foreach ($subscriptionSqlServer in $objSubscriptionConfiguration.subscriptionSqlServers) {
$subscriptionTotalSqlDatabases = $subscriptionTotalSqlDatabases + $subscriptionSqlServer.sqlServerDatabases.Length
}
Write-Host " [-] Azure SQL Databases: $subscriptionTotalSqlDatabases"
Write-Host " [-] Azure Web Applications: $($webAppsInfo.Length)"
} else {
Write-Host "[!] Please provide Subscription Id."
}
}
<#
.DESCRIPTION
Helper function to retrieve configuration information for Windows systems.
.PARAMETER vmInstanceName
The Virtual Machine's name.
.PARAMETER vInstanceResourceGroupName
The Resource Group's name.
.OUTPUT
The function returns an object populated with the VM's details.
#>
function Get-CustomAzureRmWindowsVM {
Param (
[String] $vmInstanceName,
[String] $vmInstanceResourceGroupName
)
Write-Host "[+] Retrieve Virtual Machine's $vmInstanceResourceGroupName - $vmInstanceName configuration."
# Get VM object based on the provided parameters.
$vmInstance = Get-AzureRmVM -Name $vmInstanceName -ResourceGroupName $vmInstanceResourceGroupName
# Retrieve the network configuration.
$vmInstanceNetworkConfiguration = Get-CustomAzureRmNetworkConfiguration -vmInstance $vmInstance
# Retrieve the encryption configuration.
$vmInstanceEncryption = Get-CustomAzureRmVMEncryption -vmInstance $vmInstance
# Retrieve the Network Security Groups (NSGs) configuration.
$vmInstanceNetworkSecurityGroups = Get-CustomAzureRmNetworkSecurityGroups -vmInstance $vmInstance
# Retrieve the VM's security extensions.
$vmInstanceSecurityExtensions = Get-CustomAzureRmVMSecurityExtensions -vmInstance $vmInstance
# Populate the object that will be returned from the function with VM's details.
$objVMInstanceInfoProperties = [ordered] @{
vmName = $vmInstance.Name
vmResourceGroupName = $vmInstance.ResourceGroupName
vmLocation = $vmInstance.Location
itemType = 'Virtual Machine'
vmStorageProfile = $vmInstance.StorageProfile
vmNetworkConfiguration = $vmInstanceNetworkConfiguration
vmNetworkSecurityGroups = $vmInstanceNetworkSecurityGroups
vmEncryption = $vmInstanceEncryption
}
# If network security extensions are available, include it in the object.
if ($vmInstanceSecurityExtensions) {
$objVMInstanceInfoProperties.Add('vmSecurityExtensions', $vmInstanceSecurityExtensions)
}
# Create the object containing VM's details.
$objVMInstanceInfo = New-Object -TypeName PSObject -Property $objVMInstanceInfoProperties
# Return the object to the function call.
return $objVMInstanceInfo
}
<#
.DESCRIPTION
Helprer fuction to retrieve configuration information for Linux systems.
.PARAMETER vmInstanceName
The Virtual Machine's name.
.PARAMETER vInstanceResourceGroupName
The Resource Group's name.
.OUTPUT
The function returns an object populated with the VM's details.
#>
function Get-CustomAzureRmLinuxVM {
Param (
[String] $vmInstanceName,
[String] $vmInstanceResourceGroupName
)
Write-Host "[+] Retrieve Virtual Machine's $vmInstanceResourceGroupName - $vmInstanceName configuration."
# Get VM object based on the provided parameters.
$vmInstance = Get-AzureRmVM -Name $vmInstanceName -ResourceGroupName $vmInstanceResourceGroupName
# Retrieve the network configuration.
$vmInstanceNetworkConfiguration = Get-CustomAzureRmNetworkConfiguration -vmInstance $vmInstance
# System's additional configuration
if ($vmInstance.OSProfile.LinuxConfiguration.DisablePasswordAuthentication) { $passwordAuthenticationStatus = 'Disabled' }
else { $passwordAuthenticationStatus = 'Enabled'}
# Retrieve the encryption configuration.
$vmInstanceEncryption = Get-CustomAzureRmVMEncryption -vmInstance $vmInstance
# Retrieve the Network Security Groups (NSGs) configuration.
$vmInstanceNetworkSecurityGroups = Get-CustomAzureRmNetworkSecurityGroups -vmInstance $vmInstance
# Retrieve the VM's security extensions.
$vmInstanceSecurityExtensions = Get-CustomAzureRmVMSecurityExtensions -vmInstance $vmInstance
# Populate the object that will be returned from the function with VM's details.
$objVMInstanceInfoProperties = [ordered] @{
vmName = $vmInstance.Name
vmResourceGroupName = $vmInstance.ResourceGroupName
vmLocation = $vmInstance.Location
vmStorageProfile = $vmInstance.StorageProfile
vmNetworkConfiguration = $vmInstanceNetworkConfiguration
vmNetworkSecurityGroups = $vmInstanceNetworkSecurityGroups
vmEncryption = $vmInstanceEncryption
vmPasswordAuthentication = $passwordAuthenticationStatus
}
# If network security extensions are available, include it in the object.
if ($vmInstanceSecurityExtensions) {
$objVMInstanceInfoProperties.Add('vmSecurityExtensions', $vmInstanceSecurityExtensions)
}
# Create the object containing VM's details.
$objVMInstanceInfo = New-Object -TypeName PSObject -Property $objVMInstanceInfoProperties
# Return the object to the function call.
return $objVMInstanceInfo
}
<#
.DESCRIPTION
Helprer function to retrieve configuration for Azure SQL Server systems.
.PARAMETER sqlServer
The Azure SQL Server instance which has been retrieved from a previous operation.
.OUTPUT
The function returns an object populated with the Azure SQL Server's details.
#>
function Get-CustomAzureRmSqlServer {
Param ([System.Object] $sqlServer)
# Populate the object to return the Azure SQL Server's details.
$objSqlServerInfoProperties = [ordered] @{
sqlServerName = $sqlServer.ServerName
sqlServerResourceGroupName = $sqlServer.ResourceGroupName
sqlServerLocation = $sqlServer.Location
sqlServerAdministratorLogin = $sqlServer.SqlAdministratorLogin
sqlServerVersion = $sqlServer.ServerVersion
}
# Retrieve the Azure Active Directory administrator that has been granted access to the Azure SQL Server.
# If a user has been configured, pupulate the object.
$sqlServerADAdministrator = Get-AzureRmSqlServerActiveDirectoryAdministrator -ServerName $sqlServer.ServerName -ResourceGroupName $sqlServer.ResourceGroupName
if ($sqlServerADAdministrator) {
$objSqlServerInfoProperties.Add('sqlServerADAdministrator', $sqlServerADAdministrator)
}
# Retrieve the Azure SQL Server's auditing policy.
$sqlServerAuditingPolicy = Get-AzureRmSqlServerAuditingPolicy -ServerName $sqlServer.ServerName -ResourceGroupName $sqlServer.ResourceGroupName
$objSqlServerInfoProperties.Add('sqlServerAuditingPolicy', $sqlServerAuditingPolicy)
# Retrieve the Azure SQL Server's communication links.
$sqlServerCommunicationLinks = Get-AzureRmSqlServerCommunicationLink -ResourceGroupName $sqlServer.ResourceGroupName -ServerName $sqlServer.ServerName
# In case that there are Azure SQL Server communication links, populate the object which each one of them recursively.
if ($sqlServerCommunicationLinks) {
$sqlServerCommunicationLinkInfo = @()
$index = 1
foreach ($sqlServerCommunicationLink in $sqlServerCommunicationLinks) {
$sqlServerCommunicationLinkInfo += $sqlServerCommunicationLink
}
$objSqlServerInfoProperties.Add('sqlServerCommunicationLinks', $sqlServerCommunicationLinkInfo)
}
# Retrieve the Azure SQL Server's firewall rules.
$sqlServerFirewallRules = Get-AzureRmSqlServerFirewallRule -ResourceGroupName $sqlServer.ResourceGroupName -ServerName $sqlServer.ServerName
$sqlServerFirewallRuleInfo = @()
$index = 1
foreach ($sqlServerFirewallRule in $sqlServerFirewallRules) {
$sqlServerFirewallRuleInfo += $sqlServerFirewallRule
}
$objSqlServerInfoProperties.Add('sqlServerFirewallRules', $sqlServerFirewallRuleInfo)
# Retrieve the Azure SQL Databases that are hosted on the current Azure SQL Server.
$sqlServerDatabases = Get-AzureRmSqlDatabase -ResourceGroupName $sqlServer.ResourceGroupName -ServerName $sqlServer.ServerName
# In case there are any Azure SQL Databases hosted on the current Azure SQL Server, retrieve the details for each of the Azure SQL Database, recursively.
if ($sqlServerDatabases) {
$sqlServerDatabaseInfo = @()
foreach ($sqlServerDatabase in $sqlServerDatabases) {
# Retrieve the configuration details for an Azure SQL Database.
$objSqlServerDatabaseInfo = Get-CustomAzureRmSqlDatabase -sqlDatabase $sqlServerDatabase
if ($objSqlServerDatabaseInfo) {
$sqlServerDatabaseInfo += $objSqlServerDatabaseInfo
}
}
$objSqlServerInfoProperties.Add('sqlServerDatabases', $sqlServerDatabaseInfo)
}
# Create the object containing Azure SQL Server's details.
$objSqlServerInfo = New-Object -TypeName PSObject -Property $objSqlServerInfoProperties
# Return the object to the function call.
return $objSqlServerInfo
}
<#
.DESCRIPTION
Helprer function to retrieve the details for the Azure SQL Databases.
.PARAMETER $sqlDatabase
The Azure SQL Database instance that has been retrieved during a previous operation.
.OUTPUT
The function returns an object populated with the Azure SQL Database's details.
#>
function Get-CustomAzureRmSqlDatabase {
Param ([System.Object] $sqlDatabase)
# We are not interested in the configuration of the master DB (default Azure Database).
# Retrieve the Azure SQL Database's details if the database name is not 'master'.
if ($sqlDatabase.DatabaseName -ne 'master') {
# Populate the object to return with Azure SQL Server's details.
$objSqlDatabaseInfoProperties = [ordered] @{
sqlDatabaseName = $sqlDatabase.DatabaseName
sqlDatabaseServerName = $sqlDatabase.ServerName
sqlDatabaseResourceGroupName = $sqlDatabase.ResourceGroupName
sqlDatabaseLocation = $sqlDatabase.Location
sqlDatabaseEdition = $sqlDatabase.Edition
sqlDatabaseStatus = $sqlDatabase.Status
}
# Retrieve the Azure SQL Database's auditing policy.
$sqlDatabaseAuditingPolicy = Get-AzureRmSqlDatabaseAuditingPolicy -ResourceGroupName $sqlDatabase.ResourceGroupName -ServerName $sqlDatabase.ServerName -DatabaseName $sqlDatabase.DatabaseName
if ($sqlDatabaseAuditingPolicy) {
$objSqlDatabaseInfoProperties.Add('sqlDatabaseAuditingPolicy', $sqlDatabaseAuditingPolicy)
}
# Retrieve the Azure SQL Database's data masking policy.
$sqlDatabaseDataMaskingPolicy = Get-AzureRmSqlDatabaseDataMaskingPolicy -ResourceGroupName $sqlDatabase.ResourceGroupName -ServerName $sqlDatabase.ServerName -DatabaseName $sqlDatabase.DatabaseName
if ($sqlDatabaseDataMaskingPolicy) {
# Initiliase the object that returns the data masking policy.
$objSqlDatabaseDataMaskingPolicy = New-Object PSObject
if ($sqlDatabaseDataMaskingPolicy.DataMaskingState -eq 2) {
$objSqlDatabaseDataMaskingPolicy | Add-Member NoteProperty 'DataMaskingState' 'Disabled'
}
else {
$objSqlDatabaseDataMaskingPolicy | Add-Member NoteProperty 'DataMaskingState' $sqlDatabaseDataMaskingPolicy.DataMaskingState
}
# Retrieve the users that are able to view the data without masking.
$objSqlDatabaseDataMaskingPolicy | Add-Member NoteProperty 'PrivilegedUsers' $sqlDatabaseDataMaskingPolicy.PrivilegedUsers
$objSqlDatabaseInfoProperties.Add('sqlDatabaseDataMaskingPolicy', $objSqlDatabaseDataMaskingPolicy)
}
# Retrieve the Azure SQL Database's connection policy details (connection strings) for various technologies.
$sqlDatabaseSecureConnectionPolicy = Get-AzureRmSqlDatabaseSecureConnectionPolicy -ResourceGroupName $sqlDatabase.ResourceGroupName -ServerName $sqlDatabase.ServerName -DatabaseName $sqlDatabase.DatabaseName
if ($sqlDatabaseSecureConnectionPolicy) {
$objSqlDatabaseSecureConnectionPolicy = New-Object PSObject
$objSqlDatabaseSecureConnectionPolicy | Add-Member NoteProperty 'ProxyDnsName' $sqlDatabaseSecureConnectionPolicy.ProxyDnsName
$objSqlDatabaseSecureConnectionPolicy | Add-Member NoteProperty 'ProxyPort' $sqlDatabaseSecureConnectionPolicy.ProxyPort
$objSqlDatabaseSecureConnectionPolicyConnectionStrings = New-Object PSObject
$objSqlDatabaseSecureConnectionPolicyConnectionStrings | Add-Member NoteProperty 'AdoNetConnectionString' $sqlDatabaseSecureConnectionPolicy.ConnectionStrings.AdoNetConnectionString
$objSqlDatabaseSecureConnectionPolicyConnectionStrings | Add-Member NoteProperty 'JdbcConnectionString' $sqlDatabaseSecureConnectionPolicy.ConnectionStrings.JdbcConnectionString
# Currently having problems in converting to JSON.
# $objSqlDatabaseSecureConnectionPolicyConnectionStrings | Add-Member NoteProperty 'PhpConnectionString' $sqlDatabaseSecureConnectionPolicy.ConnectionStrings.PhpConnectionString
$objSqlDatabaseSecureConnectionPolicyConnectionStrings | Add-Member NoteProperty 'OdbcConnectionString' $sqlDatabaseSecureConnectionPolicy.ConnectionStrings.OdbcConnectionString
$objSqlDatabaseSecureConnectionPolicy | Add-Member NoteProperty 'ConnectionStrings' $objSqlDatabaseSecureConnectionPolicyConnectionStrings
# Check whether secure connection policy is enforced.
if ( $sqlDatabaseSecureConnectionPolicy.SecureConnectionState -eq 1 ) {
$objSqlDatabaseSecureConnectionPolicy | Add-Member NoteProperty 'SecureConnectionState' 'Optional'
}
else {
$objSqlDatabaseSecureConnectionPolicy | Add-Member NoteProperty 'SecureConnectionState' $sqlDatabaseSecureConnectionPolicy.SecureConnectionState
}
$objSqlDatabaseInfoProperties.Add('sqlDatabaseSecureConnectionPolicy', $objSqlDatabaseSecureConnectionPolicy)
}
# Retrieve the Azure SQL Database's threat detection policy details.
$sqlDatabaseThreatDetectionPolicy = Get-AzureRmSqlDatabaseThreatDetectionPolicy -ResourceGroupName $sqlDatabase.ResourceGroupName -ServerName $sqlDatabase.ServerName -DatabaseName $sqlDatabase.DatabaseName
if ($sqlDatabaseThreatDetectionPolicy) {
$objsqlDatabaseThreadDetectionPolicy = New-Object PSObject
if ($sqlDatabaseThreatDetectionPolicy.ThreatDetectionState -eq 2) {
$objsqlDatabaseThreadDetectionPolicy | Add-Member NoteProperty 'ThreatDetectionState' 'New'
}
else {
$objsqlDatabaseThreadDetectionPolicy | Add-Member NoteProperty 'ThreatDetectionState' $sqlDatabaseThreatDetectionPolicy.ThreatDetectionState
}
$objsqlDatabaseThreadDetectionPolicy | Add-Member NoteProperty 'NotificationRecipientsEmails' $sqlDatabaseThreatDetectionPolicy.NotificationRecipientsEmails
$objsqlDatabaseThreadDetectionPolicy | Add-Member NoteProperty 'EmailAdmins' $sqlDatabaseThreatDetectionPolicy.EmailAdmins
$objsqlDatabaseThreadDetectionPolicy | Add-Member NoteProperty 'ExcludedDetectionTypes' $sqlDatabaseThreatDetectionPolicy.ExcludedDetectionTypes
$objSqlDatabaseInfoProperties.Add('sqlDatabaseThreatDetectionPolicy', $objSqlDatabaseThreatDetectionPolicy)
}
# Retrieve the configuration for the Azure SQL Database's Transparent Data Encryption (TDE).
$sqlDatabaseTransparentDataEncryption = Get-AzureRmSqlDatabaseTransparentDataEncryption -ResourceGroupName $sqlDatabase.ResourceGroupName -ServerName $sqlDatabase.ServerName -DatabaseName $sqlDatabase.DatabaseName
if ( $sqlDatabaseTransparentDataEncryption.State -eq 1 ) {
$objSqlDatabaseInfoProperties.Add('sqlDatabaseTransparentDataEncryption', 'Disabled')
}
else {
$objSqlDatabaseInfoProperties.Add('sqlDatabaseTransparentDataEncryption','Enabled')
}
# Create the object containing Azure SQL Database's details.
$objSqlDatabaseInfo = New-Object -TypeName PSObject -Property $objSqlDatabaseInfoProperties
# Return the object to the function call.
return $objSqlDatabaseInfo
} else {
return $null
}
}
<#
.DESCRIPTION
Helprer function to retrieve the network configuration for VM's and VNET Gateways.
.PARAMETER $vmInstance
The Virtual Machine's instance.
.PARAMATER $gatewayInstance
The VNET Gateway's instance.
.OUTPUT
The function returns an object which contains the network configuration of either a VM or a VNET Gateway.
#>
function Get-CustomAzureRmNetworkConfiguration {
Param (
[Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine] $vmInstance,
[Microsoft.Azure.Commands.Network.Models.PSVirtualNetworkGateway] $gatewayInstance
)
# Check if the cmdlet was called with input a VM instance.
if ($vmInstance) {
# Get the network interface IDs of the VM.
$vmNetworkInterfaceIds = $vmInstance.NetworkInterfaceIDs
$vmNetworkInterfaceInfo = @()
# For each VM network interface retrieve the configuration and store it in an object.
foreach ($vmNetworkInterfaceId in $vmNetworkInterfaceIds) {
# Get the name of the network interface from the network interface Id.
# The format of a network interface Id is:
#/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroup>/providers/Microsoft.Network/networkInterfaces/windows311400
$vmNetworkInterfaceName = $vmNetworkInterfaceId | Split-Path -Leaf
# Retrieve network inteface details.
$vmNetworkInterfaceConfig = Get-AzureRmNetworkInterface -Name $vmNetworkInterfaceName -ResourceGroupName $vmInstance.ResourceGroupName
$vmNetworkInterfaceIpConfigurationInfo = @()
# Each VM can have multiple interfaces (IpConfigurations)
# Create an array of all the IpConfigurations.
foreach ($ipConfiguration in $vmNetworkInterfaceConfig.IpConfigurations) {
$objVMNetworkInterfaceIpConfigurationInfo = New-Object PSObject
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationName' $ipConfiguration.Name
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationPrivateIpAddress' $ipConfiguration.PrivateIpAddress
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationPrivateIpAddressAllocationMethod' $ipConfiguration.PrivateIpAllocationMethod
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationMacAddress' $ipConfiguration.MacAddress
# The VM references the public IP address interface separately.
# Get the name of the public IP address interface, if available.
if ($ipConfiguration.PublicIpAddress) {
$vmNetworkInterfaceIpConfigurationPublicIpInterfaceName = ($ipConfiguration.PublicIpAddress.Id) | Split-Path -Leaf
# Get the VM IpConfiguration's public IP address and populate the object.
$objVMNetworkInterfaceIpConfigurationPublicIp = Get-AzureRmPublicIpAddress -Name $vmNetworkInterfaceIpConfigurationPublicIpInterfaceName -ResourceGroupName $vmInstance.ResourceGroupName
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationPublicIpAddress' $objVMNetworkInterfaceIpConfigurationPublicIp.IpAddress
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationPublicIpAddressAllocationMethod' $objVMNetworkInterfaceIpConfigurationPublicIp.PublicIpAllocationMethod
}
# Get the Virtual Network (VNet) name from the Subnet UNC
$vmVNETName = ($ipConfiguration.Subnet.Id).Split('/')[-3]
$vmVNETSubnetName = ($ipConfiguration.Subnet.Id | Split-Path -Leaf)
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationVNETName' $vmVNETName
$objVMNetworkInterfaceIpConfigurationInfo | Add-Member 'vmNetworkConfigurationSubnetName' $vmVNETSubnetName
# TODO: Routing configuration.
$vmNetworkInterfaceIpConfigurationInfo += $objVMNetworkInterfaceIpConfigurationInfo
}
$objVMNetworkInterfaceInfo = New-Object PSObject
$objVMNetworkInterfaceInfo | Add-Member 'vmNetworkConfigurationIpConfigurations' $vmNetworkInterfaceIpConfigurationInfo
$objVMNetworkInterfaceInfo | Add-Member 'vmNetworkConfigurationDNSSettings' $vmNetworkInterfaceConfig.DnsSettingsText
$objVMNetworkInterfaceInfo | Add-Member 'vmNetworkConfigurationIpForwarding' $vmNetworkInterfaceConfig.EnableIPForwarding
$vmNetworkInterfaceInfo += $objVMNetworkInterfaceInfo
}
return $vmNetworkInterfaceInfo
} elseif ($virtualNetworkGatewayInstance) {
# Each VNet ateway can have multiple interfaces (IpConfigurations).
# Retrieve the information for each VNet Gateway.
$virtualNetworkGatewayIpConfigurationInfo = @()
foreach ($virtualNetworkGatewayIpConfiguration in $virtualNetworkGatewayInstance.IpConfigurations) {
$objVirtualNetworkGatewayIpConfigurationInfo = New-Object PSObject
$objVirtualNetworkGatewayIpConfigurationInfo | Add-Member NoteProperty 'virtualNetworkGatewayPrivateIpAddress' $virtualNetworkGatewayIpConfiguration.PrivateIpAddress
$objVirtualNetworkGatewayIpConfigurationInfo | Add-Member NoteProperty 'virtualNetworkGatewayPrivateIpAddressAllocationMethod' $virtualNetworkGatewayIpConfiguration.PrivateIpAllocationMethod
# $objVirtualNetworkGatewayIpConfigurationInfo | Add-Member NoteProperty 'virtualNetworkGatewayMacAddress' $virtualNetworkGatewayIpConfiguration.MacAddress
# The VNet Gateway references the public IP address interface separately.
# Get the name of public IP address interface, if available.
if ($virtualNetworkGatewayIpConfiguration.PublicIpAddress) {
$virtualNetworkGatewayIpConfigurationPublicIpInterfaceName = ($virtualNetworkGatewayIpConfiguration.PublicIpAddress.Id) | Split-Path -Leaf
# Get VNet Gateway Ip Configuration's public IP address.
$virtualNetworkGatewayIpConfigurationPublicIpAddress = Get-AzureRmPublicIpAddress -Name $virtualNetworkGatewayIpConfigurationPublicIpInterfaceName -ResourceGroupName $virtualNetworkGatewayInstance.ResourceGroupName
$objVirtualNetworkGatewayIpConfigurationInfo | Add-Member 'virtualNetworkGatewayPublicIpAddress' $virtualNetworkGatewayIpConfigurationPublicIpAddress.IpAddress
$objVirtualNetworkGatewayIpConfigurationInfo | Add-Member 'virtualNetworkGatewayPublicIpAddressAllocationMethod' $virtualNetworkGatewayIpConfigurationPublicIpAddress.PublicIpAllocationMethod
}
$virtualNetworkGatewayIpConfigurationInfo += $objVirtualNetworkGatewayIpConfigurationInfo
}
return $virtualNetworkGatewayIpConfigurationInfo
}
}
<#
.DESCRIPTION
Helprer function to retrieve the encryption status and configuration for the VMs.
.PARAMETER $vmInstance
The Virtual Machine's instance.
.OUTPUT
The function returns an object which contains the encryption status and configuration of a VM.
#>
function Get-CustomAzureRmVMEncryption {
Param ([Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine] $vmInstance)
# Retrieve the encryption status for the VM.
$vmEncryptionStatus = Get-AzureRmVMDiskEncryptionStatus -ResourceGroupName $vmInstance.ResourceGroupName -VMName $vmInstance.Name
# Initialiase the object that will contain theinformation for the VM's encryption configuration.
$objVMDiskEncryption = New-Object PSObject
# Check whether the 'Data' volume is encrypted.
# Status '1': NotEncrypted
if (($vmEncryptionStatus.DataVolumesEncrypted -eq '1') -or ($vmEncryptionStatus.DataVolumesEncrypted -eq '2')) {
# $objVMDiskEncryption | Add-Member NoteProperty 'dataVolumesEncryption' $vmEncryptionStatus.DataVolumesEncrypted
$objVMDiskEncryption | Add-Member NoteProperty 'dataVolumesEncryption' 'Disabled'
} else {
$objVMDiskEncryption | Add-Member NoteProperty 'dataVolumesEncryption' 'Enabled'
}
# Check whether the 'Os' volume is encrypted.
# Status '1': NotEncrypted
# Status '2': Unknown
if (($vmEncryptionStatus.OsVolumeEncrypted -eq '1') -or ($vmEncryptionStatus.OsVolumeEncrypted -eq '2')) {
# $objVMDiskEncryption | Add-Member NoteProperty 'osVolumeEncryption' $vmEncryptionStatus.OsVolumeEncrypted
$objVMDiskEncryption | Add-Member NoteProperty 'osVolumeEncryption' 'Disabled'
} else {
# $objVMDiskEncryption | Add-Member NoteProperty 'osVolumeEncryption' $vmEncryptionStatus.OsVolumeEncrypted
$objVMDiskEncryption | Add-Member NoteProperty 'osVolumeEncryption' 'Enabled'
$objVMDiskEncryption | Add-Member NoteProperty 'osVolumeEncryptionInfo' $vmEncryptionStatus.OsVolumeEncryptionSettings
}
return $objVMDiskEncryption
}
<#
.DESCRIPTION
Helprer function to retrieve the security extensions that have been deployed and configuratio in a VM.
.PARAMETER $vmInstance
The Virtual Machine's instance.
.OUTPUT
The function returns an object which contains the details of the extensions that have been deployed in a VM.
#>
function Get-CustomAzureRmVMSecurityExtensions {
Param ([Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine] $vmInstance)
$vmExtensionInfo = @()
# For some reason this function does not work if I don't retrieve the VM object.
$objVMInstance = Get-AzureRmVM -ResourceGroupName $vmInstance.ResourceGroupName -Name $vmInstance.Name
# Retrieve the information for all the extensions that is believed to be security-related.
foreach ($vmExtension in $objVMInstance.Extensions) {
if ($vmExtension.Publisher -like '*security*') {
$objVMExtensionInfo = New-Object PSObject
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionPublisher' $vmExtension.Publisher
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionType' $vmExtension.VirtualMachineExtensionType
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionName' $vmExtension.Name
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionVersion' $vmExtension.TypeHandlerVersion
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionProvisioningState' $vmExtension.ProvisioningState
# The value of this property is a String.
$objVMExtensionInfo | Add-Member NoteProperty 'vmExtensionSettings' $vmExtension.Settings.ToString()
$vmExtensionInfo += $objVMExtensionInfo
}
}
# Return the object containing the details of the security extensions of a VM.
return $vmExtensionInfo
}
<#
.DESCRIPTION
Helprer function to retrieve the Network Security Groups (NSGs) for Virtual Machines and Subnets.
.PARAMETER $vmInstance
The Virtual Machine's instance.
.PARAMETER $subnet
The subnet's instance.
.OUTPUT
The function returns an object which contains the Network Security Groups (NSGs) configuration for Virtual Machines and Subnets.
#>
function Get-CustomAzureRmNetworkSecurityGroups {
Param (
[Microsoft.Azure.Commands.Compute.Models.PSVirtualMachine] $vmInstance,
[Microsoft.Azure.Commands.Network.Models.PSSubnet] $subnet
)
# Check if the cmdlet was called with input a VM instance.
if ($vmInstance) {
# Get the network interface IDs
$vmNetworkInterfaceIds = $vmInstance.NetworkInterfaceIDs
$vmNetworkInterfaceNetworkSecurityGroups = @()
# The NSGs are associated with the VM's network interface (NIC).
# Retrieve information about each VM's network interface.
foreach ($vmNetworkInterfaceId in $vmNetworkInterfaceIds) {
# Get the name of the network interface from the UNC.
$vmNetworkInterfaceName = $vmNetworkInterfaceId | Split-Path -Leaf
# Collect the configuration of the associated VM's network interface.
$vmNetworkInterfaceConfig = Get-AzureRmNetworkInterface -Name $vmNetworkInterfaceName -ResourceGroupName $vmInstance.ResourceGroupName
# The network interface is associated with a NSG if there's the NSG's Id in the NIC's configuration.
# Check if there's is an NSG associated and retrieve the configuration.
if ($vmNICNetworkSecurityGroupName = $vmNetworkInterfaceConfig.NetworkSecurityGroup.Id) {
# Get the network interface NSG's name
$vmNICNetworkSecurityGroupName = $vmNICNetworkSecurityGroupName | Split-Path -Leaf
# Collect the coonfiguration information for the NSG.
$vmNICNetworkSecurityGroup = Get-AzureRmNetworkSecurityGroup -Name $vmNICNetworkSecurityGroupName -ResourceGroupName $vmInstance.ResourceGroupName
# Create the object containing the Virtual Machine's Network Security Group details.
$objVMNetworkInterfaceNetworkSecurityGroups = New-Object PSObject
$objVMNetworkInterfaceNetworkSecurityGroups | Add-Member NoteProperty 'vmNICNetworkSecurityGroupName' $vmNICNetworkSecurityGroup.Name
$objVMNetworkInterfaceNetworkSecurityGroups | Add-Member NoteProperty 'vmNICNetworkSecurityGroupCustomRules' $vmNICNetworkSecurityGroup.SecurityRules
$objVMNetworkInterfaceNetworkSecurityGroups | Add-Member NoteProperty 'vmNICNetworkSecurityGroupDefaultRules' $vmNICNetworkSecurityGroup.DefaultSecurityRules
$vmNetworkInterfaceNetworkSecurityGroups += $objVMNetworkInterfaceNetworkSecurityGroups
}
}
# Return the object for the configuration of the VM's NSGs.
return $vmNetworkInterfaceNetworkSecurityGroups
# Check if the cmdlet was called with input a Subnet instance.
} elseif ($subnet) {
# Check if the Subnet has a NSG associated with it.
if ($subnet.NetworkSecurityGroup.Id) {
# Get Subnet's NSG's name.
$subnetNetworkSecurityGroupName = $subnet.NetworkSecurityGroup.Id | Split-Path -Leaf
# Get Subnet NSG Resource Group's name
$subnetNetworkSecurityGroupResourceGroupName = ($subnet.NetworkSecurityGroup.Id).Split('/')[4]
# Use the information gathered to retrieve the NSG configuration for the Subnet.
$subnetNetworkSecurityGroup = Get-AzureRmNetworkSecurityGroup -Name $subnetNetworkSecurityGroupName -ResourceGroupName $subnetNetworkSecurityGroupResourceGroupName
# Create the object containing the Subnet's Network Security Group details.
$subnetNetworkSecurityGroups = New-Object PSObject
$subnetNetworkSecurityGroups | Add-Member NoteProperty 'subnetNetworkSecurityGroupName' $subnetNetworkSecurityGroup.Name
$subnetNetworkSecurityGroups | Add-Member NoteProperty 'subnetNetworkSecurityGroupCustomRules' $subnetNetworkSecurityGroup.SecurityRules
$subnetNetworkSecurityGroups | Add-Member NoteProperty 'subnetNetworkSecurityGroupDefaultRules' $subnetNetworkSecurityGroup.DefaultSecurityRules
}
# Return the object for the configuration of the Subnet's NSGs.
return $subnetNetworkSecurityGroups
}
}
<#
.DESCRIPTION
Helprer function to retrieve the routing configuration (Route Tables) for Subnets.
.PARAMETER $subnet
The subnet's instance.
.OUTPUT
The function returns an object which contains the route table configuration for the Subnets.
.NOTES
Under development.
#>
function Get-CustomAzureRmRouteTable {
Param (
[Microsoft.Azure.Commands.Network.Models.PSSubnet] $subnet
)
# TODO: Check if you can have more route tables associated with the same subnet.
if ($subnet.RouteTable.Id) {
# Get subnet's route table.
$subnetRouteTable = Get-AzureRmRouteTable -Id $subnet.RouteTable.Id
# TODO: Otherwise, get subnet's route table (workaround as Id is not an accepted parameter.)
$subnetRouteTableName = $subnet.RouteTable.Id | Split-Path -Leaf
$subnetRouteTableNameResourceGroupName = ($subnet.RouteTable.Id).Split('/')[4]
$subnetRouteTable = Get-AzureRmRouteTable -Name $subnetRouteTableName -ResourceGroupName $subnetRouteTableResourceGroupName
# TODO: Populate the object to contain the details about the subnet.
$objSubnetRouteTableInfoProperties = [ordered] @{
subnetRouteTableName = $subnetRouteTableName
subnetRouteTableConfig = $subnetRouteTable.Routes
}
# Create the object containing the Subnet's Route Table details.
$objSubnetRouteTableInfo = New-Object -TypeName PSObject -Property $objSubnetRouteTableInfoProperties
}
}
<#
.DESCRIPTION
Helprer function to retrieve details and configuration for Azure gateways (VNet and Local Network gateways). The method is called from the
Get-CustomAzureRmSubscriptionNetworkConfiguration helper function, to retrieve information about each Azure Gateway when one is discovered.
.PARAMETER $virtualNetworkGatewayName
The Virtual Network (VNet) Gateway's name.
.PARAMETER $localNetworkGatewayName
The Local Network Gateway's name.
.PARAMETER $gatewayResourceGroupName
The gateway's Resource Group name.
.OUTPUT
The function returns an object which contains information about the configuration and connections for either the VNet Gateway or the Local Network Gateway.
#>
function Get-CustomAzureRmGateway {
Param (
[String] $virtualNetworkGatewayName,
[String] $localNetworkGatewayName,
[String] $gatewayResourceGroupName
)
# Check if the cmdlet was called with input a VNet Gateway's name.
if ($virtualNetworkGatewayName) {
# Retrieve the instance for the given VNet Gateway.
$virtualNetworkGatewayInstance = Get-AzureRmVirtualNetworkGateway -Name $virtualNetworkGatewayName -ResourceGroupName $gatewayResourceGroupName
# Retrieve the gateway's network configuration.
# The call is made to the custom cmdlet defined in this script.
$virtualNetworkGatewayInstanceNetworkConfiguration = Get-CustomAzureRmNetworkConfiguration -gatewayInstance $virtualNetworkGatewayInstance
# Retrieve the VNet Gateway connections for the resource group that the current VNet gateway belongs to.
$virtualNetworkGatewayConnections = Get-AzureRmVirtualNetworkGatewayConnection -ResourceGroupName $gatewayResourceGroupName
# Check whether there are any connections returned.
if ($virtualNetworkGatewayConnections) {
# Collect information about each Virtual Network Gateway connection with other VNET Gateways or Local Network Gateways.