Skip to content

Commit

Permalink
3.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio-Merola committed Sep 17, 2024
1 parent 1a6d71a commit 0f410bc
Show file tree
Hide file tree
Showing 68 changed files with 522 additions and 503 deletions.
11 changes: 8 additions & 3 deletions AzureResourceInventory.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'AzureResourceInventory.psm1'

# Version number of this module.
ModuleVersion = '3.5.1'
ModuleVersion = '3.5.2'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -68,7 +68,7 @@ RequiredModules = @('ImportExcel','Az.Accounts','Az.Compute','Az.ResourceGraph',
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('Modules/Core/ARITestPS.psm1',
'Modules/ARIResourceJobs.psm1',
'Modules/ARIResourcesReport.psm1',
'Modules/ARIReportJobs.psm1',
'Modules/Core/ARIExtraJobs.psm1'
'Modules/Core/ARILoginSession.psm1',
'Modules/Inventory/ARIResourceDataPull.psm1',
Expand All @@ -81,6 +81,7 @@ NestedModules = @('Modules/Core/ARITestPS.psm1',
'Modules/Inventory/ARISubInv.psm1',
'Modules/Inventory/ARIQuotaInv.psm1',
'Modules/Extras/ARIReportCharts.psm1',
'Modules/Extras/ARIExcelDetails.psm1',
'Modules/Diagram/ARIDrawIODiagram.psm1',
'Modules/Diagram/ARIDiagramNetwork.psm1',
'Modules/Diagram/ARIDiagramOrganization.psm1',
Expand All @@ -91,6 +92,7 @@ NestedModules = @('Modules/Core/ARITestPS.psm1',
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Invoke-ARI',
'Connect-ARILoginSession',
'Build-ARIExcelLargeReport',
'Test-ARIPS',
'Start-AzureResourceDataPull',
'Get-ARISubscriptions',
Expand Down Expand Up @@ -118,7 +120,10 @@ FunctionsToExport = @('Invoke-ARI',
'Get-ARIUnsupportedData',
'Start-ARIQuotaJob',
'Start-ARIAutResourceJob',
'Get-ARIAPIResources')
'Get-ARIAPIResources',
'Build-ARILargeReportResources',
'Start-ARIExcelHeaders',
'Start-ARILargeEnvOrderFiles')

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
12 changes: 7 additions & 5 deletions AzureResourceInventory.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ param ([ValidateSet('AzureCloud', 'AzureUSGovernment')]
$TagKey,
$TagValue,
[switch]$SecurityCenter,
[switch]$Heavy,
[switch]$SkipAdvisory,
[switch]$SkipPolicy,
[switch]$SkipAPIs,
Expand Down Expand Up @@ -162,8 +163,6 @@ param ([ValidateSet('AzureCloud', 'AzureUSGovernment')]
Write-Host ""
}

<######################################################### END OF FUNCTIONS ######################################################################>

$TotalRunTime = Measure-Command -Expression {

if ($Help.IsPresent) {
Expand All @@ -172,8 +171,11 @@ param ([ValidateSet('AzureCloud', 'AzureUSGovernment')]
}
else {

Write-Host ('Checking for Powershell Module Updates..')
Update-Module -Name AzureResourceInventory -AcceptLicense
if ($PlatOS -ne 'Azure CloudShell' -and !$Automation.IsPresent)
{
Write-Host ('Checking for Powershell Module Updates..')
Update-Module -Name AzureResourceInventory -AcceptLicense
}

$PlatOS = Test-ARIPS -Debug $Debug

Expand Down Expand Up @@ -325,7 +327,7 @@ param ([ValidateSet('AzureCloud', 'AzureUSGovernment')]

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting Resources Report Function.')

Build-AzureResourceReport -Subscriptions $Subscriptions -DefaultPath $DefaultPath -ExtractionRunTime $ExtractionRuntime -Resources $Resources -SecurityCenter $SecurityCenter -File $File -DDFile $DDFile -SkipDiagram $SkipDiagram -RunLite $RunLite -PlatOS $PlatOS -InTag $InTag -SkipPolicy $SkipPolicy -SkipAdvisory $SkipAdvisory -Automation $Automation -SkipAPIs $SkipAPIs, -Overview $Overview -Debug $Debug
Build-AzureResourceReport -Subscriptions $Subscriptions -DefaultPath $DefaultPath -ExtractionRunTime $ExtractionRuntime -Resources $Resources -SecurityCenter $SecurityCenter -File $File -DDFile $DDFile -Heavy $Heavy -SkipDiagram $SkipDiagram -RunLite $RunLite -PlatOS $PlatOS -InTag $InTag -SkipPolicy $SkipPolicy -SkipAdvisory $SkipAdvisory -Automation $Automation -SkipAPIs $SkipAPIs, -Overview $Overview -Debug $Debug

if ($StorageAccount)
{
Expand Down
230 changes: 230 additions & 0 deletions Modules/ARIReportJobs.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
function Start-ARIResourceReporting {
Param($InTag, $file, $SmaResources, $DefaultPath, $TableStyle, $Unsupported, $DebugEnvSize, $DataActive, $Debug)
if ($Debug.IsPresent)
{
$DebugPreference = 'Continue'
$ErrorActionPreference = 'Continue'
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting Reporting Phase.')
Write-Progress -activity $DataActive -Status "Processing Inventory" -PercentComplete 50

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Running Asynchronous, Gathering List Of Modules.')
if($PSScriptRoot -like '*\*')
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '\Scripts\*.ps1') -Recurse
}
else
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '/Scripts/*.ps1') -Recurse
}

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Modules Found: ' + $Modules.Count)
$Lops = $Modules.count
$ReportCounter = 0
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Large Environment. Looking for Cached Resource Files.')

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting to Process Modules.')
foreach ($Module in $Modules) {

$c = (($ReportCounter / $Lops) * 100)
$c = [math]::Round($c)
Write-Progress -Id 1 -activity "Building Report" -Status "$c% Complete." -PercentComplete $c

$ModuSeq0 = New-Object System.IO.StreamReader($Module.FullName)
$ModuSeq = $ModuSeq0.ReadToEnd()
$ModuSeq0.Dispose()
$ModuleName = $Module.name.replace('.ps1','')

if ($DebugEnvSize -eq 'Large')
{
$SmaResources = @{}
if (Test-Path -Path ($DefaultPath+'\ReportCache\ResourceCache\'+$ModuleName+'.json') -PathType Leaf)
{
$SmaResources["$ModuleName"] = Get-Content -Path ($DefaultPath+'\ReportCache\ResourceCache\'+$ModuleName+'.json') | ConvertFrom-Json
}
else
{
$SmaResources["$ModuleName"] = 0
}
}

$ModuleResourceCount = $SmaResources.$ModuleName.count

if ($ModuleResourceCount -gt 0)
{
Start-Sleep -Milliseconds 25
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+"Running Module: '$ModuleName'. Lines Count: $ModuleResourceCount")

$ExcelRun = ([PowerShell]::Create()).AddScript($ModuSeq).AddArgument($PSScriptRoot).AddArgument($null).AddArgument($InTag).AddArgument($null).AddArgument('Reporting').AddArgument($file).AddArgument($SmaResources).AddArgument($TableStyle).AddArgument($Unsupported)

$ExcelJob = $ExcelRun.BeginInvoke()

while ($ExcelJob.IsCompleted -contains $false) { Start-Sleep -Milliseconds 200 }

$ExcelRun.EndInvoke($ExcelJob)

$ExcelRun.Dispose()
Remove-Variable -Name ExcelRun
Remove-Variable -Name ExcelJob

}

if ($DebugEnvSize -eq 'Large')
{
Remove-Variable -Name SmaResources
[System.GC]::Collect()
Start-Sleep -Milliseconds 50
}

$ReportCounter ++
}

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Cleaning Variables to Release Memory.')

if ($DebugEnvSize -eq 'Large')
{
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Removing Cache Files.')
Remove-Item -Path ($DefaultPath+'\ReportCache') -Recurse
}
else
{
Remove-Variable -Name SmaResources
}

[System.GC]::GetTotalMemory($true) | out-null
Start-Sleep -Milliseconds 50
}

function Build-ARILargeReportResources {
Param($DefaultPath, $Debug)
if ($Debug.IsPresent)
{
$DebugPreference = 'Continue'
$ErrorActionPreference = 'Continue'
}
else
{
$ErrorActionPreference = "silentlycontinue"
}
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Looking for Cache Files.')
$LocalCacheFiles = Get-ChildItem -Path ($DefaultPath+'\ReportCache\*.json')

$Looper = 0

foreach ($LocalFile in $LocalCacheFiles)
{
$Looper ++
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting Resource Excel Data Processing Jobs.')
Start-job -Name ('ExcelJob_'+$Looper) -ScriptBlock {

$LocalFile = $($args[1])
$DefaultPath = $($args[2])
$LocalFolder = $LocalFile.Name.replace(".json","")

if($($args[0]) -like '*\*')
{
$Modules = Get-ChildItem -Path ($($args[0]) + '\Scripts\*.ps1') -Recurse

$ModFolder = ($DefaultPath+'\ReportCache\'+$LocalFolder+'\')
if ((Test-Path -Path $ModFolder -PathType Container) -eq $false) {
New-Item -Type Directory -Force -Path $ModFolder | Out-Null
}
}
else
{
$Modules = Get-ChildItem -Path ($($args[0]) + '/Scripts/*.ps1') -Recurse
$ModFolder = ($DefaultPath+'/ReportCache/'+$LocalFolder+'/')
}

$TempContent = Get-Content -Path $LocalFile | ConvertFrom-Json

$job = @()

$Modules | ForEach-Object {
$ModName = $_.Name.replace(".ps1","")
if($TempContent.$ModName.count -gt 0)
{
$TempVal = $TempContent.$ModName
$ModNameFile = ($ModName+'.json')
Start-Sleep -Milliseconds 100

New-Variable -Name ('ModRun' + $ModName)
New-Variable -Name ('ModJob' + $ModName)

Set-Variable -Name ('ModRun' + $ModName) -Value ([PowerShell]::Create()).AddScript({Param($ModFolder,$TempVal,$ModNameFile)$TempVal | ConvertTo-Json -Depth 50 | Out-File -FilePath ($ModFolder+$ModNameFile)}).AddArgument($ModFolder).AddArgument($TempVal).AddArgument($ModNameFile)

Set-Variable -Name ('ModJob' + $ModName) -Value ((get-variable -name ('ModRun' + $ModName)).Value).BeginInvoke()

Start-Sleep -Milliseconds 100

$job += (get-variable -name ('ModJob' + $ModName)).Value
Remove-Variable -Name ModName
}
}

while ($Job.Runspace.IsCompleted -contains $false) { Start-Sleep -Milliseconds 1000 }

$Modules | ForEach-Object {
$ModName = $_.Name.replace(".ps1","")
if($TempContent.$ModName.count -gt 0)
{
Remove-Variable -Name ('ModRun' + $ModName)
Remove-Variable -Name ('ModJob' + $ModName)
Remove-Variable -Name ModName
}
}

[System.GC]::Collect() | out-null
Start-Sleep -Milliseconds 50

} -ArgumentList $PSScriptRoot, $LocalFile, $DefaultPath
}

}

function Start-ARILargeEnvOrderFiles {
Param($DefaultPath,$Debug)
if ($Debug.IsPresent)
{
$DebugPreference = 'Continue'
$ErrorActionPreference = 'Continue'
}
else
{
$ErrorActionPreference = "silentlycontinue"
}

Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Ordering Cached Files.')

if($PSScriptRoot -like '*\*')
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '\Scripts\*.ps1') -Recurse
$ModFolder = ($DefaultPath+'\ReportCache\ResourceCache\')
if ((Test-Path -Path $ModFolder -PathType Container) -eq $false) {
New-Item -Type Directory -Force -Path $ModFolder | Out-Null
}
}
else
{
$Modules = Get-ChildItem -Path ($PSScriptRoot + '/Scripts/*.ps1') -Recurse
$ModFolder = ($DefaultPath+'/ReportCache/ResourceCache/')
if ((Test-Path -Path $ModFolder -PathType Container) -eq $false) {
New-Item -Type Directory -Force -Path $ModFolder | Out-Null
}
}

foreach ($Module in $Modules)
{
$ModuleName = $Module.name.replace('.ps1','')
if (Test-Path -Path ($DefaultPath+'\ReportCache\ResourceJob_*\'+$ModuleName+'.json') -PathType Leaf)
{
Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Merging Cached File for: '+$ModuleName)
$ModContent = Get-ChildItem -Path ($DefaultPath+'\ReportCache\ResourceJob_*\'+$ModuleName+'.json') | ForEach-Object {Get-Content -Path $_ | ConvertFrom-Json}
$ModContent | ConvertTo-Json -Depth 40 | Out-File -FilePath ($ModFolder+'\'+$ModuleName+'.json')
}
}
}
Loading

0 comments on commit 0f410bc

Please sign in to comment.