Repository navigation
Expand file tree
/
Copy pathResourceInventory.ps1
More file actions
858 lines (685 loc) · 34.3 KB
/
Copy pathResourceInventory.ps1
File metadata and controls
858 lines (685 loc) · 34.3 KB
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
param ($TenantID,
$Appid,
$SubscriptionID,
$Secret,
$ResourceGroup,
[switch]$Debug,
[switch]$SkipMetrics,
[switch]$SkipConsumption,
[switch]$DeviceLogin,
[switch]$EnableLogs,
$ConcurrencyLimit = 6,
$ReportName = 'ResourcesReport',
$OutputDirectory)
if ($Debug.IsPresent) {$DebugPreference = 'Continue'}
if ($Debug.IsPresent) {$ErrorActionPreference = "Continue" }Else {$ErrorActionPreference = "silentlycontinue" }
Write-Debug ('Debbuging Mode: On. ErrorActionPreference was set to "Continue", every error will be presented.')
Function Write-Log([string]$Message, [string]$Severity)
{
$DateTime = "[{0:dd-MM-yyyy} {0:HH:mm:ss}]" -f (Get-Date)
if($EnableLogs.IsPresent)
{
$Global:Logging.Logs.Add([PSCustomObject]@{ Date = $DateTime; Message = $Message; Severity = $Severity })
}
switch ($Severity)
{
"Info" { Write-Host $Message -ForegroundColor Cyan }
"Warning" { Write-Host $Message -ForegroundColor Yellow }
"Error" { Write-Host $Message -ForegroundColor Red }
"Success" { Write-Host $Message -ForegroundColor Green }
default { Write-Host $Message }
}
}
function GetLocalVersion()
{
$versionJsonPath = "./Version.json"
if (Test-Path $versionJsonPath)
{
$localVersionJson = Get-Content $versionJsonPath | ConvertFrom-Json
return ('{0}.{1}.{2}' -f $localVersionJson.MajorVersion, $localVersionJson.MinorVersion, $localVersionJson.BuildVersion)
}
else
{
Write-Host "Local Version.json not found. Clone the repo and execute the script from the root. Exiting." -ForegroundColor Red
Exit
}
}
function Variables
{
$Global:ResourceContainers = @()
$Global:Resources = @()
$Global:Subscriptions = ''
$Global:ReportName = $ReportName
$Global:Version = GetLocalVersion
$Global:Logging = New-Object PSObject
$Global:Logging | Add-Member -MemberType NoteProperty -Name Logs -Value NotSet
$Global:Logging.Logs = [System.Collections.Generic.List[object]]::new()
$Global:RawRepo = 'https://raw.githubusercontent.com/DNXLabs/azure-resource-discovery/main'
$Global:TableStyle = "Medium15"
}
Function RunInventorySetup()
{
function CheckVersion()
{
Write-Log -Message ('Checking Version') -Severity 'Info'
Write-Log -Message ('Version: {0}' -f $Global:Version) -Severity 'Info'
$versionJson = (New-Object System.Net.WebClient).DownloadString($RawRepo + '/Version.json') | ConvertFrom-Json
$versionNumber = ('{0}.{1}.{2}' -f $versionJson.MajorVersion, $versionJson.MinorVersion, $versionJson.BuildVersion)
if($versionNumber -ne $Global:Version)
{
Write-Log -Message ('New Version Available: {0}.{1}.{2}' -f $versionJson.MajorVersion, $versionJson.MinorVersion, $versionJson.BuildVersion) -Severity 'Warning'
Write-Log -Message ('Download or Clone the latest version and run again: https://github.com/DNXLabs/azure-resource-discovery') -Severity 'Error'
Exit
}
}
function CheckCliRequirements()
{
Write-Log -Message ('Verifying Azure CLI is installed...') -Severity 'Info'
$azCliVersion = az --version
Write-Log -Message ('CLI Version: {0}' -f $azCliVersion[0]) -Severity 'Success'
if ($null -eq $azCliVersion)
{
Read-Host "Azure CLI Not Found. Please install to and run the script again, press <Enter> to exit." -ForegroundColor Red
Exit
}
Write-Log -Message ('Verifying Azure CLI Extension...') -Severity 'Info'
$azCliExtension = az extension list --output json | ConvertFrom-Json
$azCliExtension = $azCliExtension | Where-Object {$_.name -eq 'resource-graph'}
Write-Log -Message ('Current Resource-Graph Extension Version: {0}' -f $azCliExtension.Version) -Severity 'Success'
$azCliExtensionVersion = $azcliExt | Where-Object {$_.name -eq 'resource-graph'}
if (!$azCliExtensionVersion)
{
Write-Log -Message ('Azure CLI Extension not found') -Severity 'Warning'
Write-Log -Message ('Installing Azure CLI Extension...') -Severity 'Info'
az extension add --name resource-graph
}
Write-Log -Message ('Checking Azure PowerShell Module...') -Severity 'Info'
$VarAzPs = Get-InstalledModule -Name Az -ErrorAction silentlycontinue
Write-Log -Message ('Azure PowerShell Module Version: {0}.{1}.{2}' -f [string]$VarAzPs.Version.Major, [string]$VarAzPs.Version.Minor, [string]$VarAzPs.Version.Build) -Severity 'Success'
IF($null -eq $VarAzPs)
{
Write-Log -Message ('Trying to install Azure PowerShell Module...') -Severity 'Warning'
Install-Module -Name Az -Repository PSGallery -Force
}
$VarAzPs = Get-InstalledModule -Name Az -ErrorAction silentlycontinue
if ($null -eq $VarAzPs)
{
Write-Log -Message ('Admininstrator rights required to install Azure PowerShell Module. Press <Enter> to finish script') -Severity 'Error'
Read-Host ''
Exit
}
Write-Log -Message ('Checking ImportExcel Module...') -Severity 'Info'
$VarExcel = Get-InstalledModule -Name ImportExcel -ErrorAction silentlycontinue
Write-Log -Message ('ImportExcel Module Version: {0}.{1}.{2}' -f [string]$VarExcel.Version.Major, [string]$VarExcel.Version.Minor, [string]$VarExcel.Version.Build) -Severity 'Success'
if ($null -eq $VarExcel)
{
Write-Log -Message ('Trying to install ImportExcel Module...') -Severity 'Warning'
Install-Module -Name ImportExcel -Force
}
$VarExcel = Get-InstalledModule -Name ImportExcel -ErrorAction silentlycontinue
if ($null -eq $VarExcel)
{
Write-Log -Message ('Admininstrator rights required to install ImportExcel Module. Press <Enter> to finish script') -Severity 'Error'
Read-Host ''
Exit
}
}
function CheckPowerShell()
{
Write-Log -Message ('Checking PowerShell...') -Severity 'Info'
$Global:PlatformOS = 'PowerShell Desktop'
$cloudShell = try{Get-CloudDrive}catch{}
$Global:CurrentDateTime = (get-date -Format "yyyyMMddHHmmss")
$Global:FolderName = $Global:ReportName + $CurrentDateTime
if ($cloudShell)
{
Write-Log -Message ('Identified Environment as Azure CloudShell') -Severity 'Success'
$Global:PlatformOS = 'Azure CloudShell'
$defaultOutputDir = "$HOME/InventoryReports/" + $Global:FolderName + "/"
}
elseif ($PSVersionTable.Platform -eq 'Unix')
{
Write-Log -Message ('Identified Environment as PowerShell Unix') -Severity 'Success'
$Global:PlatformOS = 'PowerShell Unix'
$defaultOutputDir = "$HOME/InventoryReports/" + $Global:FolderName + "/"
}
else
{
Write-Log -Message ('Identified Environment as PowerShell Desktop') -Severity 'Success'
$Global:PlatformOS= 'PowerShell Desktop'
$defaultOutputDir = "C:\InventoryReports\" + $Global:FolderName + "\"
$psVersion = $PSVersionTable.PSVersion.Major
Write-Log -Message ("PowerShell Version {0}" -f $psVersion) -Severity 'Info'
if ($PSVersionTable.PSVersion.Major -lt 7)
{
Write-Log -Message ("You must use Powershell 7 to run the inventory script.") -Severity 'Error'
Write-Log -Message ("https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3") -Severity 'Error'
Exit
}
}
if ($OutputDirectory)
{
try
{
$OutputDirectory = Join-Path (Resolve-Path $OutputDirectory -ErrorAction Stop) ('/' -or '\')
}
catch
{
Write-Log -Message ("Wrong OutputDirectory Path! OutputDirectory Parameter must contain the full path.") -Severity 'Error'
Exit
}
}
$Global:DefaultPath = if($OutputDirectory) {$OutputDirectory} else {$defaultOutputDir}
if ($platformOS -eq 'Azure CloudShell')
{
$Global:Subscriptions = @(az account list --output json --only-show-errors | ConvertFrom-Json)
}
elseif ($platformOS -eq 'PowerShell Unix' -or $platformOS -eq 'PowerShell Desktop')
{
LoginSession
}
}
function LoginSession()
{
$CloudEnv = az cloud list | ConvertFrom-Json
Write-Host "Azure Cloud Environment: " -NoNewline
$CurrentCloudEnvName = $CloudEnv | Where-Object {$_.isActive -eq 'True'}
Write-Host $CurrentCloudEnvName.name -ForegroundColor Green
if (!$TenantID)
{
Write-Log -Message ('Tenant ID not specified. Use -TenantID parameter if you want to specify directly.') -Severity 'Warning'
Write-Log -Message ('Authenticating Azure') -Severity 'Info'
Write-Log -Message ('Clearing account cache') -Severity 'Info'
az account clear | Out-Null
Write-Log -Message ('Calling Login, the browser will open and prompt you to login.') -Severity 'Info'
$DebugPreference = "SilentlyContinue"
if($DeviceLogin.IsPresent)
{
Write-Log -Message ('Using device login') -Severity 'Info'
az login --use-device-code
Connect-AzAccount -UseDeviceAuthentication | Out-Null
}
else
{
Write-Log -Message ('Using device login') -Severity 'Info'
az login --only-show-errors | Out-Null
Connect-AzAccount | Out-Null
}
$DebugPreference = "Continue"
$Tenants = az account list --query [].homeTenantId -o tsv --only-show-errors | Sort-Object -Unique
Write-Log -Message ('Checking number of Tenants') -Severity 'Info'
if ($Tenants.Count -eq 1)
{
Write-Log -Message ('You have privileges only in One Tenant') -Severity 'Success'
$TenantID = $Tenants
}
else
{
Write-Log -Message ('Select the the Azure Tenant ID that you want to connect: ') -Severity 'Warning'
$SequenceID = 1
foreach ($TenantID in $Tenants)
{
write-host "$SequenceID) $TenantID"
$SequenceID ++
}
[int]$SelectTenant = read-host "Select Tenant (Default 1)"
$defaultTenant = --$SelectTenant
$TenantID = $Tenants[$defaultTenant]
if($DeviceLogin.IsPresent)
{
az login --use-device-code -t $TenantID
Connect-AzAccount -UseDeviceAuthentication -Tenant $TenantID | Out-Null
}
else
{
az login -t $TenantID --only-show-errors | Out-Null
Connect-AzAccount -Tenant $TenantID | Out-Null
}
}
Write-Log -Message ("Extracting from Tenant $TenantID") -Severity 'Info'
Write-Log -Message ("Extracting Subscriptions") -Severity 'Info'
$Global:Subscriptions = @(az account list --output json --only-show-errors | ConvertFrom-Json)
$Global:Subscriptions = @($Subscriptions | Where-Object { $_.tenantID -eq $TenantID })
}
else
{
az account clear | Out-Null
if (!$Appid)
{
if($DeviceLogin.IsPresent)
{
az login --use-device-code -t $TenantID
Connect-AzAccount -UseDeviceAuthentication -Tenant $TenantID | Out-Null
}
else
{
az login -t $TenantID --only-show-errors | Out-Null
Connect-AzAccount -Tenant $TenantID | Out-Null
}
}
elseif ($Appid -and $Secret -and $tenantid)
{
Write-Log -Message ("Using Service Principal Authentication Method") -Severity 'Success'
az login --service-principal -u $appid -p $secret -t $TenantID | Out-Null
$SecureSecret = ConvertTo-SecureString $Secret -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential($Appid, $SecureSecret)
Connect-AzAccount -ServicePrincipal -Credential $Credential -Tenant $TenantID | Out-Null
}
else
{
Write-Log -Message ("You are trying to use Service Principal Authentication Method in a wrong way.") -Severity 'Error'
Write-Log -Message ("It's Mandatory to specify Application ID, Secret and Tenant ID in Azure Resource Inventory") -Severity 'Error'
Write-Log -Message (".\ResourceInventory.ps1 -appid <SP AppID> -secret <SP Secret> -tenant <TenantID>") -Severity 'Error'
Exit
}
$Global:Subscriptions = @(az account list --output json --only-show-errors | ConvertFrom-Json)
$Global:Subscriptions = @($Subscriptions | Where-Object { $_.tenantID -eq $TenantID })
}
}
function GetSubscriptionsData()
{
$SubscriptionCount = $Subscriptions.Count
Write-Log -Message ("Number of Subscriptions Found: {0}" -f $SubscriptionCount) -Severity 'Info'
Write-Log -Message ("Checking report folder: {0}" -f $DefaultPath) -Severity 'Info'
if ((Test-Path -Path $DefaultPath -PathType Container) -eq $false)
{
New-Item -Type Directory -Force -Path $DefaultPath | Out-Null
}
}
function ResourceInventoryLoop()
{
if(![string]::IsNullOrEmpty($ResourceGroup) -and [string]::IsNullOrEmpty($SubscriptionID))
{
Write-Log -Message ("Resource Group Name present, but missing Subscription ID.") -Severity 'Error'
Write-Log -Message ("If using ResourceGroup parameter you must also put SubscriptionId") -Severity 'Error'
Exit
}
if(![string]::IsNullOrEmpty($ResourceGroup))
{
$ResourceGroup = $ResourceGroup.ToLower()
}
if(![string]::IsNullOrEmpty($ResourceGroup) -and ![string]::IsNullOrEmpty($SubscriptionID))
{
Write-Log -Message ('Extracting Resources from Subscription: ' + $SubscriptionID + '. And from Resource Group: ' + $ResourceGroup) -Severity 'Success'
$Subscri = $SubscriptionID
$GraphQuery = "resources | where resourceGroup == '$ResourceGroup' and strlen(properties.definition.actions) < 123000 | summarize count()"
$EnvSize = az graph query -q $GraphQuery --subscriptions $Subscri --output json --only-show-errors | ConvertFrom-Json
$EnvSizeNum = $EnvSize.data.'count_'
if ($EnvSizeNum -ge 1) {
$Loop = $EnvSizeNum / 1000
$Loop = [math]::ceiling($Loop)
$Looper = 0
$Limit = 0
while ($Looper -lt $Loop) {
$GraphQuery = "resources | where resourceGroup == '$ResourceGroup' and strlen(properties.definition.actions) < 123000 | project id,name,type,tenantId,kind,location,resourceGroup,subscriptionId,managedBy,sku,plan,properties,identity,zones,extendedLocation,tags | order by id asc"
$Resource = (az graph query -q $GraphQuery --subscriptions $Subscri --skip $Limit --first 1000 --output json --only-show-errors).tolower() | ConvertFrom-Json
$Global:Resources += $Resource.data
Start-Sleep 2
$Looper ++
$Limit = $Limit + 1000
}
}
}
elseif([string]::IsNullOrEmpty($ResourceGroup) -and ![string]::IsNullOrEmpty($SubscriptionID))
{
Write-Log -Message ('Extracting Resources from Subscription: ' + $SubscriptionID) -Severity 'Success'
$GraphQuery = "resources | where strlen(properties.definition.actions) < 123000 | summarize count()"
$EnvSize = az graph query -q $GraphQuery --output json --subscriptions $SubscriptionID --only-show-errors | ConvertFrom-Json
$EnvSizeNum = $EnvSize.data.'count_'
if ($EnvSizeNum -ge 1) {
$Loop = $EnvSizeNum / 1000
$Loop = [math]::ceiling($Loop)
$Looper = 0
$Limit = 0
while ($Looper -lt $Loop) {
$GraphQuery = "resources | where strlen(properties.definition.actions) < 123000 | project id,name,type,tenantId,kind,location,resourceGroup,subscriptionId,managedBy,sku,plan,properties,identity,zones,extendedLocation,tags | order by id asc"
$Resource = (az graph query -q $GraphQuery --subscriptions $SubscriptionID --skip $Limit --first 1000 --output json --only-show-errors).tolower() | ConvertFrom-Json
$Global:Resources += $Resource.data
Start-Sleep 2
$Looper ++
$Limit = $Limit + 1000
}
}
}
else
{
$GraphQuery = "resources | where strlen(properties.definition.actions) < 123000 | summarize count()"
$EnvSize = az graph query -q $GraphQuery --output json --only-show-errors | ConvertFrom-Json
$EnvSizeCount = $EnvSize.Data.'count_'
Write-Log -Message ("Resources Output: {0} Resources Identified" -f $EnvSizeCount) -Severity 'Success'
if ($EnvSizeCount -ge 1)
{
$Loop = $EnvSizeCount / 1000
$Loop = [math]::Ceiling($Loop)
$Looper = 0
$Limit = 0
while ($Looper -lt $Loop)
{
$GraphQuery = "resources | where strlen(properties.definition.actions) < 123000 | project id,name,type,tenantId,kind,location,resourceGroup,subscriptionId,managedBy,sku,plan,properties,identity,zones,extendedLocation,tags | order by id asc"
$Resource = (az graph query -q $GraphQuery --skip $Limit --first 1000 --output json --only-show-errors).tolower() | ConvertFrom-Json
$Global:Resources += $Resource.Data
Start-Sleep 2
$Looper++
$Limit = $Limit + 1000
}
}
}
}
function ResourceInventoryAvd()
{
$AVDSize = az graph query -q "desktopvirtualizationresources | summarize count()" --output json --only-show-errors | ConvertFrom-Json
$AVDSizeCount = $AVDSize.data.'count_'
Write-Host ("AVD Resources Output: {0} AVD Resources Identified" -f $AVDSizeCount) -BackgroundColor Black -ForegroundColor Green
if ($AVDSizeCount -ge 1)
{
$Loop = $AVDSizeCount / 1000
$Loop = [math]::ceiling($Loop)
$Looper = 0
$Limit = 0
while ($Looper -lt $Loop)
{
$GraphQuery = "desktopvirtualizationresources | project id,name,type,tenantId,kind,location,resourceGroup,subscriptionId,managedBy,sku,plan,properties,identity,zones,extendedLocation,tags | order by id asc"
$AVD = (az graph query -q $GraphQuery --skip $Limit --first 1000 --output json --only-show-errors).tolower() | ConvertFrom-Json
$Global:Resources += $AVD.data
Start-Sleep 2
$Looper++
$Limit = $Limit + 1000
}
}
}
CheckVersion
CheckCliRequirements
CheckPowerShell
GetSubscriptionsData
ResourceInventoryLoop
ResourceInventoryAvd
}
function ExecuteInventoryProcessing()
{
function InitializeInventoryProcessing()
{
$Global:ZipOutputFile = ($DefaultPath + $Global:ReportName + "_" + $CurrentDateTime + ".zip")
$Global:File = ($DefaultPath + $Global:ReportName + "_" + $CurrentDateTime + ".xlsx")
$Global:AllResourceFile = ($DefaultPath + "Full_" + $Global:ReportName + "_" + $CurrentDateTime + ".json")
$Global:JsonFile = ($DefaultPath + "Inventory_"+ $Global:ReportName + "_" + $CurrentDateTime + ".json")
$Global:MetricsJsonFile = ($DefaultPath + "Metrics_"+ $Global:ReportName + "_" + $CurrentDateTime + ".json")
$Global:ConsumptionFile = ($DefaultPath + "Consumption_"+ $Global:ReportName + "_" + $CurrentDateTime + ".json")
$Global:ConsumptionFileCsv = ($DefaultPath + "Consumption_"+ $Global:ReportName + "_" + $CurrentDateTime + ".csv")
$Global:LogFile = ($DefaultPath + "Logs_"+ $Global:ReportName + "_" + $CurrentDateTime + ".json")
Write-Log -Message ('Report Excel File: {0}' -f $File) -Severity 'Info'
}
function CreateMetricsJob()
{
Write-Log -Message ('Checking if Metrics Job Should be Run.') -Severity 'Info'
if (!$SkipMetrics.IsPresent)
{
Write-Log -Message ('Running Metrics Jobs') -Severity 'Success'
if($PSScriptRoot -like '*\*')
{
$MetricPath = Get-ChildItem -Path ($PSScriptRoot + '\Extension\Metrics.ps1') -Recurse
}
else
{
$MetricPath = Get-ChildItem -Path ($PSScriptRoot + '/Extension/Metrics.ps1') -Recurse
}
$metricsFilePath = ($DefaultPath + "Metrics_"+ $Global:ReportName + "_" + $CurrentDateTime + "_")
$Global:AzMetrics = New-Object PSObject
$Global:AzMetrics | Add-Member -MemberType NoteProperty -Name Metrics -Value NotSet
$Global:AzMetrics.Metrics = & $MetricPath -Subscriptions $Subscriptions -Resources $Resources -Task "Processing" -File $file -Metrics $null -TableStyle $null -ConcurrencyLimit $ConcurrencyLimit -FilePath $metricsFilePath
}
}
function ProcessMetricsResult()
{
if (!$SkipMetrics.IsPresent)
{
$([System.GC]::GetTotalMemory($false))
$([System.GC]::Collect())
$([System.GC]::GetTotalMemory($true))
}
}
function GetServiceName($moduleUrl)
{
if ($moduleUrl -like '*Services/Analytics*')
{
$directoryService = 'Analytics'
}
if ($moduleUrl -like '*Services/Compute*')
{
$directoryService = 'Compute'
}
if ($moduleUrl -like '*Services/Containers*')
{
$directoryService = 'Containers'
}
if ($moduleUrl -like '*Services/Data*')
{
$directoryService = 'Data'
}
if ($moduleUrl -like '*Services/Infrastructure*')
{
$directoryService = 'Infrastructure'
}
if ($moduleUrl -like '*Services/Integration*')
{
$directoryService = 'Integration'
}
if ($moduleUrl -like '*Services/Networking*')
{
$directoryService = 'Networking'
}
if ($moduleUrl -like '*Services/Storage*')
{
$directoryService = 'Storage'
}
return $directoryService
}
function CreateResourceJobs()
{
$Global:SmaResources = New-Object PSObject
Write-Log -Message ('Starting Service Processing Jobs.') -Severity 'Info'
if($PSScriptRoot -like '*\*')
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '\Services\*.ps1') -Recurse
}
else
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '/Services/*.ps1') -Recurse
}
$Resource = $Resources | Select-Object -First $Resources.count
#$Resource = ($Resource | ConvertTo-Json -Depth 50)
foreach ($Module in $Modules)
{
$ModName = $Module.Name.Substring(0, $Module.Name.length - ".ps1".length)
Write-Log -Message ("Service Processing: {0}" -f $ModName) -Severity 'Success'
$result = & $Module -SCPath $SCPath -Sub $Subscriptions -Resources $Resource -Task "Processing" -File $file -SmaResources $null -TableStyle $null -Metrics $Global:AzMetrics
$Global:SmaResources | Add-Member -MemberType NoteProperty -Name $ModName -Value NotSet
$Global:SmaResources.$ModName = $result
$result = $null
[System.GC]::Collect()
}
}
function ProcessResourceResult()
{
Write-Log -Message ("Starting Reporting Phase.") -Severity 'Info'
$Services = @()
if($PSScriptRoot -like '*\*')
{
$Services = Get-ChildItem -Path ($PSScriptRoot + '\Services\*.ps1') -Recurse
}
else
{
$Services = Get-ChildItem -Path ($PSScriptRoot + '/Services/*.ps1') -Recurse
}
Write-Log -Message ('Services Found: ' + $Services.Count) -Severity 'Info'
$Lops = $Services.count
$ReportCounter = 0
foreach ($Service in $Services)
{
$c = (($ReportCounter / $Lops) * 100)
$c = [math]::Round($c)
Write-Log -Message ("Running Services: $Service") -Severity 'Info'
$ProcessResults = & $Service.FullName -SCPath $PSScriptRoot -Sub $null -Resources $null -Task "Reporting" -File $file -SmaResources $Global:SmaResources -TableStyle $Global:TableStyle -Metrics $null
$ReportCounter++
}
$Global:SmaResources | Add-Member -MemberType NoteProperty -Name 'Version' -Value NotSet
$Global:SmaResources.Version = $Global:Version
$Global:SmaResources | ConvertTo-Json -depth 100 -compress | Out-File $Global:JsonFile
#$Global:Resources | ConvertTo-Json -depth 100 -compress | Out-File $Global:AllResourceFile
Write-Log -Message ('Resource Reporting Phase Done.') -Severity 'Info'
}
function GetResorceConsumption()
{
$DebugPreference = "SilentlyContinue"
#Force the culture here...
[System.Threading.Thread]::CurrentThread.CurrentUICulture = "en-US";
[System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US";
$reportedStartTime = (Get-Date).AddDays(-31).Date.AddHours(0).AddMinutes(0).AddSeconds(0).DateTime
$reportedEndTime = (Get-Date).AddDays(-1).Date.AddHours(0).AddMinutes(0).AddSeconds(0).DateTime
foreach($sub in $Global:Subscriptions)
{
# Check if SubscriptionId is not null, not empty, and matches $sub.id
if (![string]::IsNullOrEmpty($SubscriptionID))
{
if (![string]::IsNullOrEmpty($ResourceGroup))
{
Write-Log -Message ("Cannot filter consumption by resource group." -f $sub.Name) -Severity 'Info'
}
if($SubscriptionID -ne $sub.Id)
{
Write-Log -Message ("Skipping: {0}" -f $sub.Name) -Severity 'Info'
continue
}
}
Set-AzContext -Subscription $sub.id | Out-Null
Write-Log -Message ("Gathering Consumption for: {0}" -f $sub.Name) -Severity 'Info'
do
{
$params = @{
ReportedStartTime = $reportedStartTime
ReportedEndTime = $reportedEndTime
AggregationGranularity = 'Daily'
ShowDetails = $true
}
$params.ContinuationToken = $usageData.ContinuationToken
$usageData = Get-UsageAggregates @params
$usageDataExport = $usageData.UsageAggregations.Properties | Select-Object InstanceData, MeterCategory, MeterId, MeterName, MeterRegion, MeterSubCategory, Quantity, Unit, UsageStartTime, UsageEndTime
Write-Log -Message ("Records found: $($usageDataExport.Count)...") -Severity 'Info'
$newUsageDataExport = [System.Collections.ArrayList]::new()
for($item = 0; $item -lt $usageDataExport.Count; $item++)
{
$instanceInfo = ($usageDataExport[$item].InstanceData.tolower() | ConvertFrom-Json)
if (![string]::IsNullOrEmpty($ResourceGroup))
{
if(!$instanceInfo.'Microsoft.Resources'.resourceUri.toLower().Contains("/" + $ResourceGroup.toLower() + "/"))
{
continue;
}
}
$usageDataExport[$item] | Add-Member -MemberType NoteProperty -Name ResourceId -Value NotSet
$usageDataExport[$item] | Add-Member -MemberType NoteProperty -Name ResourceLocation -Value NotSet
$usageDataExport[$item] | Add-Member -MemberType NoteProperty -Name ConsumptionMeter -Value NotSet
$usageDataExport[$item] | Add-Member -MemberType NoteProperty -Name ReservationId -Value NotSet
$usageDataExport[$item] | Add-Member -MemberType NoteProperty -Name ReservationOrderId -Value NotSet
$usageDataExport[$item].ResourceId = $instanceInfo.'Microsoft.Resources'.resourceUri
$usageDataExport[$item].ResourceLocation = $instanceInfo.'Microsoft.Resources'.location
$usageDataExport[$item].ConsumptionMeter = $instanceInfo.'Microsoft.Resources'.additionalInfo.ConsumptionMeter
$usageDataExport[$item].ReservationId = $instanceInfo.'Microsoft.Resources'.additionalInfo.ReservationId
$usageDataExport[$item].ReservationOrderId = $instanceInfo.'Microsoft.Resources'.additionalInfo.ReservationOrderId
$instanceObject = [PSCustomObject]@{}
$additionalInfoInstance = [PSCustomObject]@{
ResourceUri = $instanceInfo.'Microsoft.Resources'.resourceUri
Location = $instanceInfo.'Microsoft.Resources'.location
additionalInfo = [PSCustomObject]@{
ConsumptionMeter = if ($null -eq $instanceInfo.'Microsoft.Resources'.additionalInfo.ConsumptionMeter) { "" } else { $instanceInfo.'Microsoft.Resources'.additionalInfo.ConsumptionMeter }
vCores = 0
VCPUs = 0
ServiceType = ""
ResourceCategory = ""
}
}
$instanceObject | Add-Member -MemberType NoteProperty -Name "Microsoft.Resources" -Value $additionalInfoInstance
$usageDataExport[$item].InstanceData = $instanceObject | ConvertTo-Json -Compress
$newUsageDataExport.Add($usageDataExport[$item]) | Out-Null
}
#$newUsageDataExport | Export-Csv $Global:ConsumptionFileCsv -Encoding utf-8 -Append
$newUsageDataExport | Select-Object InstanceData, MeterCategory, MeterId, MeterName, MeterRegion, MeterSubCategory, Quantity, Unit, UsageStartTime, UsageEndTime, ResourceId, ResourceLocation, ConsumptionMeter, ReservationId, ReservationOrderId | Export-Csv $Global:ConsumptionFileCsv -Encoding utf8 -Append -NoTypeInformation
} while ('ContinuationToken' -in $usageData.psobject.properties.name -and $usageData.ContinuationToken)
}
$DebugPreference = "Continue"
}
InitializeInventoryProcessing
CreateMetricsJob
CreateResourceJobs
ProcessMetricsResult
ProcessResourceResult
if(!$SkipConsumption.IsPresent)
{
GetResorceConsumption
#ProcessResourceConsumption
}
}
function FinalizeOutputs
{
function ProcessSummary()
{
Write-Log -Message ('Creating Summary Report') -Severity 'Info'
Write-Log -Message ('Starting Summary Report Processing Job.') -Severity 'Info'
if($PSScriptRoot -like '*\*')
{
$SummaryPath = Get-ChildItem -Path ($PSScriptRoot + '\Extension\Summary.ps1') -Recurse
}
else
{
$SummaryPath = Get-ChildItem -Path ($PSScriptRoot + '/Extension/Summary.ps1') -Recurse
}
$ChartsRun = & $SummaryPath -File $file -TableStyle $TableStyle -PlatOS $PlatformOS -Subscriptions $Subscriptions -Resources $Resources -ExtractionRunTime $Runtime -ReportingRunTime $ReportingRunTime -RunLite $false -Version $Global:Version
}
ProcessSummary
}
$Global:PowerShellTranscriptFile = ($DefaultPath + "Transcript_Log_"+ $Global:ReportName + "_" + $CurrentDateTime + ".txt")
Start-Transcript -Path $Global:PowerShellTranscriptFile -UseMinimalHeader
# Setup and Inventory Gathering
$Global:Runtime = Measure-Command -Expression {
Variables
RunInventorySetup
}
# Execution and processing of inventory
$Global:ReportingRunTime = Measure-Command -Expression {
ExecuteInventoryProcessing
}
Stop-Transcript
# Prepare the summary and outputs
FinalizeOutputs
Write-Log -Message ("Compressing Resources Output: {0}" -f $Global:ZipOutputFile) -Severity 'Info'
if($EnableLogs.IsPresent)
{
$Global:Logging | ConvertTo-Json -depth 5 -compress | Out-File $Global:LogFile
}
if($SkipMetrics.IsPresent)
{
"Metrics Not Gathered" | ConvertTo-Json -depth 5 -compress | Out-File $Global:MetricsJsonFile
}
$consumptionCreated = Test-Path -Path $Global:ConsumptionFileCsv
if($SkipConsumption.IsPresent -or !$consumptionCreated)
{
Out-File $Global:ConsumptionFileCsv
}
$jsonWildCard = $DefaultPath + "*.json"
$compressionOutput = @{
Path = $Global:File, $Global:ConsumptionFileCsv, $Global:PowerShellTranscriptFile, $jsonWildCard
CompressionLevel = 'Fastest'
DestinationPath = $Global:ZipOutputFile
}
try
{
Compress-Archive @compressionOutput
}
catch
{
$_ | Format-List -Force
Write-Error ("Error Compressing Output File: {0}." -f $Global:ZipOutputFile)
Write-Error ("Please zip the output files manually.")
}
Write-Log -Message ("Execution Time: {0}" -f $Runtime) -Severity 'Success'
Write-Log -Message ("Reporting Time: {0}" -f $ReportingRunTime) -Severity 'Success'
Write-Log -Message ("Reporting Data File: {0}" -f $Global:ZipOutputFile) -Severity 'Success'