Note that there are some explanatory texts on larger screens.

plurals
  1. POSql query in Powershell scheduled job fails to run
    primarykey
    data
    text
    <p>Hello and thanks for taking a look! I'm new at PS and SQL, but learning lots.</p> <p><strong>Environment</strong>: client: win7 64bit, server sql2008r2 on server2008r2 sp1, powershell3.0, Active Directory</p> <p><strong>What I want to do</strong>: Run weekly query from client and email the results.</p> <p>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:</p> <pre><code>#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&gt;&amp;1 |Tee-Object -File $errorlog $nRecs | Out-Null #Populate Hash Table $objTable = $DataSet.Tables[0] 2&gt;&amp;1 |Tee-Object -File $errorlog #Export Hash Table to CSV File $objTable | Export-CSV $AttachmentPath 2&gt;&amp;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 </code></pre> <p>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.</p> <p>I've seen plenty of issues (on forums) using powershell to schedule jobs, but can't seem to find anyone with my specific problem.</p> <p>Ideas? Any help would be greatly appreciated :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload