Modules/Core/ARILoginSession.psm1

<#
.Synopsis
Azure Login Session Module for Azure Resource Inventory
 
.DESCRIPTION
This module is used to invoke the authentication process that is handle by the Azure CLI.
 
.Link
https://github.com/microsoft/ARI/Core/Connect-LoginSession.psm1
 
.COMPONENT
This powershell Module is part of Azure Resource Inventory (ARI)
 
.NOTES
Version: 4.0.1
First Release Date: 15th Oct, 2024
Authors: Claudio Merola
 
#>

function Connect-ARILoginSession {
    Param($AzureEnvironment,$TenantID,$SubscriptionID,$DeviceLogin)
    Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting Connect-LoginSession function')
    Write-Host $AzureEnvironment -BackgroundColor Green
    if (!$TenantID) {
        write-host "Tenant ID not specified. Use -TenantID parameter if you want to specify directly. "
        write-host "Authenticating Azure"
        write-host ""
        Clear-AzContext -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
        if($DeviceLogin.IsPresent)
            {
                Connect-AzAccount -UseDeviceAuthentication -Environment $AzureEnvironment -WarningAction SilentlyContinue -InformationAction SilentlyContinue
            }
        else
            {
                try 
                    {
                        $AZConfigNewLogin = Get-AzConfig -LoginExperienceV2
                        if ($AZConfigNewLogin.value -eq 'On' )
                            {
                                Update-AzConfig -LoginExperienceV2 Off
                                Connect-AzAccount -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                                Update-AzConfig -LoginExperienceV2 On
                            }
                        else
                            {
                                Connect-AzAccount -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                            }
                    }
                catch
                    {
                        Connect-AzAccount -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                    }
            }
        write-host ""
        write-host ""
        $Tenants = Get-AzTenant -WarningAction SilentlyContinue -InformationAction SilentlyContinue | Sort-Object -Unique
        if ($Tenants.Count -eq 1) {
            write-host "You have privileges only in One Tenant "
            write-host ""
            $TenantID = $Tenants.Id
        }
        else {
            write-host "Select the the Azure Tenant ID that you want to connect : "
            write-host ""
            $SequenceID = 1
            foreach ($Tenant in $Tenants) {
                $TenantName = $Tenant.name
                write-host "$SequenceID) $TenantName"
                $SequenceID ++
            }
            write-host ""
            [int]$SelectTenant = read-host "Select Tenant ( default 1 )"
            $defaultTenant = --$SelectTenant
            $TenantID = ($Tenants[$defaultTenant]).Id
            if($DeviceLogin.IsPresent)
                {
                    Connect-AzAccount -Tenant $TenantID -UseDeviceAuthentication -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                }
            else
                {
                    Connect-AzAccount -Tenant $TenantID -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                }
        }
    }
    else {
        Clear-AzContext -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
        if($DeviceLogin.IsPresent)
            {
                Connect-AzAccount -Tenant $TenantID -UseDeviceAuthentication -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
            }
        else
            {
                try 
                    {
                        $AZConfig = Get-AzConfig -LoginExperienceV2
                        if ($AZConfig.value -eq 'On')
                            {
                                Update-AzConfig -LoginExperienceV2 Off -WarningAction SilentlyContinue -InformationAction SilentlyContinue
                                Connect-AzAccount -Tenant $TenantID -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                                Update-AzConfig -LoginExperienceV2 On -WarningAction SilentlyContinue -InformationAction SilentlyContinue
                            }
                        else
                            {
                                Connect-AzAccount -Tenant $TenantID -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                            }
                    }
                catch
                    {
                        Connect-AzAccount -Tenant $TenantID -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Environment $AzureEnvironment
                    }
            }
    }
    return $TenantID
}