任务计划程序不会正确执行批处理文件

时间:2014-08-08 17:30:26

标签: batch-file powershell scheduled-tasks

我有一个批处理文件来运行PowerShell程序。当我双击批处理文件时,它会将powershell代码运行到

  1. 生成EXCEL电子表格
  2. 通过电子邮件发送此EXCEL电子表格
  3. 我甚至看到这个动作正在发生。

    但是,当我使用任务计划程序执行批处理文件时,它将运行,但它既不会生成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

1 个答案:

答案 0 :(得分:3)

我在尝试安排自动化Microsoft Word的脚本时遇到了类似的问题。就我而言,我最终能够通过设置DCOM身份来解决它。

步骤

  1. 开始&gt;运行:dcomcnfg
    • 如果您在64位操作系统上运行32位,请使用mmc comexp.msc /32
  2. 展开Component Services&gt; Computers&gt; My Computer&gt; DCOM Config
  3. 查找Microsoft Excel Application
  4. 右键点击,PropertiesIdentity标签。
  5. 将其设置为This user并输入您为计划任务输入的相同凭据。