Login-AzAccount
、Add-AzAccount
或者Connect-AzAccount,如果你在使用PowerShell连接到Azure订阅,似乎有很多不同的认证方式!
当迷茫时,使用Connect-AzAccount
当我刚开始在PowerShell中使用Azure时,我感到非常困惑。我会看到一些文章谈论使用Login-AzAccount
,而其他一些提到了Add-AzAccount
,但很少有人提到Connect-AzAccount
。在什么情况下我应该使用哪个?到底发生了什么?!
I’m here to tell you if you’re struggling with the same problem I was, the solution is a lot easier than you might expect. Although there may seem to be three different commands to authenticate to Azure with PowerShell, in reality, there’s only one. It is Connect-AzAccount
. Both Login-AzAccount
and Add-AzAccount
are only aliases to the Connect
command.
I’m here to tell you to just use Connect-AzAccount and you’ll be good as gold. I don’t recommend using aliases, if possible just because it makes things confusing as you can vouch for. With people using different ways to accomplish the same task, it’s hard to figure out what exactly is happening.
随时可以通过使用Get-Alias
来验证我。

使用Connect-AzAccount进行身份验证
有很多种使用Connect-AzAccount
进行Azure身份验证的方式。这取决于您要进行身份验证的资源。例如,有大约五种不同的方式可以对Azure进行身份验证。
- 交互式
- 使用服务主体
- 使用Azure托管的服务标识
- 作为云解决方案提供商(CSP)
- 进入非公共云
通过交互式登录
刚开始使用Azure的人们最常见的方式是进行交互式连接。这意味着,他们将运行Connect-AzAccount
,然后会提示输入凭据。
如果你拥有Microsoft或组织的Office 365账户,并且不需要自动化任务,这种方法就可以使用。
使用服务主体登录
您也可以使用服务主体进行身份验证。如果您需要在自动化脚本中进行身份验证,这与托管服务身份识别一起使用是可行的方式。但是,这需要预先创建一个Azure Active Directory应用程序以及服务主体本身,需要一些设置。要了解如何进行设置的详细概述,请查看这个名为如何使用PowerShell创建和验证Azure服务主体的TechSnips视频。它涵盖了您需要进行设置的所有步骤。
使用服务主体进行身份验证将强制您使用Connect-AzAccount
Credential
和ServicePrincipal
参数。
ServicePrincipal
参数表示此帐户通过提供服务主体凭据进行身份验证。Credential
参数指定了一个PSCredential对象。
使用托管服务标识登录
另一种方法是使用托管服务标识,说实话,我以前从未做过。我在本节提供了一个链接,以便了解详情。与Connect-AzAccount
一起使用的一些命令在使用托管服务标识进行身份验证时包括:
ManagedServiceHostName
– 托管服务登录的主机名ManagedServicePort
– 托管服务登录的端口号ManagedServiceSecret
– 用于某些类型的托管服务登录的秘密。
作为云解决方案提供商 (CSP) 登录
如果贵公司是微软合作伙伴,并直接向客户提供 Azure 服务,则可以使用Connect-AzAccount
并使用TenantId
参数。这是为了指定不同的 Azure AD 租户而必需的。
登录到非公共云
最后,虽然不太常见,但有能力进行身份验证到非公共云,比如政府或国家云。这些云通过在Connect-AzAccount
上使用环境参数来表示 Azure 环境。如果不知道环境名称,您可以随时使用Get-AzEnvironment
命令。

总结
你可以看到,有很多不同的方法可以用来验证到 Azure,因为 Azure 是一个庞大的服务!使用 PowerShell 的 Connect-AzAccount
,你可以提供 Azure 交互式或非交互式处理凭据所需的所有必要参数,从而使你能够开始使用!