Azure / Azure/azure-powershell

Retrieving list of tenants using Get-AzTenant when using a serviceprincipal

Open
#24,730 2 comments 0 reactions 0 assignees View on GitHub
customer-reported needs-triage question
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

### Description

When logging in interactively Get-AzTenant returns a list of tenants to which the respective logon has access (although it may not currently be logged into the other tenants). When doing the same when connected using a service principal with the same permissions and list of tenants to which it is a member it only returns the current tenant connected with. If you connect to a number of tenants using the service principal you still only see a single tenant when calling Get-AzTenant.

I'm not 100% sure if this is a bug, so am asking this as a question as to how you can retrieve the list of tenants?

### Script or Debug output

```PowerShell
Connect-AzAccount -TenantId aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
Get-AzTenant
{returns list of tenants}

$appId = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
$tenantId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
$secret= ConvertTo-SecureString "cccccccccccccccccccccccccccccccccccccccc" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($appId, $secret)
Connect-AzAccount -Credential $credentials -TenantId $tenantId -ServicePrincipal
Get-AzTenant
{returns single tenant}
```

### Environment data

```PowerShell
PS C:\Users\ChrisV> $PSVersionTable

Name Value
---- -----
PSVersion 7.4.2
PSEdition Core
GitCommitId 7.4.2
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
```

### Module versions

```PowerShell
PS C:\Users\ChrisV> Get-Module Az.Accounts

ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.17.0 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzConte…
```

### Error output

```PowerShell
No error output
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.