Kleber Ramos
Kleber Ramos

Reputation: 11

Pipeline script vbs

I created the following script, but it only returns 0, even finding errors in DNS test.

Dim consult, objShell

Set objShell = WScript.CreateObject ("WScript.shell")

consult = objShell.run ("dcdiag /test:DNS | findstr /i failed", 0)

If consult = "0" THEN
    WScript.Echo "OK"
else
    WScript.Echo "ERROR"
end If

I understand that is not running the findstr.

Thank attention.

Upvotes: 1

Views: 556

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

  • You don't use "Option Explicit"; that's reckless.
  • You don't init near/immediately after your Dims; that's error prone.
  • You use variables (objShell) just once; that's wasteful.
  • You mix data types (integer vs string) in a comparison; that's stupid.
  • You don't read the Docs (carefully); that's impertinent.

From the Docs:

bWaitOnReturn

Optional. Boolean value indicating whether the script should wait for the program to finish executing before continuing to the next statement in your script. If set to true, script execution halts until the program finishes, and Run returns any error code returned by the program. If set to false (the default), the Run method returns immediately after starting the program, automatically returning 0 (not to be interpreted as an error code).

To avoid unnecessary problems caused by missing dcdiag or different versions of findstr or mis-understanding/use of those programs, I use "minimal errorlevel setters":

type ex0.vbs, ex1.vbs

ex0.vbs
WScript.Quit 0

ex1.vbs
WScript.Quit 1

and this code:

Option Explicit

Dim consult : consult = WScript.CreateObject("WScript.Shell").Run("%comspec% /c ex0.vbs | ex1.vbs", 0, True)

If consult = "0" THEN
    WScript.Echo consult, "OK"
else
    WScript.Echo consult, "ERROR"
end If

output:

cscript 36531325.vbs
1 ERROR

Use

.Run("ex0.vbs | ex1.vbs", 0, True)

to see that you need a shell (%comspec%) to use a shell's features (|) and

.Run("%comspec% /c ex0.vbs | ex1.vbs", 0)

for understandig the importance of the bWaitOnReturn parameter.

Upvotes: 0

Related Questions