1

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.

Black
  • 18,150
  • 39
  • 158
  • 271
  • 1
    [Add the parentheses](http://stackoverflow.com/a/5413977/11683). – GSerg Aug 30 '15 at 12:41
  • Thank you for the hint. I replaced the run command with this: `myvar = myshell.run ("tasklist | find 'cmd.exe'")` but i always get the output `Output = 0`, regardless of whether `cmd.exe` is running or not. – Black Aug 30 '15 at 12:45
  • 1
    possible duplicate of [Getting command line output in VBScript (without writing to files)](http://stackoverflow.com/questions/5393345/getting-command-line-output-in-vbscript-without-writing-to-files) – GSerg Aug 30 '15 at 12:49
  • How is this a duplicate? – Black Aug 30 '15 at 12:50
  • 1
    It shows you how to grab the output of a command. `Run` simply [returns a status](https://msdn.microsoft.com/en-us/library/d5fk67ky%28v=vs.84%29.aspx). Please use documentation instead of guessing. – GSerg Aug 30 '15 at 12:53

1 Answers1

2

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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Hint for others: `When cmd transforms a command line and sees a ^, it ignores the ^ character itself and copies the next character to the new command line literally` – Black Aug 30 '15 at 14:09
  • Note: The `^` are totally unneccessary, you can also just use `objShell.Exec("cmd /C ""tasklist | find ""cmd.exe""""")` – Black Sep 04 '15 at 19:21