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.