Forum Discussion

John_Laird's avatar
John_Laird
Contributor
9 years ago

Issue using powershell with TestComplete12

My goal is to start/stop services via powershell called from a TestComplete script (python). So I read (and attempted to apply) the info in this article to do just that:

 

https://support.smartbear.com/viewarticle/86333/

 

My script which fails:

def stopLogServ():
    WshShell.Run("start-process powershell.exe -verb runAs \"sc.exe stop asiLOGSERVER\"")
    WshShell.Run("start-process powershell.exe -verb runAs \"sc.exe stop asiLOGGER\"")

^Python interpeter returns "The System cannot find the file specified."

 

Everything works fine if I copy and paste the same script lines into a powershell prompt and execute them by hand. It simply does not work in TestComplete.

 

Thoughts, help on a resolution? Seems like this should be a common problem, just not sure how to fix.

 

Thanks,

John

  • John_Laird's avatar
    John_Laird
    8 years ago

    I tried this and it worked, probably because it uses the windows native command shell instead of powershell:

    def stopLogServ():
      Sys.OleObject["WScript.Shell"].Run("sc.exe stop asiLOGSERVER")
      Sys.OleObject["WScript.Shell"].Run("sc.exe stop asiLOGGER")

    Your suggestion to use this works as well:

    WshShell.Run("net stop asiLOGSERVER")

    I realized this morning that I was running TestComplete in administrator mode which is why the above works.

     

    Powershell with equivalent commands (sc.exe stop asiLOGGER) and TestComplete running in admin mode just throws access denied errors. Must be something different in how the powershell session is started from TestComplete.

     

    When I restart TestComplete without checking the box for administrator mode, the above commands fail. So I'll just run TestComplete in adminstrator mode and not use powershell with commands that need elevated permissions.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    John_Laird

    Hi John,

     

    > I copy and paste the same script lines into a powershell prompt [...]

    Underlined is a key.

    WshShell.Run() starts regular windows command enterpreter and 'start-process' is not a valid command for it but the valid command for powershell.

    Try either

    WshShell.Run("powershell.exe -verb runAs \"sc.exe stop asiLOGSERVER\"")

    or

    WshShell.Run("powershell.exe start-process powershell.exe -verb runAs \"sc.exe stop asiLOGSERVER\"")

     

    Hope it will help

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I've not used PowerShell within TestComplete but I have used the WshShell object to execute command lines within TestComplete and I've used Powershell.  I'm not sure, however, of the syntax of your commandlines. Based upon the article you linked to, the syntax of the command lines don't match how to execute commandlines in that document.


    I do know that an easy way to start/stop services via the WshShell is to simply send command as the "net" variety.  So, based upon what you posted, have you tried something like this?

     

    def stopLogServ():
        WshShell.Run("net stop asiLOGSERVER")
        WshShell.Run("net stop asiLOGGER")
    • John_Laird's avatar
      John_Laird
      Contributor

      I tried this and it worked, probably because it uses the windows native command shell instead of powershell:

      def stopLogServ():
        Sys.OleObject["WScript.Shell"].Run("sc.exe stop asiLOGSERVER")
        Sys.OleObject["WScript.Shell"].Run("sc.exe stop asiLOGGER")

      Your suggestion to use this works as well:

      WshShell.Run("net stop asiLOGSERVER")

      I realized this morning that I was running TestComplete in administrator mode which is why the above works.

       

      Powershell with equivalent commands (sc.exe stop asiLOGGER) and TestComplete running in admin mode just throws access denied errors. Must be something different in how the powershell session is started from TestComplete.

       

      When I restart TestComplete without checking the box for administrator mode, the above commands fail. So I'll just run TestComplete in adminstrator mode and not use powershell with commands that need elevated permissions.