任务计划程序无法正确执行批处理文件

问题描述:

我有一个批处理文件来运行PowerShell的程序。当我双击该批处理文件,它运行PowerShell的code到

I have a batch file to run a powershell program. When I double click the batch file, it runs the powershell code to


  1. 生成EXCEL US preadsheet

  2. 电子邮件这EXCEL US preadsheet

我甚至看到这个动作发生。

I even see this action happening.

然而,当我使用任务计划执行批处理文件,它将运行,但它既不产生一个EXCEL US preadsheet,它肯定不会通过电子邮件发送EXCEL US preadsheet。

However, when I use Task Scheduler to execute the batch file, it will run, but it will neither generate an EXCEL spreadsheet, and it certainly won't email the EXCEL spreadsheet.

我创建其他任务运行其他批处理程序来执行PowerShell的节目,我从来没有过这样的问题。

I have created other tasks to run other batch programs to execute powershell programs, and I never had this problem.

我手动运行在任务计划程序中使用相同的权限的批处理文件,都没有问题。

I manually run the batch file with the same permissions used in the Task Scheduler, and have no problem.

我在指定任务计划的批处理文件的完整路径。

I specified the complete path of the batch file in the Task Scheduler.

我如何甚至开始解决此?

How do I even start troubleshooting this?

更多信息

下面是整个脚本,generate_GUPs_report.ps1

Here is the entire script, 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"

这是在运行,如果我双击该批处理文件的内容,但如果我通过任务计划程序

And here are the contents of the batch file that runs if I double-click, but not if I schedule through Task Scheduler

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe D:\Script\generate_GUPs_report.ps1

这是任务计划行动

我有一个类似的问题,试图安排,自动化Microsoft Word中的脚本。就我而言,我最终能够通过设置DCOM身份来解决它。

I had a similar problem trying to schedule a script that automates Microsoft Word. In my case, I was eventually able to work around it by setting the DCOM Identity.


  1. 开始>运行: DCOMCNFG

    • 如果您是在64位操作系统上运行32位Office,可以使用 MMC comexp.msc / 32

  1. Start > Run: dcomcnfg
    • If you're running 32 bit office on a 64 bit OS, use mmc comexp.msc /32