Jump to content

Encapsulate VBS inside batch command.


Recommended Posts

First off let me state that I am not looking for how to call an external.vbs from a batch script, I know how to wscript/cscript. The question I am asking is how can we run native VBS code at the command line via cmd.exe arguments.

For instance with powershell we have the -command "" parameter and can pass rather complex one-liners via cmd.exe.

Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Directory\Background\shell\Open_Registry]"MUIVerb"="Open Registry""icon"="regedit.exe""Position"="Top""HasLUAShield"=""[HKEY_CLASSES_ROOT\Directory\Background\shell\Open_Registry\command]@="cmd /c start /wait /b powershell -command \"Clear-Host;$CLIPPY = PowerShell -NoProfile -STA -Command {[reflection.assembly]::loadwithpartialname('PresentationCore') | Out-Null;[Windows.Clipboard]::GetText()};$CLIPPY = $CLIPPY | Foreach-Object {$_ -replace 'HKCU', 'HKEY_CURRENT_USER'};$CLIPPY = $CLIPPY  | Foreach-Object {$_ -replace 'HKLM', 'HKEY_LOCAL_MACHINE'};$CLIPPY = $CLIPPY  | Foreach-Object {$_ -replace 'HKCR', 'HKEY_CLASSES_ROOT'};Set-ItemProperty -Path 'registry::HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit' -Name LastKey -Value $CLIPPY;regedit.exe /m;\"";

Am looking for similar functionality in VBS, does such a thing exist. Is there any built-in command line vbs parser.

cmd /c start /wait /b wscript -unknown "CreateObject("Wscript.Shell").Run """" & ".\DoSomething.ps1" & """", 0, False"

I don't see any cscript/wscript switch to do that trick.

Edited by MrJinje
Link to comment
Share on other sites

  • 3 months later...

I came up with a trick similar to the UNIX convention of starting script files with the name of the shell used to interpret the script. The difference is that this is built into how UNIX starts up files, while cmd/DOS has no such convention.

I create one file called: 'cscript.cmd

__________________________________

@cscript.exe //E:VBScript //NoLogo %*

__________________________________

The name has to start with a single quote, it has to be a batch/.cmd file, and it should be on your PATH.

Next I create my vbscript file, and call it something like HelloWorld.cmd. and put a special line at the top:

'cScript "%~f0" %*

followed by my vbscript. So it looks like this:

HelloWorld.cmd

__________________________________________

'cScript "%~f0" %*

WScript.Echo "Hello World!!"

For Each arg in WScript.Arguments.Unnamed

WScript.Echo "Arg(" & count & ") = " & arg

Next

__________________________________________

Why It Works

1.) Cmd reads HelloWorld.cmd and executes 'cscript.cmd, passing along HelloWorld.cmd as the first argument.

2.) Cmd executes 'cscript.cmd. It starts cscript passing it HelloWorld.cmd.

3. Cscript starts reading HelloWorld.cmd, and skips the first line since it starts with a single quote, a comment.

4.) Cscript goes on to execute the file as usual.

5.) When Cscript exits, cmd does not resume reading HelloWorld.cmd, because cmd has this quirk where if you execute a cmd file without using CALL, it never returns. Note with and exe, cmd resumes at the next line. Because of this cmd does not attempt to process the rest of the file, which is all vbscript.

In short it works because, the first line is interpreted as cmd file name, and then as a vbscript comment. And also because cmd never tries to read the second line which is vbscript.

There are probably issues with arguments that include special characters and quotes, but in practice it has not been a problem for me.

I have used this same trick with scripts for Beyond Compare, and also for autohotkey.

If you download the attachments, just rename the files to end with .cmd. Some virus checkers don't like cmd files.

'cscript.cmd.txt

HelloWorld.vbs.cmd.txt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...