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-LoginSession { Param($AzureEnvironment,$TenantID,$SubscriptionID,$DeviceLogin) Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Starting Connect-LoginSession function') if(![string]::IsNullOrEmpty($AzureEnvironment)) { az cloud set --name $AzureEnvironment } $CloudEnv = az cloud list | ConvertFrom-Json Write-Host "Azure Cloud Environment: " -NoNewline $CurrentCloudEnvName = $CloudEnv | Where-Object {$_.isActive -eq 'True'} Write-Host $CurrentCloudEnvName.name -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 "" Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Cleaning az account cache') az account clear | Out-Null Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Calling az login') if($DeviceLogin.IsPresent) { az login --use-device-code } else { az login --only-show-errors | Out-Null } write-host "" write-host "" $Tenants = az account list --query [].homeTenantId -o tsv --only-show-errors | Sort-Object -Unique Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Checking number of Tenants') if ($Tenants.Count -eq 1) { write-host "You have privileges only in One Tenant " write-host "" $TenantID = $Tenants } else { write-host "Select the the Azure Tenant ID that you want to connect : " write-host "" $SequenceID = 1 foreach ($TenantID in $Tenants) { write-host "$SequenceID) $TenantID" $SequenceID ++ } write-host "" [int]$SelectTenant = read-host "Select Tenant ( default 1 )" $defaultTenant = --$SelectTenant $TenantID = $Tenants[$defaultTenant] if($DeviceLogin.IsPresent) { az login --use-device-code -t $TenantID } else { az login -t $TenantID --only-show-errors | Out-Null } } write-host "Extracting from Tenant $TenantID" Write-Debug ((get-date -Format 'yyyy-MM-dd_HH_mm_ss')+' - '+'Extracting Subscription details') $Script:Subscriptions = az account list --output json --only-show-errors | ConvertFrom-Json $Script:Subscriptions = $Subscriptions | Where-Object { $_.tenantID -eq $TenantID } if ($SubscriptionID) { if($SubscriptionID.count -gt 1) { $Script:Subscriptions = $Subscriptions | Where-Object { $_.ID -in $SubscriptionID } } else { $Script:Subscriptions = $Subscriptions | Where-Object { $_.ID -eq $SubscriptionID } } } } else { az account clear | Out-Null if($DeviceLogin.IsPresent) { az login --use-device-code -t $TenantID } else { $AZConfig = az config get core.enable_broker_on_windows --only-show-errors | ConvertFrom-Json if ($AZConfig.value -eq $true) { az config set core.enable_broker_on_windows=false --only-show-errors #az config set core.login_experience_v2=off --only-show-errors az login -t $TenantID --only-show-errors az config set core.enable_broker_on_windows=true --only-show-errors } else { az login -t $TenantID --only-show-errors } } $Script:Subscriptions = az account list --output json | ConvertFrom-Json $Script:Subscriptions = $Subscriptions | Where-Object { $_.tenantID -eq $TenantID } if ($SubscriptionID) { if($SubscriptionID.count -gt 1) { $Script:Subscriptions = $Subscriptions | Where-Object { $_.ID -in $SubscriptionID } } else { $Script:Subscriptions = $Subscriptions | Where-Object { $_.ID -eq $SubscriptionID } } } } } |