你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure PowerShell 登录

Azure PowerShell 支持多种身份验证方法。 最简单的入门方法是使用 Azure Cloud Shell 自动登录。 通过本地安装,可以通过浏览器以交互方式登录。 编写自动化脚本时,建议的方法是使用具有必要权限 的服务主体 。 尽可能限制用例的登录权限时,有助于保护 Azure 资源的安全。

登录后,将针对默认订阅运行命令。 若要更改会话的活动订阅,请使用 Set-AzContext cmdlet。 若要更改使用 Azure PowerShell 登录时使用的默认订阅,请使用 Set-AzDefault

重要

只要保持登录状态,凭据就可在多个 PowerShell 会话之间共享。 有关详细信息,请参阅有关 持久凭据的文章。

以交互方式登录

若要以交互方式登录,请使用 Connect-AzAccount cmdlet。

Connect-AzAccount

运行时,此 cmdlet 将显示令牌字符串。 若要登录,请复制此字符串并将其粘贴到 https://microsoft.com/devicelogin 浏览器中。 PowerShell 会话将进行身份验证以连接到 Azure。

重要

由于 Active Directory 授权实现和安全问题发生更改,Azure PowerShell 中删除了用户名/密码凭据授权。 如果出于自动化目的使用凭据授权,请改为 创建服务主体

使用服务主体进行登录

服务主体属于非交互式 Azure 帐户。 与其他用户帐户一样,其权限使用 Microsoft Entra ID 进行管理。 通过仅向服务主体授予它所需的权限,自动化脚本将保持安全。

若要了解如何创建用于 Azure PowerShell 的服务主体,请参阅 使用 Azure PowerShell 创建 Azure 服务主体

若要使用服务主体进行登录,请将 -ServicePrincipal 参数与 Connect-AzAccount cmdlet 一起使用。 还需要服务主体的应用程序 ID、登录凭据和与服务主体关联的租户 ID。 使用服务主体登录的方式取决于是针对基于密码的身份验证还是基于证书的身份验证进行配置。

基于密码的身份验证

若要获取用作相应对象的服务主体凭据,请使用 Get-Credential cmdlet。 此 cmdlet 将提示输入用户名和密码。 请使用服务主体 ID 作为用户名。

$pscredential = Get-Credential
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

对于自动化方案,需要从用户名和安全字符串创建凭据:

$passwd = ConvertTo-SecureString <use a secure password here> -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential('service principal name/id', $passwd)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $tenantId

当自动执行服务主体连接时,请确保使用良好的密码存储做法。

基于证书的身份验证

基于证书的身份验证要求 Azure PowerShell 可以根据证书指纹从本地证书存储中检索信息。

Connect-AzAccount -ApplicationId $appId -Tenant $tenantId -CertificateThumbprint <thumbprint>

使用服务主体而不是已注册的应用程序时,添加 -ServicePrincipal 参数并提供服务主体的应用程序 ID 作为 -ApplicationId 参数的值。

Connect-AzAccount -ServicePrincipal -ApplicationId $servicePrincipalId -Tenant $tenantId -CertificateThumbprint <thumbprint>

在 PowerShell 5.1 中,可以使用 PKI 模块管理并检查证书存储。 对于 PowerShell Core 6.x 及更高版本,此过程更为复杂。 以下脚本演示如何将现有证书导入到 PowerShell 可访问的证书存储中。

在 PowerShell 5.1 中导入证书

# Import a PFX
$credentials = Get-Credential -Message "Provide PFX private key password"
Import-PfxCertificate -FilePath <path to certificate> -Password $credentials.Password -CertStoreLocation cert:\CurrentUser\My

在 PowerShell Core 6.x 及更高版本中导入证书

# Import a PFX
$storeName = [System.Security.Cryptography.X509Certificates.StoreName]::My
$storeLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
$store = [System.Security.Cryptography.X509Certificates.X509Store]::new($storeName, $storeLocation)
$certPath = <path to certificate>
$credentials = Get-Credential -Message "Provide PFX private key password"
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new($certPath, $credentials.Password, $flag)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$store.Add($Certificate)
$store.Close()

使用托管标识登录

托管标识是 Microsoft Entra ID 的一项功能。 托管标识是分配给 Azure 中运行的资源的服务主体。 可以使用托管标识服务主体登录,并获取仅限应用的访问令牌来访问其他资源。 托管标识仅适用于在 Azure 云中运行的资源。

此命令使用主机环境的托管标识进行连接。 例如,如果使用分配的托管服务标识在 VirtualMachine 上执行,则允许代码使用该分配的标识登录。

 Connect-AzAccount -Identity

使用非默认租户或云解决方案提供商(CSP)登录

如果帐户与多个租户相关联,则登录需要在连接时使用 -Tenant 参数。 此参数将适用于任何登录方法。 登录时,此参数值可以是租户的 Azure 对象 ID(租户 ID)或租户的完全限定域名。

如果你是云解决方案提供商(CSP),该值-Tenant必须是租户 ID。

Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx'

登录到其他云

Azure 云服务提供符合区域数据处理法的环境。 对于区域云中的帐户,请在登录时使用 -Environment 参数设置环境。 此参数将适用于任何登录方法。 例如,如果帐户位于中国云中:

Connect-AzAccount -Environment AzureChinaCloud

以下命令获取可用环境的列表:

Get-AzEnvironment | Select-Object Name