Powershell计划作业中的Sql查询无法运行

标签 sql powershell

您好,感谢您的浏览! 我是 PS 和 SQL 新手,但学到了很多东西。

环境:客户端:win7 64位,服务器sql2008r2 on server2008r2 sp1,powershell3.0, 事件目录

我想要做什么:从客户那里运行每周查询并通过电子邮件发送结果。

我可以在控制台中手动运行 powershell 脚本,它工作正常,但是当我将其设置为计划作业时,它会完成脚本,但内部的 sql 查询失败/不运行。 这是脚本:

#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

现在,我最初在 sql server 日志中收到登录错误,表明它正在尝试使用 NT AUTHORITY\ANONYMOUS USER 登录,并意识到计划的作业未设置为保存我的密码。更改了这一点,现在它使用我的域凭据登录; SQL Server 日志中不再出现登录失败错误。

我在论坛上看到了很多使用 powershell 来安排作业的问题,但似乎找不到任何人遇到我的具体问题。

想法? 任何帮助将不胜感激:)

最佳答案

我相信这是因为这一行:

$SqlQuery = gc sqlquery.sql

假设您正在执行这样的计划任务:

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

当您将 powershell 脚本作为计划作业运行时,它将在 “C:\Windows\system32” 文件夹中启动。

注意:当您指定“开始于”目录时也会发生这种情况

因此,当您的 Get-Content 执行时,它会在以下位置查找您的 SQL 查询:"C:\Windows\system32\sqlquery.sql"

每当我在需要访问任何文件的计划任务中运行脚本时,我总是对所有内容使用完整路径名,因为您无法假设事情是从哪里启动或执行的。

关于Powershell计划作业中的Sql查询无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202428/

相关文章:

MySQL 将行格式化为列

mysql - MySQL如何拆分字段数据中的字符串

Powershell - Get-ADOrganizationalUnit 组

mysql - 在 MYSQL 中,如何获得 LEFT JOIN 返回一个表中的每一行,以及如果另一个表中存在任何匹配行则返回一个标志?

python - 如何检查列是否存在,如果存在,则从中返回一个值

powershell - PowerShell 如何处理大型数据集?

powershell - Powershell-与Containskey和哈希表的设置值匹配不起作用

powershell - 即使在 shell 窗口关闭后仍保持作业运行

powershell - 如何在 PowerShell 中从 Web 下载整个文件夹/子文件夹

mysql - 获取mysql中重复值的多个最大计数