Azure Runbook运行Sql查询:目标主体名称不正确。无法生成SSPI上下文。”

时间:2020-10-27 16:43:54

标签: sql sql-server azure powershell azure-runbook

嗨,我正在尝试使用Azure Runbook在我的一个数据库上运行SQL查询。我的设置在下面列出。测试此错误时,出现错误The target principal name is incorrect. Cannot generate SSPI context.",我四处张望,但未发现与Azure安装有关的任何内容。有人有什么想法吗?

$myCred = Get-AutomationPSCredential -Name 'my-cred'
$userName = $myCred.UserName
$securePassword = $myCred.Password
$password = $myCred.GetNetworkCredential().Password

#Query to execute
# Invoke to Azure SQL DB
invoke-sqlcmd2 -ServerInstance $ServerName -Database $DatabaseName -Credential $SQLServerCred -Query $SqlQuery -Encrypt

1 个答案:

答案 0 :(得分:1)

如评论中所述,看来您之前没有设置$SQLServerCred

尝试:

$myCred = Get-AutomationPSCredential -Name 'my-cred'
$userName = $myCred.UserName
$securePassword = $myCred.Password
$SQLServerCred = New-Object System.Management.Automation.PSCredential($userName , $securePassword)

#Query to execute
# Invoke to Azure SQL DB
invoke-sqlcmd2 -ServerInstance $ServerName -Database $DatabaseName -Credential $SQLServerCred -Query $SqlQuery -Encrypt