【问题标题】:VBScript and batch file fails when run by Task Scheduler任务计划程序运行时 VBScript 和批处理文件失败
【发布时间】:2014-02-21 14:29:08
【问题描述】:

我有一个 VBScript,它检查 MS Word 是否正在运行隐藏,使其可见,然后再次隐藏它。

这是我在资源管理器中双击文件时可以正常工作的脚本代码:

dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")

set oWord = getobject(, "Word.Application")

if isobject(oWord) then 
    on error goto 0
    wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
    oWord.visible=true
    wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
    oWord.visible=false
else    
    wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if

当我运行它时它可以工作,但是当它在任务计划程序下运行时它失败了,所以我创建了一个批处理文件来启动它

wscript C:\dev\checkALPS.vbs

现在当我尝试从任务计划程序运行它时,它仍然失败并显示以下错误消息

---------------------------
Windows Script Host
---------------------------
Script: C:\dev\checkALPS.bat
Line:   7
Char:   1
Error:  ActiveX component can't create object: 'getobject'
Code:   800A01AD
Source:     Microsoft VBScript runtime error

我该怎么做才能让它工作?

【问题讨论】:

  • 您的任务是否以受限权限运行?您使用的是 64 位操作系统吗?如果是这样,请确保您在任务/bat 中使用的 WSCRIPT.EXE 是您想要的(64 位为 \System32 或 32 位为 \SysWOW64)。
  • 为我工作。我怀疑您的计划任务的配置有问题。您在任务的安全选项中配置了什么?错误消息表明在运行任务的用户的上下文中没有运行 Word 进程。
  • 任务设置为在管理员帐户下以最高权限运行。 Word 实例由在同一帐户下运行的 .NET 应用程序创建。

标签: batch-file vbscript scheduled-tasks wsh windows-scripting


【解决方案1】:

我遇到过类似的问题,我通过使用 cscript.exe 应用程序将 vbscript 激活为控制台应用程序而不是 Windows 应用程序来绕过它。域或计算机可能存在限制,不允许通过 wscript 执行 Windows 应用程序。作为替代方案,请尝试通过“Cscript.exe”激活相同的脚本。

所以代码是:

cscript C:\dev\checkALPS.vbs

get object 方法不会从 wscript 可执行文件中激活。所以你需要通过 wscript 激活它。

dim oWord
Dim wshShell, btn
Set wshShell = WScript.CreateObject("WScript.Shell")

set oWord = Wscript.GetObject(, "Word.Application")

if isobject(oWord) then 
    on error goto 0
    wshShell.Popup "Word is running, making visible", 7, "ALPS Push print", &H0 + &H40
    oWord.visible=true
    wshShell.Popup "MS Word is now visible" & vbcrlf & vbcrlf & "Waiting 30 seconds then hiding it", 30, "ALPS Push print", &H0 + &H30
    oWord.visible=false
else    
    wshShell.Popup "Word is not running" & vbcrlf & vbcrlf & "Quitting", 7, "ALPS Push print", &H0 + &H40
end if

试一试,让我知道它是如何工作的。

【讨论】:

猜你喜欢
  • 2013-06-11
  • 1970-01-01
  • 2014-03-15
  • 2013-03-01
  • 2019-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
相关资源
最近更新 更多