cancel
Showing results for 
Search instead for 
Did you mean: 

Getting PowerShell Return Code

SOLVED
Contributor

Getting PowerShell Return Code

I am having trouble getting the return code from a powershell script when I execute an external powershell script as a file.  I looked at the documentation linked here, but it wasn't much help in terms of getting me what I was looking for.

 

Anyone have a quick sample script (Preferably in VBScript, but I will take whatever) that does this?

1 ACCEPTED SOLUTION

Accepted Solutions
Contributor

Re: Getting PowerShell Return Code

Unfortunately when executing the code as you provided from within TestComplete it does not work because you are executing the powershell script and requesting the error level in two different interpreters.  Not only that, but they are executing in parallel.

 

What I ended up doing instead is this:

 

  strCMD = "powershell -file c:\test\Script.ps1"
  Set oShell=Sys.OleObject("WScript.Shell")
  rc=oShell.Run(strCMD, 0, True)
  Log.Message( "returncode=" & rc)
  

 

rc is the Return Code from the script, and as you can see, we are waiting for the result from the oShell.Run command before continuing.

View solution in original post

5 REPLIES 5
Community Hero

Re: Getting PowerShell Return Code

What happened when you tried it?

Contributor

Re: Getting PowerShell Return Code

There are no examples in that documentation on retrieving the exitcode of a powershell script.  I tried using the example that returns text and instead of this line:

 

Set oExec = oShell.Exec("powershell -command Get-Process")

 

I used this:

 

Set oExec = oShell.Exec("powershell -file c:\script\script.ps1")

 

But I did not get a return value.

Contributor

Re: Getting PowerShell Return Code

@Marsha_R any suggestions on how I might be able to tackle this?

Community Hero

Re: Getting PowerShell Return Code

You can get return as like below in command line

ps-return.PNG

 

I hope below script will do the same trick in TestComplete

Set oExec = oShell.Exec("powershell -file c:\script\script.ps1");
Log.Message(oShell.Run("cmd echo %errorlevel%");

Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”


Extension Available

Contributor

Re: Getting PowerShell Return Code

Unfortunately when executing the code as you provided from within TestComplete it does not work because you are executing the powershell script and requesting the error level in two different interpreters.  Not only that, but they are executing in parallel.

 

What I ended up doing instead is this:

 

  strCMD = "powershell -file c:\test\Script.ps1"
  Set oShell=Sys.OleObject("WScript.Shell")
  rc=oShell.Run(strCMD, 0, True)
  Log.Message( "returncode=" & rc)
  

 

rc is the Return Code from the script, and as you can see, we are waiting for the result from the oShell.Run command before continuing.

View solution in original post