1

How can one run a vbs command from cmd when the command is not saved?

For example to run a powershell command which has not been saved one can simply use powershell "get-childitem", where get-childitem is the command we want to use. We do not need to save a powershell file to run the command.

Let's say we want to run the vbs command Wscript.Echo Date() without saving a vbs file first, how can we do that?

I'm not looking to do this from a .bat file. I really am looking to embed a vbs script in a node.js script. I realize that this is not heavily reflected in the questions title :-/ I want to use exec and embed the vbs. It's important that there's no files other than the one js.

If I could run the vbs command from the cmd console without the use of any files then I could run the command from node.js without the need for any other files.

I don't think Is it possible to embed and execute VBScript within a batch file without using a temporary file? answers this as it's concerning using a batch file.

After the comments it looks like the way to go is using mshta What I am trying to do is to access the Windows index from a Node js script. Node Js allows me to exec cmd line commands. So I can directly execute this command powershell "$connector = new-object system.data.oledb.oledbdataadapter -argument \"SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE CONTAINS (System.FileName, '\"\"Google Chrome\"\" OR Cefclient.exe')\", \"provider=search.collatordso;extended properties='application=windows';\"; $dataset = new-object system.data.dataset; if ($connector.fill($dataset)) { $dataset.tables[0] }" which will return the result from the Windows index. The problem with this method is that it takes about 10 seconds just to get powershell running.

To do the same thing in vbs one can use

Set objConnection=CreateObject("ADODB.Connection")
Set objRecordSet=CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
objRecordSet.Open "SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE CONTAINS (System.FileName,'Chrome OR Cefclient.exe')",objConnection
objRecordSet.MoveFirst
Do Until objRecordset.EOF
Wscript.Echo objRecordset.Fields.Item("System.ItemPathDisplay")
objRecordset.MoveNext
Loop

This is much quicker. However the only way for me to run the vbs with nodejs seems to be by converting the code to a format that mshta accepts.

I am now trying to convert this to a valid mshta format. To do so I am using this snippet.

var vbs, before, after;
vbs = `Set objConnection=CreateObject("ADODB.Connection")
Set objRecordSet=CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"
objRecordSet.Open "SELECT System.ItemPathDisplay FROM SYSTEMINDEX WHERE CONTAINS (System.FileName,'Chrome OR Cefclient.exe')",objConnection
objRecordSet.MoveFirst
Do Until objRecordset.EOF
Wscript.Echo objRecordset.Fields.Item("System.ItemPathDisplay")
objRecordset.MoveNext
Loop`;
before = 'mshta "vbscript:window.close(execute("';
after = '"))"';

vbs = vbs.replace(/\n/g,':').
  replace(/"/g, '""').
  replace(/ /g, '"&chr(32)&"');
vbs = before + vbs + after;
$('#vbs').text(vbs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<code id="vbs"></code>

I can see that the conversion method is not fail-proof but can't see where it's going wrong in this case.

If anyone can get the above mshta to work it would be much appreciated.

Trevor
  • 425
  • 1
  • 4
  • 16
  • 1
    [This](https://stackoverflow.com/questions/36443703/is-it-possible-to-pipe-a-script-to-wscript), I think, is about as close as you will get. – JNevill Dec 11 '17 at 15:12
  • @JNevill could be, but I'm look for something with no vbs file at all. – Trevor Dec 11 '17 at 15:35
  • 2
    Possible duplicate of [Is it possible to embed and execute VBScript within a batch file without using a temporary file?](https://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a) – aschipfl Dec 11 '17 at 15:41
  • 1
    You can load the VBScript engine and feed it strings. Any COM language (but 32 bits without a lot of configuring) can do this. See https://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting – ACatInLove Dec 12 '17 at 01:03
  • 1
    Maybe [this](https://stackoverflow.com/a/25440385/2861476) could help you. – MC ND Dec 12 '17 at 06:58
  • @MCND That looks very much in the right direction. If you write it up as an answer and I'll mark it up and if all goes well after testing I'll mark it as correct. – Trevor Dec 12 '17 at 13:25
  • @ACatInLove looks interesting but I'm not sure how I could apply it in a node.js function – Trevor Dec 12 '17 at 13:27
  • 1
    I don't know what node is but if it's MS JavaScript this loads the engine `newObj = new ActiveXObject("MSScriptControl.ScriptControl")`. You can feed it JScript or VBScript. ANY COM language C++, VBA, VBScript, and COM based language. All .NET languages can do COM. – ACatInLove Dec 12 '17 at 19:22
  • Thanks ACatInLove Node.js is not MS JavaScript, I'll let you google to see what it is. It does look however that using MS JavaScript using the method @MCND mentioned looks the most promising method. https://stackoverflow.com/questions/45132747/unterminated-string-constant-mshtajavascript shows that when using mshta with JS one has much less problems with spaces and quotes. Of course the question refereed to vbs and not js but that looks like the way to go. – Trevor Dec 12 '17 at 21:30
  • VBScript/Jscript are interchangeable. `set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)` `With ScriptControl1` `.Language = "VBScript"` `.UseSafeSubset = False` `.AllowUI = True` `.AddCode Script` `End With` Var Script contains VB or JS code. – ACatInLove Dec 12 '17 at 23:24
  • It doesn't matter what node is, only if it's a COM language. – ACatInLove Dec 12 '17 at 23:25
  • There are three ways programs interact with windows. API calls, COM calls, and .NET framework. – ACatInLove Dec 12 '17 at 23:30
  • @MCND I updated my question to reflect the problem I'm having implementing the `mshta` method – Trevor Dec 13 '17 at 16:49
  • As JScript is the same as VBScript this is bizzare. – ACatInLove Dec 14 '17 at 13:48

1 Answers1

3

Sorry to revive this old post, but I've found a way to do this in pure Batch. It uses 'mshta.exe', a utility that is installed on most Windows versions by default. Here is an example:

mshta vbscript:Close(MsgBox("Hello, World!"))

This method does not write to disk. Further reading on 'mshta.exe' can be found here.

Hope this helps.

-Gabe

Gabriel Mills
  • 166
  • 2
  • 7