AzureResourceInventory.psm1

<#
.SYNOPSIS
    This script creates Excel file to Analyze Azure Resources inside a Tenant
 
.DESCRIPTION
    Do you want to analyze your Azure Advisories in a table format? Document it in xlsx format.
 
.PARAMETER TenantID
    Specify the tenant ID you want to create a Resource Inventory.
 
    >>> IMPORTANT: YOU NEED TO USE THIS PARAMETER FOR TENANTS WITH MULTI-FACTOR AUTHENTICATION. <<<
 
.PARAMETER SubscriptionID
    Use this parameter to collect a specific Subscription in a Tenant
 
.PARAMETER ManagementGroup
    Use this parameter to collect a all Subscriptions in a Specific Management Group in a Tenant
 
.PARAMETER Lite
    Use this parameter to use only the Import-Excel module and don't create the charts (using Excel's API)
 
.PARAMETER SecurityCenter
    Use this parameter to collect Security Center Advisories
 
.PARAMETER SkipAdvisory
    Use this parameter to skip the capture of Azure Advisories
 
.PARAMETER SkipPolicy
    Use this parameter to skip the capture of Azure Policies
 
.PARAMETER QuotaUsage
    Use this parameter to include Quota information
 
.PARAMETER IncludeTags
    Use this parameter to include Tags of every Azure Resources
 
.PARAMETER Debug
    Output detailed debug information.
 
.EXAMPLE
    Default utilization. Read all tenants you have privileges, select a tenant in menu and collect from all subscriptions:
    PS C:\> .\AzureResourceInventory.ps1
 
    Define the Tenant ID:
    PS C:\> .\AzureResourceInventory.ps1 -TenantID <your-Tenant-Id>
 
    Define the Tenant ID and for a specific Subscription:
    PS C:\>.\AzureResourceInventory.ps1 -TenantID <your-Tenant-Id> -SubscriptionID <your-Subscription-Id>
 
.NOTES
    AUTHORS: Claudio Merola and Renato Gregio | Azure Infrastucture/Automation/Devops/Governance
 
    Copyright (c) 2018 Microsoft Corporation. All rights reserved.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
 
.LINK
    Official Repository: https://github.com/microsoft/ARI
#>

