How to check the exit code after WScript.Quit

Hello everyone,

Here's basicly what my script looks like.

WScript.Echo("Hello", "world")
WScript.Quit 1
WScript.Echo("You should never see this.")

That "1" is what I believe to ba an exit code.

How do I check for its value ?

I have a .vbs script(two) that is called from another .vbs(one) script.
If .vbs script (two) fails,  I want to stop it with a specific exit code.

Then in the calling .vbs script(one), I want to evaluate the exit code value and display a specific message based it the value.

Any idea how I might evaluate the exit code ?

Accidental Hyper-V AdministratorAsked:
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.

How are you calling the second script from the first
Mike TomlinsonMiddle School Assistant TeacherCommented:
Check out the ExitCode() property of the returned object from the Exec() method in the WScript.Shell class:
Accidental Hyper-V AdministratorAuthor Commented:
First,  thanks for both responses.

As to the question from hes:

In .vbs script one,  I have the following line of code which calls, or executes,  .vbs script two. (named GetPDBs.vbs)
WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True

At several places within .vbs script two(GetPDBs.vbs),  things could go wrong.

I am writing lines into a log file on the hard drive, as .vbs script two runs, so if any failures occur,  I know what the failures are and where, within .vbs script two,  they occurred.

My goal is,  to stop .vbs script two when the failure occurs AND .vbs script one as well.

I CAN successfully stop .vbs script two by coding "WScript.Quit 1".

But of course,  control is returned to .vbs script one,  and .vbs script one continues on its merry way.

MY thinking,  was to,  add some lines of code to .vbs script one,  right after the "WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True" line,  that could check for the exit code generated by the "WScript.Quit 1" line in .vbs script two.

Then,  if it were "1" or something other than "0",  execute another "WScript.Quit 1" which would bring .vbs script one to a halt.

Make sense ?

If there is another way to accomplish the same thing,  I'm open to,  and welcome,  any other suggestions.

Second,  to Idle Mind's comment:

Thanks for the link.

I'm new to vbs scripting.
WAY new.

I truly appreciate your efforts to help me,  but the information and examples are over my head.

I'm just plain, "not that smart yet",  to be able to read those example and apply anything from them to MY issue.

(Now you know why I need to be a member of this forum.)

I'll get there.
I'm just not there at this point in my scripting career.
Thanks again.

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mike TomlinsonMiddle School Assistant TeacherCommented:
No problem...    =)

Here is a simple example:

' ----------------
' Script2.vbs
' ----------------
WScript.Echo "Hello world!"
WScript.Quit 1
WScript.Echo "You should never see this."

' ----------------
' Script1.vbs
' ----------------
Dim WshShell, oExec

' launch the second script via "CScript"...  (assuming Script2.vbs is in the same directory)
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("CScript Script2.vbs")

' wait for second script to exit...
Do While oExec.Status = 0
   WScript.Sleep 100

' do something based on the exit code...
Select Case oExec.ExitCode
    Case 0
        WScript.Echo "Continuing first script..."
        ' ...more code here...
    Case 1
        WScript.Echo "Second script had an Error!"
        WScript.Quit 1

    Case Else
        WScript.Echo "Something else happened..."
        ' ...more code here...

End Select

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
Return = WshShell.Run "cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True
Select Case Return
End Select

The True as the last parameter means
Wait for the command to complete before continuing execution of the wsh script.
Accidental Hyper-V AdministratorAuthor Commented:
I can't thank you folks enough.

Things are working perfectly now.

I NEVER(at least not for some time) would have thought of the CASE solution.

You folks truly have made my day.
My entire week end in fact.

So,  here's how I used the information.

In script number two,  after handling the error condition(i write an audit line into a text file on the hard drive),  I end the script with the following 'WScript.Quit (1)

Then,  in script number one (this calls script number two) I do the following which,  thanks to you good folks,  now ends script number one like I had hoped for.

Return = WshShell.Run ("cscript " & BASE_CURRENT_PATH & "GetPDBs.vbs", 1, True)

Select Case Return
  Case 1
End Select  


You folks are the greatest.

Thanks again and take care.

Ray in Wisconsin
Glad to have helped
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
Programming Languages-Other

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.