Reputation: 20302
I created a very simple script for exercising purposes. I simply try to output the result of my command myshell.run "tasklist | find 'cmd.exe'"
. But it fails with an error message. The error message is german 'Anweisungsende erwartet' in english it should mean 'statement completion expected'
set myshell = CreateObject("WScript.Shell")
myvar = ""
myvar = myshell.run "tasklist | find 'cmd.exe'"
MsgBox "Output = " & myvar
I expect output like this:
cmd.exe 5076 Console 1 3.156 K
Update:
I tried it again with the information on the link of the possible duplicate.
I have to use Exec
instead of Run
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /C tasklist | find 'cmd.exe'")
strText = ""
Do While Not objExecObject.StdOut.AtEndOfStream
strText = strText & objExecObject.StdOut.ReadLine()
Loop
Wscript.Echo strText
Result: No output, even though the cmd.exe is running. I think it is not possible to execute it if there is a pipe symbol. If i use only cmd /C tasklist
then it outputs all the tasks.
Upvotes: 1
Views: 2614
Reputation: 78185
If you want to pass multiple commands for cmd
to execute as one block, you need to put quotes around them.
You must also respect quote escaping rules that one must use for CMD, because one of your block commands, find
, also uses quotes.
And finally, you must remember to escape the resulting CMD-escaped quotes according to the language rules (VBScript).
In the end you should have:
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /C ""tasklist | find ^""cmd.exe^""""")
strText = ""
Do Until objExecObject.StdOut.AtEndOfStream
strText = strText & objExecObject.StdOut.ReadLine()
Loop
Wscript.Echo strText
As a debugging hint, when you don't get a result from StdOut
, you should also check StdErr
in the same way.
Upvotes: 2