Function Invoke-ARI {
param ([ValidateSet('AzureCloud', 'AzureUSGovernment')]
        $AzureEnvironment = 'AzureCloud',
        $TenantID,
        $SubscriptionID,
        $ManagementGroup,
        [string[]]$ResourceGroup,
        $TagKey,
        $TagValue,
        [switch]$SecurityCenter,
        [switch]$SkipAdvisory,
        [switch]$SkipPolicy,
        [switch]$IncludeTags,
        [switch]$QuotaUsage,
        [switch]$SkipDiagram,
        [switch]$Automation,
        $StorageAccount,
        $StorageContainer,
        [switch]$Lite,
        [switch]$Online,
        [switch]$Debug,
        [switch]$Help,
        [switch]$DeviceLogin,
        [switch]$DiagramFullEnvironment,
        $ReportName = 'AzureResourceInventory',
        $ReportDir)

    if ($Debug.IsPresent)
        {
            $DebugPreference = 'Continue'
            $ErrorActionPreference = 'Continue'
        }
    else
        {
            $ErrorActionPreference = "silentlycontinue"
        }

    Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Debbuging Mode: On. ErrorActionPreference was set to "Continue", every error will be presented.')

    if ($IncludeTags.IsPresent) { $InTag = $true } else { $InTag = $false }

    if ($Online.IsPresent) { $RunOnline = $true }else { $RunOnline = $false }
    if ($Lite.IsPresent -or $Automation.IsPresent) { $RunLite = $true }else { $RunLite = $false }
    if ($DiagramFullEnvironment.IsPresent) {$FullEnv = $true}else{$FullEnv = $false}

    <######################################################### Help ######################################################################>

    Function Get-UsageMode() {
        Write-Host ""
        Write-Host "Parameters"
        Write-Host ""
        Write-Host " -TenantID <ID> : Specifies the Tenant to be inventoried. "
        Write-Host " -SubscriptionID <ID> : Specifies Subscription(s) to be inventoried. "
        Write-Host " -ResourceGroup <NAME>: Specifies one (or more) unique Resource Group to be inventoried, This parameter requires the -SubscriptionID to work. "
        Write-Host " -TagKey <NAME> : Specifies the tag key to be inventoried, This parameter requires the -SubscriptionID to work. "
        Write-Host " -TagValue <NAME> : Specifies the tag value be inventoried, This parameter requires the -SubscriptionID to work. "
        Write-Host " -SkipAdvisory : Do not collect Azure Advisory. "
        Write-Host " -SkipPolicy : Do not collect Azure Policies. "
        Write-Host " -SecurityCenter : Include Security Center Data. "
        Write-Host " -IncludeTags : Include Resource Tags. "
        Write-Host " -Online : Use Online Modules. "
        Write-Host " -Debug : Run in a Debug mode. "
        Write-Host " -AzureEnvironment : Change the Azure Cloud Environment. "
        Write-Host " -ReportName : Change the Default Name of the report. "
        Write-Host " -ReportDir : Change the Default Path of the report. "
        Write-Host ""
        Write-Host ""
        Write-Host ""
        Write-Host "Usage Mode and Examples: "
        Write-Host "For CloudShell:"
        Write-Host "e.g. />./AzureResourceInventory.ps1"
        Write-Host ""
        Write-Host "For PowerShell Desktop:"
        Write-Host ""
        Write-Host "If you do not specify Resource Inventory will be performed on all subscriptions for the selected tenant. "
        Write-Host "e.g. />./AzureResourceInventory.ps1"
        Write-Host ""
        Write-Host "To perform the inventory in a specific Tenant and subscription use <-TenantID> and <-SubscriptionID> parameter "
        Write-Host "e.g. />./AzureResourceInventory.ps1 -TenantID <Azure Tenant ID> -SubscriptionID <Subscription ID>"
        Write-Host ""
        Write-Host "Including Tags:"
        Write-Host " By Default Azure Resource inventory do not include Resource Tags."
        Write-Host " To include Tags at the inventory use <-IncludeTags> parameter. "
        Write-Host "e.g. />./AzureResourceInventory.ps1 -TenantID <Azure Tenant ID> -IncludeTags"
        Write-Host ""
        Write-Host "Skipping Azure Advisor:"
        Write-Host " By Default Azure Resource inventory collects Azure Advisor Data."
        Write-Host " To ignore this use <-SkipAdvisory> parameter. "
        Write-Host "e.g. />./AzureResourceInventory.ps1 -TenantID <Azure Tenant ID> -SubscriptionID <Subscription ID> -SkipAdvisory"
        Write-Host ""
        Write-Host "Using the latest modules :"
        Write-Host " You can use the latest modules. For this use <-Online> parameter."
        Write-Host " It's a pre-requisite to have internet access for ARI GitHub repo"
        Write-Host "e.g. />./AzureResourceInventory.ps1 -TenantID <Azure Tenant ID> -Online"
        Write-Host ""
        Write-Host "Running in Debug Mode :"
        Write-Host " To run in a Debug Mode use <-Debug> parameter."
        Write-Host ".e.g. />/AzureResourceInventory.ps1 -TenantID <Azure Tenant ID> -Debug"
        Write-Host ""
    }

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

    $TotalRunTime = Measure-Command -Expression {

    if ($Help.IsPresent) {
        Get-UsageMode
        Exit
    }
    else {

        $PlatOS = Test-ARIPS -Debug $Debug

        if ($PlatOS -ne 'Azure CloudShell' -and !$Automation.IsPresent)
            {
                $TenantID = Connect-ARILoginSession -AzureEnvironment $AzureEnvironment -TenantID $TenantID -SubscriptionID $SubscriptionID -DeviceLogin $DeviceLogin
            }
        elseif ($Automation.IsPresent)
            {
                try {
                    $AzureConnection = (Connect-AzAccount -Identity).context

                    Set-AzContext -SubscriptionName $AzureConnection.Subscription -DefaultProfile $AzureConnection

                    $StorageContext = New-AzStorageContext -StorageAccountName $StorageAccount -UseConnectedAccount
                }
                catch {
                    Write-Output "Failed to set Automation Account requirements. Aborting." 
                    exit
                }
            }

        $Subscriptions = Get-ARISubscriptions -TenantID $TenantID

        if ($PlatOS -eq 'Azure CloudShell')
            {
                $DefaultPath = if($ReportDir) {$ReportDir} else {"$HOME/AzureResourceInventory/"}
                $DiagramCache = if($ReportDir) {$ReportDir} else {"$HOME/AzureResourceInventory/DiagramCache/"}
                if($ReportDir)
                {
                    try
                        {
                            Resolve-Path $ReportDir -ErrorAction STOP
                            if ($ReportDir -notmatch '/$')
                                {
                                    $ReportDir = $ReportDir + '/'
                                }
                        }
                    catch
                        {
                            Write-Output "ReportDir Parameter must contain the full path."
                            Exit
                        }
                }
            }
        elseif ($PlatOS -eq 'PowerShell Unix')
            {
                $DefaultPath = if($ReportDir) {$ReportDir} else {"$HOME/AzureResourceInventory/"}
                $DiagramCache = if($ReportDir) {$ReportDir} else {"$HOME/AzureResourceInventory/DiagramCache/"}
                if($ReportDir)
                {
                    try
                        {
                            Resolve-Path $ReportDir -ErrorAction STOP
                            if ($ReportDir -notmatch '/$')
                                {
                                    $ReportDir = $ReportDir + '/'
                                }
                        }
                    catch
                        {
                            Write-Output "ReportDir Parameter must contain the full path."
                            Exit
                        }
                }
            }
        elseif ($PlatOS -eq 'PowerShell Desktop')
            {
                $DefaultPath = if($ReportDir) {$ReportDir} else {"C:\AzureResourceInventory\"}
                $DiagramCache = if($ReportDir) {($ReportDir+'DiagramCache\')} else {"C:\AzureResourceInventory\DiagramCache\"}
                if($ReportDir)
                {
                    try
                        {
                            Resolve-Path $ReportDir -ErrorAction STOP
                            if ($ReportDir -notlike '*\')
                                {
                                    $ReportDir = $ReportDir + '\'
                                }
                        }
                    catch
                        {
                            Write-Output "ReportDir Parameter must contain the full path."
                            Exit
                        }
                }
            }

        Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Checking report folder: ' + $DefaultPath )
        if ((Test-Path -Path $DefaultPath -PathType Container) -eq $false) {
            New-Item -Type Directory -Force -Path $DefaultPath | Out-Null
        }
        if ((Test-Path -Path $DiagramCache -PathType Container) -eq $false) {
            New-Item -Type Directory -Force -Path $DiagramCache | Out-Null
        }

        $ExtractionData = Start-AzureResourceDataPull -ManagementGroup $ManagementGroup -Subscriptions $Subscriptions -SubscriptionID $SubscriptionID -ResourceGroup $ResourceGroup -SecurityCenter $SecurityCenter -SkipAdvisory $SkipAdvisory -SkipPolicy $SkipPolicy -IncludeTags $IncludeTags -QuotaUsage $QuotaUsage -TagKey $TagKey -TagValue $TagValue -Debug $Debug

        $ExtractionRuntime = $ExtractionData.ExtractionRunTime
        $Resources = $ExtractionData.Resources
        $ResourceContainers = $ExtractionData.ResourceContainers
        $Policies = $ExtractionData.Policies
        $Advisories = $ExtractionData.Advisories
        $Security = $ExtractionData.Security

        Clear-Variable -Name ExtractionData

        $ResourcesCount = [string]$Resources.Count
        $advco = [string]$Advisories.Count
        $Secadvco = [string]$Security.Count
        $polco = [string]$Policies.Count

        #### Creating Excel file variable:
        if ($Automation.IsPresent)
            {
                $Date = get-date -Format "yyyy-MM-dd_HH_mm"

                $File = ("ARI_Automation_Report_"+$Date+".xlsx")
            }
        else
            {
                $File = ($DefaultPath + $ReportName + "_Report_" + (get-date -Format "yyyy-MM-dd_HH_mm") + ".xlsx")
                #$Global:DFile = ($DefaultPath + $Global:ReportName + "_Diagram_" + (get-date -Format "yyyy-MM-dd_HH_mm") + ".vsdx")
                $DDFile = ($DefaultPath + $ReportName + "_Diagram_" + (get-date -Format "yyyy-MM-dd_HH_mm") + ".xml")
            }
        Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Excel file: ' + $File)

        Build-AzureResourceReport -Subscriptions $Subscriptions -ExtractionRunTime $ExtractionRuntime -Resources $Resources -ResourceContainers $ResourceContainers -Policies $Policies -Advisories $Advisories -Security $Security -File $File -DDFile $DDFile -RunOnline $RunOnline -Repo $Repo -RawRepo $RawRepo -SkipDiagram $SkipDiagram -RunLite $RunLite -FullEnv $FullEnv -DiagramCache $DiagramCache -PlatOS $PlatOS -InTag $InTag -SkipPolicy $SkipPolicy -SkipAdvisory $SkipAdvisory -Automation $Automation -Debug $Debug

        if ($Automation.IsPresent)
            {
                Set-AzStorageBlobContent -File $File -Container $StorageContainer -Context $StorageContext | Out-Null
            }
    }
}

$Measure = $TotalRunTime.Totalminutes.ToString('#######.##')

Write-Host ('Report Complete. Total Runtime was: ') -NoNewline
Write-Host $Measure -NoNewline -ForegroundColor Cyan
Write-Host (' Minutes')
Write-Host ('Total Resources: ') -NoNewline
Write-Host $ResourcesCount -ForegroundColor Cyan
if (!$SkipAdvisory.IsPresent)
    {
        Write-Host ('Total Advisories: ') -NoNewline
        write-host $advco -ForegroundColor Cyan
    }
if (!$SkipPolicy.IsPresent)
    {
        Write-Host ('Total Policies: ') -NoNewline
        write-host $polco -ForegroundColor Cyan
    }
if ($SecurityCenter.IsPresent)
    {
        Write-Host ('Total Security Advisories: ' + $Secadvco)
    }

Write-Host ''
Write-Host ('Excel file saved at: ') -NoNewline
write-host $File -ForegroundColor Cyan
Write-Host ''

if(!$SkipDiagram.IsPresent)
    {
        Write-Host ('Draw.io Diagram file saved at: ') -NoNewline
        write-host $DDFile -ForegroundColor Cyan
        Write-Host ''
    }
}