如何使用 PowerShell 连接 Azure 帐户?

powershellmicrosoft technologiessoftware & coding

要使用 PowerShell 连接 Azure 帐户,我们可以使用 Connect-AZAccount 命令。如果我们检查以下 URL 中的命令参数,会发现有多种方法可以连接到 Azure 帐户,但在本文中,我们将使用简单的方法进行连接。

  • 使用交互式控制台连接门户

  • 使用 DeviceLogin 方法。

  • 使用 Credentials 方法。

使用交互式控制台方法连接门户。

当我们直接使用 Connect-AZAccount 而不使用任何参数时,它会打开一个 Azure 门户凭据的弹出窗口。

您需要在此处输入您的 Azure 凭据。

使用设备登录方法。

在此方法中,Connect-AZAccount 使用参数 -DeviceLogin。运行该命令后,控制台窗口将显示验证码,以及我们需要在网站上输入的验证码。

示例

Connect-AzAccount -DeviceCode

输出

WARNING: To sign in, use a web browser to open the page
https://microsoft.com/devicelogin and enter the code SAYX4512 to authenticate.

如上输出所示,您需要在网站 https://Microsoft.com/DeviceLogin 上输入显示代码。如果您尚未通过身份验证,则需要在那里输入 Azure 凭据。

使用凭据方法。

您可以在 Get-Credentials 命令中直接提供 Azure 用户凭据,并在 Connect-AZAccount 中使用这些凭据。

示例

$creds = Get-Credential
Connect-AZAccount -Credential $creds

输入正确的凭据后,您的帐户将被连接。但如果您的组织已配置多重身份验证,则此方法将不起作用,并且可能会产生错误。

输出


相关文章