How can I handle quotes in a WSH Exec method in VBscript

Using VBScript I need to capture output from a Windows command that has been parsed with Find.
The direct input to windows would be: netstat -na | Find "22"
Below is what I've tried to no avail.
What is the correct syntax?

strCmd = "netstat -na | Find " & Chr(34) & "22" & Chr(34)
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec(strCmd)
strOutput = objScriptExec.StdOut.ReadAll
WScript.Echo strOutput
jrslimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StefanLambdaCommented:
try this:   "  =  "" in VB

strCmd = "netstat -na | Find " & Chr(34) & """22""" & Chr(34)
Set objShell = CreateObject("WScript.Shell")
Set objScriptExec = objShell.Exec(strCmd)
strOutput = objScriptExec.StdOut.ReadAll
WScript.Echo strOutput
0
jrslimAuthor Commented:
Thanks Stephan, but that does not work.
0
StefanLambdaCommented:
It should be :
 strCmd = "netstat -na | Find ""22"""

But I cannot get the result !!!, I investigate on this
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jrslimAuthor Commented:
I found that adding cmd /C works in thius case: strCmd = "cmd /C netstat -na | Find " & Chr(34) & "22" & Chr(34)
0
jrslimAuthor Commented:
I am still interested to know a solution to this that does not include cmd /C so I will leave this open and points avaialble if you still want to work on it.
0
StefanLambdaCommented:
I didn’t find a solution without 'cmd /C'
To replace your Chr(34), you can use:
  strCmd = "cmd /C  netstat -na | Find ""80"""
But another idea: why not redirect to tmp file and read the file
  netstat -na | Find "22" > MyTempFile.txt
  MyTempFile.txt  can be replaced with Scripting.FileSystemObject and GetTempName function

Stefan
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
You cannot do this without using CMD /C.  You are basically piping the output of one command into another, and that capability is only available via the command line (or in batch files), which requires cmd.exe.

The basic VBS Exec() command is intended to run a single EXE program, so it can't handle the piping and other special things that CMD process does.

Hope that helps.

~bp
0
StefanLambdaCommented:
Yes, I think  it's not the best use the StdOut/StdIn or any pipe... the behavior can change following excute wscript.exe or cscript. Or …

I think it's better to redirect to a temp file and read the file.

Stefan
0
jrslimAuthor Commented:
Thanks Guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.