Search code examples
cmdvbscriptconditional-execution

VBScript - Pass shell command using conditional execution with "choice" command


Not sure this is doable but I'm trying to write the following batch script in a single line:

@echo off

echo Shutdown initiated...
echo.
choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
if errorlevel 2 goto EXEC
if errorlevel 1 goto ABORT

:EXEC
echo.
echo Computer shutting down 
timeout /t 10 
exit
:ABORT
echo.
echo Shutdown cancelled 
timeout /t 10
exit

The above script needs to be passed via the vbs run command into cmd. The following is the closest I can get it:

option explicit
dim wshell, strcmd

set wshell = createobject("wscript.shell")

if hour(now()) >= 0 and hour(now()) < 6 then
strcmd = "cmd /c @echo off & echo ""Pre-Dawn Shutdown initiated"" & echo. & choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X"""" & if errorlevel 2 goto exec & if errorlevel 1 goto abort & :exec & echo. & echo ""Computer shutting down"" & timeout /t 10 & exit & :abort & echo. & echo ""Shutdown cancelled"" & timeout /t 10 & exit"
wshell.run strcmd
end if

The above works as expected up until the choice command is reached then the script fails to execute the remainder correctly. Any help in resolving this is very much appreciated.


Solution

  • You can write your batch script in a single line having in mind that GOTO command directs a batch script to jump to a labelled line. Hence, you need to rewrite your script using IF… ELSE… command syntax as follows:

    @echo off
    
    echo Shutdown initiated...
    echo.
    choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""
    if errorlevel 2 (
      rem goto EXEC
      rem :EXEC
      echo.
      echo Computer shutting down 
      timeout /t 10 
      exit
    ) else if errorlevel 1 (
      rem goto ABORT
      rem :ABORT
      echo.
      echo Shutdown cancelled 
      timeout /t 10
      exit
    )
    

    For debugging purposes (i.e. to see output), in next oneliner is every exit replaced with pause:

    @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown press "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
    

    Output:

    ==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
    s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
    rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
    Shutdown initiated...
    
    To cancel shutdown press X X
    
    Shutdown cancelled
    
    Waiting for  0 seconds, press a key to continue ...
    Press any key to continue . . .
    
    ==> @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown pres
    s "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&pause) else if erro
    rlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10& pause )
    Shutdown initiated...
    
    To cancel shutdown press X Y
    
    Computer shutting down
    
    Waiting for  0 seconds, press a key to continue ...
    Press any key to continue . . .
    

    Next VBScript code snippet is slightly modified, again for debugging purposes.

    option explicit
    On Error GoTo 0
    Dim strResult: strResult = Wscript.ScriptName
    
    dim wshell, strcmd, Return 
    
    set wshell = createobject("wscript.shell")
    
    if True or (hour(now()) >= 0 and hour(now()) < 6) then
      strcmd = "cmd /c @echo Shutdown initiated...&echo." & _
        "&choice /c xy /n /t 10 /d y /m ""To cancel shutdown press ""X""""" & _ 
        "&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10" & _
        "&exit /B 2) else if errorlevel 1 (echo." & _
        "&echo Shutdown cancelled&timeout /t 10&exit /B 1)"
      strResult = strResult & vbNewLine & strcmd
      Return = wshell.run( strcmd, 1, True)
      strResult = strResult & vbNewLine & CStr( Return)
    end if
    
    'strResult = strResult & vbNewLine & 
    
    Wscript.Echo strResult
    

    Output: pressed X on the first run and Y (or nothing) on the later.

    ==> cscript //nologo D:\VB_scripts\SO\39313751.vbs
    39313751.vbs
    cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
    ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
    if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
    1
    
    ==> cscript //nologo D:\VB_scripts\SO\39313751.vbs
    39313751.vbs
    cmd /c @echo Shutdown initiated...&echo.&choice /c xy /n /t 10 /d y /m "To cancel shutdown p
    ress "X""&if errorlevel 2 (echo. &echo Computer shutting down&timeout /t 10&exit /B 2) else
    if errorlevel 1 (echo.&echo Shutdown cancelled&timeout /t 10&exit /B 1)
    2
    
    ==>