3

**Update: I added a much shortened script which generates the same issue. See the bottom of this port:

I have a PowerShell script that runs a SQL query against a MSSQL server (different server). The script runs as expected when run interactively. When run as a scheduled task with the same credentials, I don't get query results. The task runs the script and report success, but no data is retrieved. From what we see in the logs, the connection to the SQL server is made as NT AUTHORITY\ANONYMOUS LOGIN.
I've tried setting delegation for the the machine running the task. There is an authentication issue here for which I just can't find a solution.

Details:

  • Task server Win 2019
  • SQL server Win 2019
  • SQL v.2019 running on an Windows Availability Group (I get the same error if script connects directly to active node)
  • Credentials are an AD account with password stored at task creation
  • Account has local admin rights
  • AD func. level 2016
  • Script uses Get-SQL module for query
  • Connect string uses Integrated Security=true
  • Task is set to run with highest priv.

Pertinent info from a transcript:

**********************
Transcript started, output file is C:\Temp\sessionrecord2.txt
WARNING: Error opening connection to 'Server=svr126AGLa.myco.com;Integrated Security=true;Initial Catalog=mydb;ApplicationIntent=ReadOnly'
PS>TerminatingError(): "System error."
>> $global:?
True
**********************

Script:

# Connects to database and retrieves the first 100k records

$Connect = "Server=svr126AGLa.myco.com;Integrated Security=true;Initial Catalog=mydb;ApplicationIntent=ReadOnly"
$exportDir = "D:\Logs\Events"

function Get-LSEventinfo {
    #Grabs all assets in lansweeper with usernames defined
    #stored in sqlite db
    $recs = $args[0]  #Number of records to request
    $sql = @"
    
    Select Top $recs tblAssets.AssetName,
    tblAssets.Domain,
    tblAssets.IPAddress,
    tblNtlog.Eventcode,
    Case tblNtlog.Eventtype
      When 1 Then 'Error'
      When 2 Then 'Warning'
      When 3 Then 'Information'
      When 4 Then 'Success Audit'
      When 5 Then 'Failure Audit'
    End As Eventtype,
    tblNtlogFile.Logfile,
    tblNtlogMessage.Message,
    tblNtlogSource.Sourcename,
    tblNtlogUser.Loguser,
    tblNtlog.TimeGenerated
  From tblAssets
    Inner Join tblAssetCustom On tblAssets.AssetID = tblAssetCustom.AssetID
    Inner Join tblNtlog On tblAssets.AssetID = tblNtlog.AssetID
    Inner Join tblNtlogFile On tblNtlogFile.LogfileID = tblNtlog.LogfileID
    Inner Join tblNtlogMessage On tblNtlogMessage.MessageID = tblNtlog.MessageID
    Inner Join tblNtlogSource On tblNtlogSource.SourcenameID = tblNtlog.SourcenameID
    Inner Join tblComputersystem On tblAssets.AssetID = tblComputersystem.AssetID
    Left Join tblNtlogUser On tblNtlogUser.LoguserID = tblNtlog.LoguserID
    Inner Join tsysOS On tsysOS.OScode = tblAssets.OScode
  Where tblAssets.Domain = 'DOMAIN' And tblNtlogFile.Logfile = 'Security' And
    tblAssetCustom.State = 1 And tblNtlog.Eventtype != 3 And tblComputersystem.Domainrole < 2
  Order By tblNtlog.TimeGenerated Desc
"@   

#Connect & query
try { $hld = get-sql -MsSQLserver -connection $connect -Session TT   }
catch { "failed real connect"| set-content c:\temp\errcon2.log -force}

# Export
try{  TT $SQL |export-csv "$exportDir\LSEventlog.csv" -notypeinformation; write-host "file exported to $exportdir"} }
Catch {write-output "failed query"}
    TT -close
    $hld = $null
}

# main:
  Start-Transcript -Path C:\Temp\sessionrecord2.txt
  Get-LSEventinfo 100000
  Stop-Transcript

Simplified script - same error. Note - error is the same whether we conenct to the AvailGrp listener or one of the nodes

$Connect = "Server=tcp:Svr126SQLb.myco.com;Integrated Security=SSPI;Initial Catalog=mydb;ApplicationIntent=ReadOnly"
#$Connect = "Server=tcp:Svr126AGLa.myco.com;Integrated Security=SSPI;Initial Catalog=mydb;ApplicationIntent=ReadOnly"


