Forum Discussion

DavidE's avatar
6 years ago

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?

  • DavidE's avatar
    6 years ago

    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.

5 Replies

    • DavidE's avatar

      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.

      • shankar_r's avatar
        Community Hero

        You can get return as like below in command line


        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%");