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

冷暖自知 提交于 2019-12-02 07:23:48
Syberdoor

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

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

This is of course a fantasticly insane hack and on a system where you are not even allowed to create a file I am not sure if mshta.exe would be allowed.

Maybe you can also find additional inspiration from this thread (the mshta solution is also posted there). Although mostly batch related it is imo a great compendium of several really crazy ways to fool windows into executing vbs code.

No, the interpreters shipped with Windows (wscript.exe and cscript.exe) don't support that. If you can't create a file anywhere you're out of luck. You need some kind of wrapper script to transform the argument into something that VBScript interpreters can execute.

The naïve approach would be to create a VBScript with an ExecuteGlobal statement like this:

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

However, that won't work correctly, because double quotes aren't preserved in the arguments. If you ran the above script like this:

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

it would effectively execute the statement WScript.Echo foo instead of WScript.Echo "foo", and I was unable to find a way to escape nested double quotes so that they're preserved in the argument.

What will work is a batch script that writes the argument to a temporary file, and then executes that file with a VBScript interpreter:

@echo off

setlocal

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

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

That way you can run VBScript statements on the command line like this:

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

If you wanted to replicate Python's interactive mode you could use my vbsh, but that would still require the ability to create a file somewhere.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!