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

3
我有一个批处理文件来运行powershell程序。当我双击批处理文件时,它会运行powershell代码,以
  1. 生成一个EXCEL电子表格
  2. 将此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

这里是任务计划程序中的操作:

enter image description here


如果您在身份验证任务中输入您通常的用户凭据,然后启动任务 - 会发生任何变化吗? - foxidrive
@foxidrive 我总是为我创建的任何任务输入我的用户凭据,但出于某种原因,这个任务不起作用。我尝试删除并重新创建它... - Glowie
批处理文件是否使用了相对路径?如果是这种情况,请尝试设置明确的路径,因为工作目录可能不是您所期望的。这只是一个猜测 - 如果没有看到脚本,那就是我们能做的全部。 - foxidrive
D:\ 是物理驱动器还是映射的网络驱动器? - Cole9350
@Cole9350 D:\ 是物理驱动器。 - Glowie
1个回答

3

我曾经遇到过类似的问题,尝试调度自动化Microsoft Word脚本。最终,我通过设置DCOM身份解决了这个问题。

步骤

  1. 开始菜单 > 运行: dcomcnfg
    • 如果您在64位操作系统上运行32位office,请使用 mmc comexp.msc /32
  2. 展开 组件服务 > 计算机 > 我的电脑 > DCOM 配置
  3. 找到 Microsoft Excel 应用程序
  4. 右键单击,选择 属性,切换到 身份 选项卡。
  5. 将其设置为 此用户 并输入与计划任务相同的凭据。

HKEY_CLASSES_ROOT\AppID{00020812-0000-0000-C000-000000000046} 存在,我尝试了 http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2012/11/12/microsoft-excel-does-not-appear-in-dcom-configuration-snap-in.aspx 中的其他步骤。 - Glowie
哦,我在两台机器上找到了它,一台运行Windows 2008 R2和Office 2010,另一台运行Windows 8.1和Office 2013。我猜 excel.exe -REGSERVER 能行?也许重新安装修复可以解决它? - briantist
我尝试了excel.exe -REGSERVER,将尝试修复安装。 - Glowie
2
我刚意识到我的两台机器都在64位操作系统上安装了64位的Office。如果你使用的是32位的Office,也许可以尝试在这里查找:mmc comexp.msc /32 - briantist
1
太好了!我编辑了答案,包括那一部分,以防其他人也遇到这个问题。 - briantist
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接