Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - Connect-AzAccount - how to avoid azure device authentication?

I have installed the PowerShell 6.1.3 version and I want to get a connection to the Azure account using the following Azure PowerShell command:

Connect-AzAccount -Tenant <tenantId> -Subscription <subId>

After entering this command I get the warning with the url and some code. Then I have to go to the URL and enter the code there. After that, I get a connection to the Azure account.

Are there any ways to avoid this confirmation?

I've also tried to do it using the following command:

az login -u <username> -p <password>

This command only returns some account information(subscriptionId, tenantId etc) but it doesn't install a connection to this account.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

1.To login with the user account, try the command as below, make sure your account doesn't enable the MFA(Multi-Factor Authentication).

$User = "[email protected]"
$PWord = ConvertTo-SecureString -String "<Password>" -AsPlainText -Force
$tenant = "<tenant id>"
$subscription = "<subscription id>"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
Connect-AzAccount -Credential $Credential -Tenant $tenant -Subscription $subscription

enter image description here

2.You can also use a service principal to login, use the command as below.

$azureAplicationId ="Azure AD Application Id"
$azureTenantId= "Your Tenant Id"
$azurePassword = ConvertTo-SecureString "strong password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($azureAplicationId , $azurePassword)
Connect-AzAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal 

See a similar issue I answered here, it use the old AzureRM module, for Az, just change the last line.

If you are not familiar with service principal, Also see : How to: Use the portal to create an Azure AD application and service principal that can access resources, the application id and authentication key are the Azure AD Application Id and strong password you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...