Search code examples
vbscriptshutdown

Simple shutdown script does not function properly after PC wipe


I had a simple VB Script that would let me enter in the amount of minutes before I wanted my PC to turn off by itself, and then it would auto-shutdown. That worked fine. After I wiped my PC, the script no longer functions as intended, instead showing a blank cmd window after I enter the number of minutes before shutdown, and displays the inputbox again (asking for # of minutes before shutdown).

Any ideas on why this won't function correctly, and why it worked before but not now? Do I need a certain package from Microsoft that maybe I didn't reinstall?

Code:

Dim a
Dim oShell
a=inputbox("After how many minutes would you like to shut down your PC? Enter cancel to cancel a previous shutdown")
Set oShell = WScript.CreateObject ("WScript.Shell")
if a = "cancel" then
    oShell.run "cmd.exe /c shutdown /a"
elseif a = "" then
    MsgBox"Please enter after how many minutes you would like to turn off this PC",0+16,"Enter a number"
elseif a = "0" then
    b=msgbox("Are you sure you want to shut down this PC immediately?",4+32,"Shut down immediately?")
    if b = "6" then
        oShell.run "cmd.exe /c shutdown /s /f"
    end if
else
    oShell.run "cmd.exe /c shutdown /s /t " & (a * 60)
end if

EDIT: Running the script from its directory works as intended, but running the VBScript from a shortcut (as a I had been doing) doesn't work and yields the above results.

EDIT: Also the script itself won't run properly on my desktop, but runs fine in the folder I store my scripts.


Solution

  • You named the script shutdown.vbs and run it with the working directory set to the directory containing the script. By running oShell.Run "cmd.exe /c shutdown ..." your script is effectively calling itself.

    If you call a command shutdown (without path and extension) the system is looking for a file with one of the extensions listed in %PATHEXT% in the directories listed in the %PATH% environment variable. The first match wins. Since on Windows the current directory comes first in the %PATH% the file %CD%\shutdown.vbs is found before %windir%\system32\shutdown.exe.

    Either rename your VBScript or change cmd.exe /c shutdown to cmd.exe /c shutdown.exe and the problem will disappear.