azure - 如何使用服务主体连接到 Azure?

标签 azure powershell powershell-az-module

我正在尝试通过本地 Azure DevOps 以非交互方式连接到 Azure 以应用 SQL 迁移。我在 Azure AD 中创建了一个应用程序注册,并带有相应的客户端 key 。

当我运行时

$appId = {Guid}
$clientSecret = {secret value}
$securePassword = ConvertTo-SecureString -String $clientSecret -AsPlainText -Force
$tenantId = {Guid}
$credential = New-Object -TypeName System.Management.Automation.PSCredential @{ UserName = $appId; Password = $securePassword }
Connect-AzAccount -ServicePrincipal -TenantId $tenantId -Credential $credential

根据https://learn.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-10.2.0处的文档(参见示例 3),我收到意外的错误消息:

No certificate thumbprint or secret provided for the given service principal ''. Could not find tenant id for provided tenant domain '{Guid}'. Please ensure that the provided service principal '' is found in the provided tenant domain.

我找不到有关此错误消息的任何信息,它似乎与我的参数/参数不一致。

如何修复此问题以获得有效连接? (我可以确认我正在运行 Az 版本 10.2.0 和 PowerShell 7.3.5。)

最佳答案

您的 $credential 变量为空,因为您错误地将用户名和密码作为哈希表提供。

PSCredential 需要 2 个字符串值(用户名和密码),这些值可以作为空格分隔值或数组传递。

$credential = New-Object -TypeName System.Management.Automation.PSCredential $appId, $securePassword

$credential = New-Object -TypeName System.Management.Automation.PSCredential @($appId, $securePassword)

关于azure - 如何使用服务主体连接到 Azure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76979931/

相关文章:

azure - 通过来自 terraform 的 VM 扩展自定义脚本安装 powershell Az.Storage 模块

.net - 创建 Azure IoT 中心设备

azure - 对 C# 和 Azure 应用服务中的 CORS 设置之间的差异感到困惑

azure - 将一对多批量导入到 DocumentDB 集合中

powershell - 如何从PowerShell函数即DataTable返回特定的数据类型

powershell - 在 PowerShell 中更改第一个输入单词的颜色

Azure CLI - 设置 Azure 应用程序网关后端设置

Azure AD - 阻止 guest 访问 Azure 网站

azure - 获取-AzDenyAssignment : 'directoryObjectId' cannot be null

Azure PowerShell::如何打印属性列表