Start-Transcript -Path C:\Temp\sessionrecord2.txt
$sql = @"
  Select * from tblAssets.Domain
"@   

 $hld = get-sql -MsSQLserver -connection $connect -Session TT -ForceNew 
 TT -close
 $hld = $null
 Stop-Transcript
20
  • 3
    uSlackr, you are right but that is not the question
    – An-dir
    Commented Jul 7, 2022 at 4:03
  • 3
    and remember without showing the script we are hunting a ghost ;)
    – djdomi
    Commented Jul 7, 2022 at 4:33
  • 3
    Your script as written never uses the $connect variable
    – mfinni
    Commented Jul 7, 2022 at 20:13
  • 2
    Also, what is TT ? Best practice is to not use aliases in your scripts, use complete cmdlet names.
    – mfinni
    Commented Jul 7, 2022 at 20:17
  • 2
    Have you tried the ForceNew switch?
    – Greg Askew
    Commented Jul 8, 2022 at 23:58

3 Answers 3

4

Turns out this is similar to the double hop issue. To resolve it, I ran Enable-WSManCredSSP Client –DelegateComputer <schedTaskHost> on the task server and Enable-WSManCredSSP Server on the SQL servers.

2
  • 2
    Interesting, thanks for sharing the result!
    – Manu
    Commented Jul 15, 2022 at 5:20
  • 1
    Cool find, I think this may be a similar to Kerberos but that turned out to be a symptom/distraction :-)
    – Greg Askew
    Commented Aug 17, 2022 at 20:44
3

The issue seems to be related to Kerberos delegation. Things that are important: using correct machine name, service accounts, and registered SPNs correctly.
Try the following:

2
  • 1
    We verified that SPNs are registered. We are using the defaults port so haven't added the port to SPN. We can test that. Connection strings appear correct. I added tcp: to be explicit, but it made no difference. Keep in mind, this works interactively, just not as a scheduled task
    – uSlackr
    Commented Jul 11, 2022 at 13:11
  • 1
    Can't imagine this being the cause of the problem but I'd sill configure SPNs as the Configuration Manager suggests, which would include a SPN with the TCP port even if it's default. Otherwise your task would just use NTLM which is not a problem except if you'd somehow prohibit NTLM authentication.
    – Manu
    Commented Jul 12, 2022 at 10:49
1
+200

This looks highly like a Kerberos related problem.

At first I’d check if the task is able to get a ticket from the domain controller. Check all of them for an event in the Security Log with ID 4769. It should include the account name (of the user which runs the task) and the username of the account running the SQL Service. This event should be comparable with a cached ticket for the user (can be listed with klist)

If this is not the case, you should look for Kerberos related errors at run time on all domain controllers.

Without the logs it’s hard to give advice but I can imagine that there is something preventing the task scheduler from reaching your domain controller (windows firewall, policy, antivirus)

Maybe a guide like this can be helpful.

What I also would check (not particularly Kerberos related):

  • Check the Credential Manager for entries regarding the SQL Server to which you user session does have access to but the scheduled task might not have.
  • Does the users UPN, sAMAccountName or CN differ from one another? Has one of these even been changed?
  • Are there network restrictions between those hosts and the domain controller
5
  • No 469 events. I do see the process login. I see no evidence of Kerberos activity at all. Could this be not enabled on the domain?
    – uSlackr
    Commented Jul 13, 2022 at 15:00
  • 1
    I'm sorry, it was a typo. I meant ID 4769, now corrected in the Answer. This should be logged by default. However it will only be logged on the DC who is responding. If there is still nothing, I'd troubleshoot the connection to dc (all on the servers site)
    – Manu
    Commented Jul 13, 2022 at 15:08
  • I do see a ticket request for the configured account to access the machine the task is hosted on (Service name). Nothing for SQL access unless it went to a different DC. Nothing in Credential Mgr, for SQL. Last thing users UPN, SAM and CN all match.
    – uSlackr
    Commented Jul 13, 2022 at 15:42
  • Then I'd say something is preventing your Task Scheduler from reaching the DC for a Kerberos ticket. I'd look through the Eventlog (especially security) of the server running the task.
    – Manu
    Commented Jul 14, 2022 at 4:55
  • 1
    Appreciate the assist! See my answer below. Login type now kerberos on the SQL server!
    – uSlackr
    Commented Jul 14, 2022 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .