Can I run vbscript commands directly from the command line (i.e. without a vbs file)?

In Python, you are not required to use a file, you can point -c "..."

and issue Python commands to the Python interpreter via a line on the command line.

Is it possible to achieve the same result with vbscript? I've seen solutions where you need to use the script package, but what if I'm on a system with zero write permissions?

Following the answer from @Syberdoor, I can run this:

mshta vbscript:Execute("dim result:result=InputBox(""message"",""title"",""input"")(window.close):echo result")

      

but it still doesn't output the result to the console.

+1


source to share


2 answers


There is one trick you can use and that is mshta.exe. You can execute the code like this:

mshta vbscript:Execute("<your code here, delimit lines with : (colon)>:close")

      



This is, of course, a fantastically insane hack and a system where you are not even allowed to create a file. I'm not sure if mshta.exe is allowed.

Perhaps you can also find additional inspiration from this thread (mshta's solution is also posted there). While mostly bundled with it, this is a great compendium of some really crazy ways to trick windows into executing vbs code.

+4


source


No, the translators supplied with Windows ( wscript.exe

and cscript.exe

) do not support this. If you can't create the file anywhere, you're out of luck. You will need a wrapper script to convert the argument to something that VBScript interpreters can do.

The na & iuml; ve would create a VBScript with an expression ExecuteGlobal

like this:

With WScript.Arguments.Named
  If .Exists("c") Then ExecuteGlobal .Item("c")
End With

      

However, this will not work correctly because double quotes are not stored in the arguments. If you run the above script like this:

C:\> vbsrunner.vbs /c:"WScript.Echo "foo""
      

it would efficiently execute the operator WScript.Echo foo

instead WScript.Echo "foo"

, and I was unable to find a way to escape the nested double quotes so that they are stored in the argument.



A batch script will work that writes the argument to a temporary file and then executes that file using the VBScript interpreter:

@echo off

setlocal

set "tempfile=%TEMP%\%RANDOM%.vbs"

>"%tempfile%" echo.%~1
cscript.exe //NoLogo "%tempfile%"
del /q "%tempfile%"

      

Thus, you can run VBScript statements on the command line like this:

C:\> vbsrunner.cmd "WScript.Echo "foo" : WScript.Echo "bar""
foo
bar
      

If you want to replicate Python interactive mode, you can use vbsh

, but that would still require the ability to create a file somewhere.

+1


source







All Articles