如何在 PowerShell 中使用 AzModule 命令创建 AzureProfile.json 文件?

问题描述

我想在 PowerShell 中使用 Az Module 命令创建 AzureProfile.json 文件,这样我就可以在我想登录 Azure 以启动/停止我的 VM 的任何时候导入 json 文件

下面是我用来创建它的命令\脚本,但它给了我错误,如屏幕截图所示。

enter image description here

我也尝试使用下面的代码,但它给出了同样的错误

Connect-AzAccount -UseDeviceAuthentication

Save-AzProfile -path "$PSScriptRoot\AzureProfile.json"

警告:无法为租户“组织”获取令牌 Connect-AzAccount:DeviceCodeCredential 身份验证失败:重试 尝试 4 次后失败。

解决方法

从错误消息来看,在 Login-AzAccount 之后出现的浏览器弹出窗口中,您的帐户身份验证似乎失败。

我运行了第一组命令并成功运行。

Powershell:

enter image description here

创建的文件:

enter image description here

请注意,我的PowerShell 版本5.7.0.18831

,

你都试过了:

  • Connect-AzAccount -UseDeviceAuthentication
  • 登录-AzAccount

交互式和非交互式登录模式。

尝试与 azure 通信时发生错误。我怀疑这可能是机器级别或网络级别(代理/防火墙)的问题

enter image description here

当 Microsoft 服务出现连接问题时,通常会发生上述问题(可在我这边重现) - 被代理 /firewall/GPO 等阻止...

为了快速测试,您可以运行以下 PowerShell 命令:

Invoke-WebRequest -Uri "https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode" -Body "client_id=1950a258-227b-4e31-a9cf-717495945fc2&scope=https%3A%2F%2Fmanagement.core.windows.net%2F%2F.default+offline_access+profile+openid" -Method Post

示例成功响应:

enter image description here

为什么是上面的命令?

当您通过 powershell 使用 https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode 时,会命中上述端点 Connect-AzAccount -UseDeviceAuthentication

enter image description here

如果您遇到超时/名称无法解决的问题 - 问题在于网络/机器配置。