@@ -97,71 +97,70 @@ function Connect-ToAzureCliOidc
9797 _LogMessage - Level ' INFO' - Message ' OIDC login OK.' - InvocationName $MyInvocation.MyCommand.Name
9898}
9999
100- function Connect-ToAzureCliDeviceCode
101- {
100+ function Connect-ToAzureCliDeviceCode {
102101 param (
103102 [string ]$TenantId ,
104103 [string ]$SubscriptionId
105104 )
106105
107- # ── 1. Is the CLI already logged in? ────────────────────────────────────
108- $currentId = az account show -- query id - o tsv 2> $null
109- if ($LASTEXITCODE -eq 0 -and $currentId )
110- {
111- _LogMessage - Level ' INFO' - Message " Azure CLI already authenticated (subscription id: $currentId ) – skipping device-code login." - InvocationName $MyInvocation.MyCommand.Name
106+ $invocation = $MyInvocation.MyCommand.Name
112107
113- # caller may still want to switch subscription
114- if ($SubscriptionId )
115- {
116- az account set -- subscription $SubscriptionId
117- _LogMessage - Level ' DEBUG' - Message " az account set exit-code: $LASTEXITCODE " - InvocationName $MyInvocation.MyCommand.Name
118- if ($LASTEXITCODE -ne 0 )
119- {
120- _LogMessage - Level ' WARN' - Message " Unable to switch to subscription $SubscriptionId ." - InvocationName $MyInvocation.MyCommand.Name
108+ try {
109+ # ── Check if already logged in and with correct tenant/sub ──
110+ $accountInfo = az account show -- output json | ConvertFrom-Json
111+
112+ if ($accountInfo -and $accountInfo.id ) {
113+ $currentSubId = $accountInfo.id
114+ $currentTenant = $accountInfo.tenantId
115+
116+ $isSubMatch = -not $SubscriptionId -or ($SubscriptionId -eq $currentSubId )
117+ $isTenantMatch = -not $TenantId -or ($TenantId -eq $currentTenant )
118+
119+ if ($isSubMatch -and $isTenantMatch ) {
120+ _LogMessage - Level ' INFO' - Message " Azure CLI already authenticated with correct subscription and tenant (sub: $currentSubId , tenant: $currentTenant ) – skipping login." - InvocationName $invocation
121+ return
122+ }
123+
124+ if (-not $isSubMatch -and $SubscriptionId ) {
125+ _LogMessage - Level ' INFO' - Message " Switching subscription to $SubscriptionId ..." - InvocationName $invocation
126+ az account set -- subscription $SubscriptionId
127+ if ($LASTEXITCODE -ne 0 ) {
128+ _LogMessage - Level ' WARN' - Message " Unable to switch to subscription $SubscriptionId ." - InvocationName $invocation
129+ }
130+ return
121131 }
122132 }
123- return
124- }
125133
126- # ── 2. Perform interactive login ───────────────────────────────────────
127- try
128- {
129- _LogMessage - Level ' INFO' - Message ' Azure CLI device-code login…' - InvocationName $MyInvocation.MyCommand.Name
134+ # ── Perform interactive login ──
135+ _LogMessage - Level ' INFO' - Message ' Azure CLI device-code login…' - InvocationName $invocation
130136
131- if ($TenantId )
132- {
137+ if ($TenantId ) {
133138 az login -- use-device - code -- tenant $TenantId -- allow- no- subscriptions
134- }
135- else
136- {
139+ } else {
137140 az login -- use-device - code -- allow- no- subscriptions
138141 }
139- _LogMessage - Level ' DEBUG' - Message " az login exit-code: $LASTEXITCODE " - InvocationName $MyInvocation.MyCommand.Name
140- if ($LASTEXITCODE -ne 0 )
141- {
142+
143+ if ($LASTEXITCODE -ne 0 ) {
142144 throw ' az login failed (device-code).'
143145 }
144146
145- if ($SubscriptionId )
146- {
147+ if ($SubscriptionId ) {
147148 az account set -- subscription $SubscriptionId
148- _LogMessage - Level ' DEBUG' - Message " az account set exit-code: $LASTEXITCODE " - InvocationName $MyInvocation.MyCommand.Name
149- if ($LASTEXITCODE -ne 0 )
150- {
149+ if ($LASTEXITCODE -ne 0 ) {
151150 throw " Unable to set subscription $SubscriptionId ."
152151 }
153152 }
154153
155- _LogMessage - Level ' INFO' - Message ' Device-code login OK.' - InvocationName $MyInvocation .MyCommand.Name
154+ _LogMessage - Level ' INFO' - Message ' Device-code login OK.' - InvocationName $invocation
156155 }
157- catch
158- {
159- _LogMessage - Level ' ERROR' - Message " Device-code login failed: $ ( $_.Exception.Message ) " - InvocationName $MyInvocation.MyCommand.Name
156+ catch {
157+ _LogMessage - Level ' ERROR' - Message " Device-code login failed: $ ( $_.Exception.Message ) " - InvocationName $invocation
160158 throw
161159 }
162160}
163161
164162
163+
165164function Test-AzureCliConnection
166165{
167166 try
0 commit comments