wshShell.Exec Error 80070002 "The system cannot find the file specified"
Shell.Run returns an integer, so you can't call a method (
Terminate) on its return value. You also can't
Set it since it's not an object.
You can call your shutdown script by just running it. Give it the full path, however, not a relative path. Scripts launched from Task Scheduler often have different "starting folders" than those launched manually so don't rely on your script finding the other one relatively.
Also, you'll have to add
Chr(34) before and after your path to account for any spaces.
strForceShutdown = "c:\path\to\ForceShutdown.vbs" wshShell.Run Chr(34) & strForceShutdown & Chr(34)
Finally, why launch the script and then ask whether to shutdown? Why not just launch your script after the user has responded and then you don't have to worry about terminating a running process.
Related videos on Youtube
System and Network Admin at Vancouver Bolt and Supply B.A. Physics and B.A. Mathematics, Lewis & Clark CollegeUpdated on June 04, 2022
Adam Harvey 7 months
I'm trying to use either wshShell.Exec or wshShell.Run to run a script through another script. I have tried both methods, and both methods give me the same error. I've Googled the issue and can't find anything that seems to fix the issue. The only suggestion that really was very relevant was to try using wshShell.Run instead of Exec.
Here's the relevant part of my script:
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
Thanks for any help!
Adam Harvey over 8 yearsYep. Looks like I just read something wrong and thought it returned an object. Anyway, I'm running it this way so that I can get the computer to shutdown if no option has been selected after 30 minutes. Otherwise, if no one is at the computer, it will get stuck on the MsgBox line. Anyway, thanks for the help!
Adam Harvey over 8 yearsActually, I needed the process to be terminateable. It looks like the only way to do that is with the Exec method. I got it working by adding "CMD" to the commands. The Terminate method only works with an object set by Exec. So it seems like Exec does return an object? Because it is working when I treat it as such. And I have seen it suggested elsewhere, after looking for some info on Terminate. Anyway, my script is working now with Exec, at least.
Bond over 8 yearsYou can have a message box that disappears and performs a default action. See the Popup method of the
Shellclass. If it returns -1, it means the timer elapsed.
Bond over 8 yearsSo if the user clicks Yes to shutdown or if the Popup times-out after 30 minutes, use
Shell.Runto launch your shutdown script.
If intResult = vbYes Or intResult = -1 Then wshShell.Run ...
Adam Harvey over 8 yearsThat would definitely help simplify my script a bit. Thanks for the info!