Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.24k stars 3.84k forks source link

Unable to login to User Assigned Identities in the Az Powershell in the local system/ powersh runbook #20480

Open ANVLISTENER opened 1 year ago

ANVLISTENER commented 1 year ago

Description

Hello Team ,

I tried the following code and getting the error.

Connect-AzAccount -Identity -AccountId $identity.ClientId WARNING: Unable to acquire token for tenant 'organizations' with error 'ManagedIdentityCredential authentication unavailable. Multiple attempts failed to obtain a token from the managed identity endpoint.'

I tried from the Ref #: https://learn.microsoft.com/en-us/azure/automation/migrate-run-as-accounts-managed-identity?tabs=ua-managed-identity

I want to login to the Automation account and access the system's and resources. I gave the access in the IAM role in the Automation. Despite doing so, it desperately fails.

request you to help me find a way to work on both localsystem and the runbook as should be.

additionally, let me know if you get me a way to get the list systems ( VMs) available / linked to the automation account.

Humble Thanks.

Thank you.

Reagrds ,

Libin

Script or Debug output

try
{ 

    "Logging in to Azure..." 

$identity = Get-AzUserAssignedIdentity -ResourceGroupName <myResourceGroup> -Name <myUserAssignedIdentity> 
Connect-AzAccount -Identity -AccountId $identity.ClientId 
} 
catch { 
    Write-Error -Message $_.Exception 
    throw $_.Exception 
} 
#Get all Resource Manager resources from all resource groups 
$ResourceGroups = Get-AzResourceGroup 
foreach ($ResourceGroup in $ResourceGroups) 
{     
    Write-Output ("Showing resources in resource group " + $ResourceGroup.ResourceGroupName) 
    $Resources = Get-AzResource -ResourceGroupName $ResourceGroup.ResourceGroupName 
    foreach ($Resource in $Resources) 
    { 
        Write-Output ($Resource.Name + " of type " +  $Resource.ResourceType) 
    } 
    Write-Output ("") 
}

Ref#: https://learn.microsoft.com/en-us/azure/automation/migrate-run-as-accounts-managed-identity?tabs=ua-managed-identity

Under the section : User Assigned Identity.

Environment data

PS> $PSVersionTable

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

Module versions

PS> Get-Module Az*

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.10.4                Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script     1.8.0                 Az.Automation                       {Export-AzAutomationDscConfiguration, Export-AzAutomationDscNodeReportContent, Export-AzAutomationRun…
Script     5.2.0                 Az.Compute                          {Add-AzImageDataDisk, Add-AzVhd, Add-AzVMAdditionalUnattendContent, Add-AzVMDataDisk…}
Script     1.1.0                 Az.ManagedServiceIdentity           {Get-AzFederatedIdentityCredentials, Get-AzSystemAssignedIdentity, Get-AzUserAssignedIdentity, Get-Az…

Error output

No response

ANVLISTENER commented 1 year ago
Capture
ANVLISTENER commented 1 year ago

The above image shows the access given to the account.

ANVLISTENER commented 1 year ago

$identity = Get-AzUserAssignedIdentity -ResourceGroupName "test" -Name "RunAsAccount"

$account=Set-AzAutomationAccount -ResourceGroupName "TEST" -Name "testing"

From here, we got the $identity.ClientId in the below.

Connect-AzAccount -Identity -AccountId $identity.ClientId

ANVLISTENER commented 1 year ago

But this fully fails, i don't know y this happens.