Forum Discussion

John_Purontong's avatar
John_Purontong
New Contributor
9 years ago

Cannot Run Powershell Script with Python

I am trying to 'Run PowerShell Script and Get the Result as Text'  PYTHON code snippet that can be found here https://support.smartbear.com/viewarticle/72616/  but I am getting the error below

 

code snippet

def Test():
oShell = Sys.OleObject["WScript.Shell"]
oExec = oShell.Exec("powershell -command Get-Process")
oExec.StdIn.Close() # Close standard input before reading output
# Get PowerShell output
strOutput = oExec.StdOut.ReadAll();
# Trim leading and trailing empty lines
strOutput = aqString.Trim(strOutput, aqString.stAll);
# Post PowerShell output to the test log line by line
aqString.ListSeparator = "\r\n";
for i in range(0, aqString.GetListLength(strOutput)):
Log.Message(aqString.GetListItem(strOutput, i))

 

AttributeError
'IDispatchIndexedPropertyWrapper' object has no attribute 'Close'
Error location:

 

I tried to run the same code snippet using VBScript and it executed successfully.

  • Dmitry_Nikolaev's avatar
    Dmitry_Nikolaev
    9 years ago

    Hello,

     

    I would like to let everyone know that the issue has been fixed some time ago and the fix is included to TestComplete version 11.31 that has been released on Tuesday.

     

    John, we were waiting for a case from you in order to provide the fix, but I was unable to find any requests from you. Did you contacted the Customer Care team regarding this issue?

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi John_purontong,

     

    I think that this line doesn’t return the PowerShell object. This is why it doesn’t have the Close method:

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

    You can check this when debugging your code.

     

    Also, please check that PowerShell is installed on your computer. Let me quote the article those link you published:

    PowerShell is included in Windows starting with Windows 7. For Windows Vista, Windows XP and Windows Server 2003, you can download PowerShell from here:

    http://support.microsoft.com/kb/968929

     

     

    • John_Purontong's avatar
      John_Purontong
      New Contributor

      Thanks for the reply.

       

      The oExec object is not null and Powershell comes pre-installed in my system and the code executed successfully with VBscript.

       

      I tried to run the same python code version in Testcomplete version 11.20 and it executed successfully.

      I am now using Testcomplete 11.30 and I noticed that some python code I have written in 11.20 are no longer

      working.

       

      Thanks and I am expecting  a quick response.

      • TanyaYatskovska's avatar
        TanyaYatskovska
        SmartBear Alumni (Retired)

        Hi John,

         

        I’ve got an update from our R&D Team – they had confirmed this issue, and they are ready to prepare a patch that will fix it. Could you please submit a support case so that our Team can start working on the issue with you? You can do this here: http://support.smartbear.com/product-list/

        Please specify the link to this community discussion in the case.

         

        Thanks in advance.