简体   繁体   English

无法将VBS作为批处理或计划任务运行

[英]Unable to run VBS as Batch or Scheduled Task

I have a VBS that prints a set of files to PDF. 我有一个VBS,可将一组文件打印为PDF。 Works fine when running the VBS manually, but when trying to execute either via Batch or Scheduled Task it does not work. 手动运行VBS时可以正常工作,但是尝试通过“批处理”或“计划任务”执行时则无法正常工作。 I have tried the following. 我尝试了以下方法。

Batch File: 批处理文件:

C:\APPS\TEMP\example.vbs

Result: 结果:

在此处输入图片说明

Batch File: 批处理文件:

%windir%\syswow64\cscript //nologo C:\APPS\TEMP\example.vbs

Same result as image shown above. 与上图所示结果相同。

VBS Code: VBS代码:

Set fso = CreateObject("Scripting.FileSystemObject")
currentdir = fso.GetAbsolutePathName(".")

Set xmldom = CreateObject("MSXML.DOMDocument")
xmldom.Load(currentdir & "\info.xml")

progid = xmldom.SelectSingleNode("/xml/progid").text

Set obj = CreateObject(progid)

printername = obj.GetPrinterName

runonce = obj.GetSettingsFileName(True)

Set fldr = fso.GetFolder(currentdir & "\in")
cnt = 0
For Each f In fldr.files
  cnt = cnt + 1
  output = currentdir & "\out\" & Replace(f.name, ".xls", "") & ".pdf"

  obj.Init
  obj.SetValue "Output", output
  obj.SetValue "ShowSettings", "never"
  obj.SetValue "ShowPDF", "no"
  obj.SetValue "ShowProgress", "no"
  obj.SetValue "ShowProgressFinished", "no"
  obj.SetValue "SuppressErrors", "yes"
  obj.SetValue "ConfirmOverwrite", "no"

  obj.WriteSettings True

  printfile = currentdir & "\in\" & f.Name
  cmd = """" & currentdir & "\printto.exe"" """ & printfile & """ """ & printername & """"

  Set WshShell = WScript.CreateObject("WScript.Shell")
  ret = WshShell.Run(cmd, 1, True)

  While fso.fileexists(runonce)
    WScript.Sleep 100
  Wend
Next

Set obj = Nothing

WScript.Echo cnt & " documents were printed."

In order to get it to run manually I had to set 32-bit cscript.exe as the default program for VBS. 为了使其能够手动运行,我必须将32位cscript.exe设置为VBS的默认程序。 So in my mind, the batch code that calls up that first them points to the VBS should work, but I am not sure why it isn't. 因此,在我看来,首先调用它们的批处理代码指向VBS应该起作用,但是我不确定为什么不行。 This in a Windows Server 2008 R2 system. 这在Windows Server 2008 R2系统中。 Also tried it on a Windows 7 x64 box to eliminate any odd GPO or security issues. 还尝试在Windows 7 x64机器上进行操作,以消除任何奇怪的GPO或安全问题。

Works fine when running the VBS manually 手动运行VBS时工作正常

Suppose that your info.xml is located in the same directory as the script file C:\\APPS\\TEMP\\info.xml . 假设您的info.xml与脚本文件C:\\APPS\\TEMP\\info.xml位于同一目录中。

fso.GetAbsolutePathName(".") gives you the current, by default startup directory. fso.GetAbsolutePathName(".")为您提供当前的默认启动目录。 It's exactly the same as running CD . 它与运行CD .完全相同CD . on a command prompt. 在命令提示符下。

Your script works when you run by double-clicking because it starts from it's parent folder. 当您通过双击运行脚本时,它可以工作,因为它是从其父文件夹开始的。

But it does not work when you start from C:\\APPS as in the image you show or as a task command with no start-in directory configuration. 但是,当您从C:\\APPS启动时(如显示的图像中所示)或作为没有启动目录配置的任务命令,它不起作用。 A task command starts from the user's home directory, commonly %windir%\\system32 when you dismiss to configure start in directory. 任务命令从用户的主目录(通常是%windir%\\system32启动,如果您取消配置目录中的启动。

In short, the file info.xml cannot be found so the method SelectSingleNode failed. 简而言之,找不到文件info.xml因此方法SelectSingleNode失败。

I'd recommend you to to locate info.xml by using the script's full path, instead of fso.GetAbsolutePathName(".") . 我建议你找到info.xml通过使用脚本的完整路径,而不是, fso.GetAbsolutePathName(".")

currentdir = fso.BuildPath(fso.GetParentFolderName(WScript.ScriptFullName), "info.xml")

Presumably there was an error when loading the XML data. 加载XML数据时大概有错误。 Insert code to check for parser errors after you load the file. 加载文件后,插入代码以检查解析器错误。 Also tell the parser to work synchronously, so you don't try to do something when the previous operation isn't completed yet. 还要告诉解析器同步工作,这样在上一个操作尚未完成时,您就不要尝试做某事。

xmldom.Async = False
xmldom.Load(currentdir & "\info.xml")

If xmldom.ParseError.ErrorCode <> 0 Then WScript.Echo xmldom.ParseError.Reason WScript.Quit xmldom.ParseError.ErrorCode End If

progid = xmldom.SelectSingleNode("/xml/progid").text

Also make sure that the file you're trying to read is where you expect it to be. 另外,请确保您要读取的文件位于期望的位置。 The statement 该声明

currentdir = fso.GetAbsolutePathName(".")

produces the path to the current working directory, but in a scheduled task that might be different from what you expect, unless you explicitly define a working directory in the task properties. 生成当前工作目录的路径,但是在计划的任务中可能会与您期望的路径不同,除非您在任务属性中明确定义了工作目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM