Connect-AzAccount - 如何避免 Azure 设备身份验证?

aga*_*a17 8 powershell azure powershell-core

我已经安装了 PowerShell 6.1.3 版本,我想使用以下 Azure PowerShell 命令连接到 Azure 帐户:

Connect-AzAccount -Tenant <tenantId> -Subscription <subId>
Run Code Online (Sandbox Code Playgroud)

输入此命令后,我收到带有 url 和一些代码的警告。然后我必须转到 URL 并在那里输入代码。之后,我连接到 Azure 帐户。

有没有办法避免这种确认?

我也尝试使用以下命令来做到这一点:

az login -u <username> -p <password>
Run Code Online (Sandbox Code Playgroud)

此命令仅返回一些帐户信息(subscriptionId、tenantId 等),但不会安装到此帐户的连接。

Joy*_*ang 15

1.要使用用户帐户登录,请尝试以下命令,确保您的帐户未启用 MFA(多重身份验证)。

$User = "xxx@xxxx.onmicrosoft.com"
$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
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

2.您也可以使用服务主体登录,使用如下命令。

$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 
Run Code Online (Sandbox Code Playgroud)

看到我在这里回答的类似问题,它使用旧AzureRM模块,for Az,只需更改最后一行。

如果你不熟悉的服务主体,另请参见:如何:使用门户网站创建Azure的AD应用程序和服务主体可以访问的资源,在应用程序ID和认证Azure AD Application Idstrong password你的需要。

  • 我刚刚检查了您的第一个选项并收到以下错误:Connect-AzAccount:PowerShell Core 不支持用户名 + 密码身份验证。请使用设备代码身份验证进行交互式登录,或使用服务主体身份验证进行脚本登录。 (3认同)