首页 > 解决方案 > 如何在 Powershell Core 中连接-AzAccount(无提示)?

问题描述

我确实看到了这个问题:Connect-AzAccount without prompt

但是当我尝试接受的答案时,我收到以下错误:

[6/12/2020 12:36:20 AM] ERROR: Connect-AzAccount : Username + Password authentication is not supported in PowerShell Core. Please use device code authentication for interactive log in, or Service Principal authentication for script log in.

所以我转到了 Connect-AzAccount 文档的示例 3,它指定了“服务主体”身份验证方法,因此我将两者混合,因为建议的 vanillaGet-Credential会触发另一个交互式会话。所以现在是脚本:

    $User = "myemail@gmail.com"
    $PWord = ConvertTo-SecureString -String "**********" -AsPlainText -Force
    $tenant = "f*********************************"
    $Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User,$PWord
    # $Credential = Get-Credential
    Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal

这带来了我的下一个错误:[6/12/2020 12:45:45 AM] ERROR: Connect-AzAccount : AADSTS700016: Application with identifier 'myemail' was not found in the directory 'f*********************************'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant.

在这一点上我真的很困惑,因为我在 Azure 中所做的只是:

  1. 创建新的 Azure 帐户
  2. 通过 UI 预配 Azure API 管理实例(顺便说一句,大约需要 20 分钟)
  3. 尝试使用上面的代码在本地连接到 Powershell Azure 函数内的 Azure。

我认为我提供的信息或我的配置方式有问题。

$User是我注册 Azure 时使用的电子邮件。

$PWord是我的 Azure 密码

$tenant是我打开 Azure AD 时看到的第一件事:

在此处输入图像描述

我尝试通过 Powershell Core 连接到 Azure 的方式有什么问题?

标签: azurepowershellazure-active-directoryazure-powershellazure-api-management

解决方案


基于示例 3,它要求输入您的应用程序 ID 作为用户名和服务主体机密作为密码。

因此,您首先需要创建一个服务主体。然后使用其应用程序 ID 和客户端密码作为凭证。

$User = "{application id}"
$PWord = ConvertTo-SecureString -String "{client secret}" -AsPlainText -Force

推荐阅读