0

I am simply trying to get a success or failure code out of a PowerShell script when run via VBScript with CreateObject("WScript.Shell").Run(strCMD, 0, True).

I thought Exit produced an exit code, but when I end my PS script with Exit 9 the value returned to the VBScript above is still 0.

The VBScript is:

strCmd = "powershell.exe -noLogo -executionpolicy bypass -file ""\\Mac\Px\Support\Px Tools\Dev 3.3.#\_Spikes\TestMessage.ps1"" -message:""I'm ALIVE"""
result = CreateObject("WScript.Shell").Run(strCMD, 0, True)
msgBox result

3 Answers 3

1

Use Return "Your Value" in your ps script instead of Exit

Take care powershell will return anything what will be printed to the console

Sign up to request clarification or add additional context in comments.

2 Comments

guiwhatsthat, I have no tried Exit, Return and Write-Host. None of which seem to work with VBScript's .Run method. Are these actually working for you? FWIW, I am using PS 2.0, because I must. I guess I need to spin up a Windows 10 VM and test this in current PS. Maybe it's juts a bug in PS 2.0.
I made a quick example with powershell. I got a returnvalue with return and with exit i did not got one. Note Return will no end your script
0

Try this:

Set wso=CreateObject("WScript.Shell")
rc=wso.Run(strCMD, 0, True)
MsgBox "returncode=" & rc

5 Comments

That's basically what I am doing. My my variables are different, and I am inlining the CreateObject, but it's the same functionality. I updated the OP to show the actual code.
Tested with simplest .ps1 (exit 9) and it works. What's in your .ps1?
I have tested with just Exit 9 also. Still no joy, the only thing getting returned is 0. BTW, how are you getting the Code formatting in comments? ;)
Oh, wait a minute. I had a blank line before that Exit. With literally one line, it works. As soon as I introduce any non comment code it stops working. But that gives me something to start from. I can start adding bits of my required code till it breaks.
Finally found it. I had reused the main PS code from a utility I already had written, and I had [CmdletBinding()] in there. No need in a script rather than a function, and it seems it causes all sorts of other problems.
0

Just write

exit 123 

in TestMessage.ps1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.