任务计划程序无法正确执行批处理文件
Posted
技术标签:
【中文标题】任务计划程序无法正确执行批处理文件【英文标题】:Task Scheduler doesn't execute batch file properly 【发布时间】:2014-10-02 06:14:57 【问题描述】:我有一个批处理文件来运行一个 powershell 程序。当我双击批处理文件时,它会运行 powershell 代码来
-
生成 EXCEL 电子表格
通过电子邮件发送此 EXCEL 电子表格
我什至看到这个动作发生了。
但是,当我使用任务计划程序执行批处理文件时,它会运行,但它既不会生成 EXCEL 电子表格,也不会通过电子邮件发送 EXCEL 电子表格。
我已经创建了其他任务来运行其他批处理程序来执行powershell程序,我从来没有遇到过这个问题。
我以与任务计划程序相同的权限手动运行批处理文件,没有问题。
我在任务计划程序中指定了批处理文件的完整路径。
我什至如何开始解决这个问题?
更多信息
这是整个脚本,generate_GUPs_report.ps1
$DSN = 'Schools SQL Server ODBC'
$DirectoryToSave='D:\Script\'
$Filename='Daily_GUP_Report'
$password = $NULL
$credentials = $NULL
$password = $NULL
$conn = $NULL
$cmd = $NULL
$k = $NULL
# constants
$xlCenter=-4108
$xlTop=-4160
$xlOpenXMLWorkbook=[int]51
<#Previously created password file in D:\Script\central_cred.txt, read-host -assecurestring | convertfrom-securestring | out-file D:\Script\central_cred.txt#>
$password = get-content D:\Script\central_cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "sem5",$password
$username = $credentials.UserName
$password = $credentials.GetNetworkCredential().Password
# SQL Query
$SQL1 = "SELECT
dbo.V_SEM_COMPUTER.COMPUTER_NAME, dbo.V_SEM_COMPUTER.IP_ADDR1_TEXT as IP_Address, EVENT_DESC as Successful_GUP_Download
FROM
dbo.V_AGENT_SYSTEM_LOG, dbo.V_SEM_COMPUTER
WHERE
EVENT_SOURCE = 'sylink'
and (EVENT_DESC LIKE '%Downloaded new content update from Group Update Provider successfully.%'
or EVENT_DESC LIKE '%Downloaded content from GUP%')
and dbo.V_AGENT_SYSTEM_LOG.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
and dbo.V_SEM_COMPUTER.COMPUTER_ID = dbo.V_AGENT_SYSTEM_LOG.COMPUTER_ID
ORDER BY
dbo.V_AGENT_SYSTEM_LOG.TIME_STAMP DESC"
$SQL2 = "SELECT
COUNT(DISTINCT EVENT_DESC) AS Number_of_distinct_GUP_downloads_past_24hrs,COUNT(DISTINCT dbo.V_SEM_COMPUTER.COMPUTER_NAME) AS Number_of_Computer_successfully_downloaded_from_GUP_past_24hrs
FROM
dbo.V_AGENT_SYSTEM_LOG, dbo.V_SEM_COMPUTER
WHERE
EVENT_SOURCE = 'sylink'
and (EVENT_DESC LIKE '%Downloaded new content update from Group Update Provider successfully.%'
or EVENT_DESC LIKE '%Downloaded content from GUP%')
and dbo.V_AGENT_SYSTEM_LOG.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
and dbo.V_SEM_COMPUTER.COMPUTER_ID = dbo.V_AGENT_SYSTEM_LOG.COMPUTER_ID"
$SQL3 = "SELECT
dbo.V_SEM_COMPUTER.COMPUTER_NAME, dbo.V_SEM_COMPUTER.IP_ADDR1_TEXT as IP_Address, COUNT(*) as Number_of_Occurrences_in_Successful_GUP_Downloads_Log
FROM
dbo.V_AGENT_SYSTEM_LOG, dbo.V_SEM_COMPUTER
WHERE
EVENT_SOURCE = 'sylink'
and (EVENT_DESC LIKE '%Downloaded new content update from Group Update Provider successfully.%'
or EVENT_DESC LIKE '%Downloaded content from GUP%')
and dbo.V_AGENT_SYSTEM_LOG.TIME_STAMP > DATEDIFF(second, '19700101', DATEADD(day, -1, GETDATE())) * CAST(1000 as bigint)
and dbo.V_SEM_COMPUTER.COMPUTER_ID = dbo.V_AGENT_SYSTEM_LOG.COMPUTER_ID
GROUP BY
dbo.V_SEM_COMPUTER.COMPUTER_NAME, dbo.V_SEM_COMPUTER.IP_ADDR1_TEXT
ORDER BY
Number_of_Occurrences_in_Successful_GUP_Downloads_Log DESC"
# Create Excel file to save the data
if (!(Test-Path -path "$DirectoryToSave")) #create it if not existing
New-Item "$DirectoryToSave" -type directory | out-null
$excel = New-Object -Com Excel.Application
$excel.Visible = $True
$wb = $Excel.Workbooks.Add()
$currentWorksheet=1
$ws = $wb.Worksheets.Item(1)
$ws.name = "GUP Download Activity"
$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL1)
if ($qt.Refresh())
$ws.Activate()
$ws.Select()
$excel.Rows.Item(1).HorizontalAlignment = $xlCenter
$excel.Rows.Item(1).VerticalAlignment = $xlTop
$excel.Rows.Item("1:1").Font.Name = "Calibri"
$excel.Rows.Item("1:1").Font.Size = 11
$excel.Rows.Item("1:1").Font.Bold = $true
$ws = $wb.Worksheets.Item(2)
$ws.name = "Totals"
$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL2)
if ($qt.Refresh())
$ws.Activate()
$ws.Select()
$excel.Rows.Item(1).HorizontalAlignment = $xlCenter
$excel.Rows.Item(1).VerticalAlignment = $xlTop
$excel.Rows.Item("1:1").Font.Name = "Calibri"
$excel.Rows.Item("1:1").Font.Size = 11
$excel.Rows.Item("1:1").Font.Bold = $true
$ws = $wb.Worksheets.Item(3)
$ws.name = "GUP Downloads per Computer"
$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN;UID=$username;PWD=$password", $ws.Range("A1"), $SQL3)
if ($qt.Refresh())
$ws.Activate()
$ws.Select()
$excel.Rows.Item(1).HorizontalAlignment = $xlCenter
$excel.Rows.Item(1).VerticalAlignment = $xlTop
$excel.Rows.Item("1:1").Font.Name = "Calibri"
$excel.Rows.Item("1:1").Font.Size = 11
$excel.Rows.Item("1:1").Font.Bold = $true
$filename = "D:\Script\Daily_GUP_Report.xlsx"
if (test-path $filename ) rm $filename
$wb.SaveAs($filename, $xlOpenXMLWorkbook) #save as an XML Workbook (xslx)
$wb.Saved = $True #flag it as being saved
$wb.Close() #close the document
$Excel.Quit() #and the instance of Excel
$wb = $Null #set all variables that point to Excel objects to null
$ws = $Null #makes sure Excel deflates
$Excel=$Null #let the air out
Start-Process "D:\Script\send_GUP_report_schools.bat"
这里是双击运行的批处理文件的内容,但如果我通过任务计划程序进行调度则不是
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe D:\Script\generate_GUPs_report.ps1
这是任务计划程序中的操作
【问题讨论】:
如果您在任务中输入常用用户凭据进行身份验证,然后启动任务 - 有什么变化吗? @foxidrive 我总是为我创建的任何任务输入我的用户凭据,由于某种原因,这个任务不起作用。我尝试删除并重新创建它.... 批处理文件是否使用任何相对路径?如果是这种情况,请尝试设置显式路径,因为工作目录可能不是您所期望的。这是一个猜测 - 没有看到我们能做的所有脚本。 D:\ 是物理驱动器还是映射的网络驱动器? @Cole9350 D:\ 是物理驱动器 【参考方案1】:我在尝试安排自动化 Microsoft Word 的脚本时遇到了类似的问题。就我而言,我最终能够通过设置 DCOM 身份来解决它。
步骤
-
开始 > 运行:
dcomcnfg
如果您在 64 位操作系统上运行 32 位 Office,请使用 mmc comexp.msc /32
展开Component Services
> Computers
> My Computer
> DCOM Config
查找Microsoft Excel Application
右键单击,Properties
,Identity
选项卡。
将其设置为 This user
并输入您为计划任务输入的相同凭据。
【讨论】:
我找不到 Microsoft Excel 应用程序。这是 Windows Server 2012 R2。是叫什么别的名字,还是在别的地方? HKEY_CLASSES_ROOT\AppID\00020812-0000-0000-C000-000000000046 存在,我在blogs.technet.com/b/the_microsoft_excel_support_team_blog/…中尝试了其他步骤 嗯,我找到了 2 台机器,一台装有运行 Office 2010 的 Windows 2008 R2,一台装有运行 Office 2013 的 Windows 8.1。我猜excel.exe -REGSERVER
工作?也许修复安装可以做到这一点?
我刚刚意识到我的两台机器在 64 位操作系统上都有 64 位办公室。如果你有 32 位 Office,不妨试试看这里:mmc comexp.msc /32
太棒了!我编辑了答案以包含该位,以防其他人发现此内容。以上是关于任务计划程序无法正确执行批处理文件的主要内容,如果未能解决你的问题,请参考以下文章
window server 2008 批处理单独运行可以,加入任务计划无法运行