wshShell.Exec错误80070002"系统找不到指定的文件"

时间:2014-08-20 18:03:59

标签: shell vbscript exec wsh

我尝试使用wshShell.Exec或wshShell.Run通过另一个脚本运行脚本。我尝试了两种方法,两种方法都给了我同样的错误。我搜索了这个问题,找不到任何可以解决问题的方法。真正非常相关的唯一建议是尝试使用wshShell.Run而不是Exec。

这是我的剧本的相关部分:

strScriptPath = "T:\IT resources\Scripts\Shutdown Scripts"
strForceShutdown = "ForceShutdown.vbs"

  For j = 0 to 99
   Set objActive = wshShell.Run(strForceShutdown)
   ' In case I ever need to get this working to run it from another folder. 
   ' Set objActive = wshShell.Exec("cd " & strScriptPath & "")
   ' Set objActive = wshShell.Exec("wscript " & strForceShutdown & "") 
    constConf = MsgBox("Automatic shutdown initializing. Continue?" & chr(10) & "Y=Shutdown N=Postpone 30 minutes",4,"Automatic Shutdown Notification")

     If constConf = 7 Then
      objActive.Terminate
      Wscript.Sleep(1800000)

      Else
       objActive.Terminate
       Exit For
     End If
   Next

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

Shell.Run returns an integer,因此您无法在其返回值上调用方法(Terminate)。你也不能Set它,因为它不是一个对象。

只需运行它即可调用关机脚本。然而,给它完整的路径,而不是相对路径。从任务计划程序启动的脚本通常具有与手动启动的脚本不同的“起始文件夹”,因此不要依赖于脚本相对地找到另一个脚本。 此外,您必须在路径之前和之后添加Chr(34)以考虑任何空格。

strForceShutdown = "c:\path\to\ForceShutdown.vbs"
wshShell.Run Chr(34) & strForceShutdown & Chr(34)

最后,为什么要启动脚本然后询问是否要关机?为什么不在用户响应后启动脚本,然后您不必担心终止正在运行的进程。