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

时间:2019-02-26 17:03:18

标签: azure powershell-core

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

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

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

有什么方法可以避免此确认?

我也尝试使用以下命令进行操作:

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

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

3 个答案:

答案 0 :(得分:1)

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

enter image description here

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 

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

如果您不熟悉服务主体,另请参见:How to: Use the portal to create an Azure AD application and service principal that can access resourcesapplication id and authentication key是您需要的Azure AD Application Idstrong password

答案 1 :(得分:0)

您有2个选择。

使用凭据登录(需要Az.Accounts v 1.2.0或更高版本)

您还可以使用被授权连接到Azure的PSCredential对象登录。获取凭据对象的最简单方法是使用Get-Credential cmdlet。运行时,此cmdlet将提示您输入用户名/密码凭据对。

$creds = Get-Credential
Connect-AzAccount -Credential $creds

使用服务负责人登录

服务主体是非交互式Azure帐户。与其他用户帐户一样,其权限是通过Azure Active Directory管理的。通过仅授予服务主体所需的权限,您的自动化脚本将保持安全。

要了解如何创建用于Azure PowerShell的服务主体,请参见Create an Azure service principal with Azure PowerShell

来源:https://docs.microsoft.com/en-us/powershell/azure/authenticate-azureps?view=azps-1.3.0

答案 2 :(得分:0)

如果启用多因素,那么下面的逻辑也应该工作

    $clientId = "***********************"
    $clientSecret = "********************"
    $tenantId = "***********************"
    $tempPassword = ConvertTo-SecureString "$clientSecret" -AsPlainText -Force
    $psCred = New-Object System.Management.Automation.PSCredential($clientId , 
    $tempPassword)
    Connect-AzAccount -Credential $psCred -TenantId $azureTenantId  -ServicePrincipal