JayCee
JayCee

Reputation: 77

Sql query in Powershell scheduled job fails to run

Hello and thanks for taking a look! I'm new at PS and SQL, but learning lots.

Environment: client: win7 64bit, server sql2008r2 on server2008r2 sp1, powershell3.0, Active Directory

What I want to do: Run weekly query from client and email the results.

I can run the powershell script manually in the console and it works fine, but when I set it as a scheduled job it completes the script but the sql query inside fails/doesn't run. Here's the script:

#Date
$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MMM-dd')

#Connection Strings
$Database = "database"
$Server = "server"

#SMTP Relay Server
$SMTPServer = "mail.server"

#Export File
$AttachmentPath = "path"

#Errorlog
$errorlog= "log path"

$SqlQuery = gc sqlquery.sql

# Connect to SQL and query data, extract data to SQL Adapter
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "server=$Server;database=$Database;integrated security=true;"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$nRecs = $SqlAdapter.Fill($DataSet)  2>&1 |Tee-Object -File $errorlog
$nRecs | Out-Null


#Populate Hash Table
$objTable = $DataSet.Tables[0] 2>&1 |Tee-Object -File $errorlog

#Export Hash Table to CSV File
$objTable | Export-CSV $AttachmentPath 2>&1 |Tee-Object -File $errorlog

#Remove quotes from CSV file
(get-content $AttachmentPath) |
foreach-object {$_ -replace '"', ''} |
set-content $AttachmentPath

#Remove first line from CSV file
(get-content $AttachmentPath |
select -skip 1) |
set-content $AttachmentPath


#Send SMTP Message
$Mailer = new-object Net.Mail.SMTPclient($SMTPServer)
$From = "from"
$To = "to"
$Subject = "Some text " + $currentdate
$Body = "Some more text.`n" + (import-csv -path $AttachmentPath | out-string)
$Msg = new-object Net.Mail.MailMessage($From,$To,$Subject,$Body)
$Msg.IsBodyHTML = $False
$Attachment = new-object Net.Mail.Attachment($AttachmentPath)
$Msg.attachments.add($Attachment)
$Mailer.send($Msg)

exit

Now, I was originally getting a login error in the sql server logs indicating it was trying to log in with NT AUTHORITY\ANONYMOUS USER and realized that the scheduled job was not set to save my password. Changed that and now it logs in using my domain credentials; no more login failed errors in the sql server logs.

I've seen plenty of issues (on forums) using powershell to schedule jobs, but can't seem to find anyone with my specific problem.

Ideas? Any help would be greatly appreciated :)

Upvotes: 2

Views: 2267

Answers (1)

HAL9256
HAL9256

Reputation: 13483

I believe that it is because of this line:

$SqlQuery = gc sqlquery.sql

Assuming that you are executing the scheduled task like this:

powershell.exe -file "C:\dir\myscript.ps1"

When you are running the powershell script as a scheduled job, it starts up in the "C:\Windows\system32" folder.

Note: This also happens when you specify the "Start in" directory

Therefore when your Get-Content executes, it looks for your SQL query in: "C:\Windows\system32\sqlquery.sql"

Anytime I am running scripts in scheduled tasks that require access to any files, I always use the full path names for everything because you can't assume where things are started or executed from.

Upvotes: 3

Related Questions