Search code examples
sqlsql-serverazurepowershellazure-runbook

Azure Runbook Running Sql Query: The target principal name is incorrect. Cannot generate SSPI context."


Hi I am trying to run a SQL query on one of my databases using an Azure Runbook. My setup is listed below. When I test this, I get the error The target principal name is incorrect. Cannot generate SSPI context." I have looked around and have not found anything related to an Azure setup. Does anyone have any ideas?

$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

Solution

  • As mentioned in the comment, looks you did not set $SQLServerCred before.

    Try:

    $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