Forum Discussion

TestQA1's avatar
TestQA1
Frequent Contributor
3 years ago

Executing powershell file from Test Complete script

Hi Everyone,

I have a PickupTest.ps1 which triggers the software to launch. C:\_Test\abc\AutomatedScripts\My Tests\PickupTest.ps1

The below line of code sometimes works but most times the Powershell seems to open but the software does not launch. The problem seems to be the space in My Test in the path which I unfortunately cannot change at the moment.  

WshShell.Run("powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1")

The other alternative someone suggested was to create a temporary drive and pick up the file from there to avoid space issue, but it also does not seem to be working all the time.

WshShell.Run("powershell -file B:\PickupTest.ps1");

 

Can anyone help me deal with the space in the path when running powershell please?

 

Thanks in advance.

  • Hi,

     

    Assume that pathvar contains the 'C:\Project' value:

    your line of code

    "powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1"

    will evaluate to

    'powershell -file "C:\Project\My Tests\PickupTest.ps1'

    which seems to be just incorrect (lack of closing double-quote -- check this in debugger)

     

    Does it help if you change this line of code to this:

    WshShell.Run("powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1\"")

    (note added \" at the end of code line)

     

8 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Assume that pathvar contains the 'C:\Project' value:

    your line of code

    "powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1"

    will evaluate to

    'powershell -file "C:\Project\My Tests\PickupTest.ps1'

    which seems to be just incorrect (lack of closing double-quote -- check this in debugger)

     

    Does it help if you change this line of code to this:

    WshShell.Run("powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1\"")

    (note added \" at the end of code line)

     

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Thanks Alex, worked just fine with /"" in the end. Can you also please explain the use of /"" and how can I get information like this? The test complete documentation mostly gives basic examples such as it didn't give example of how to use variables in the path etc. which definitely makes sense as not everything could be covered by the documentation. I'm new to powershell and automation.

       

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        TestQA1 :

        Hi,

         

        Great to hear that the problem is solved.

         

        Can you also please explain the use of /""

        Not /"" but \"" 🙂

        This is not related to PowerShell or automation but to JScript/JavaScript language. (It was my assumption that you are using one of those scripting languages in your project.)

        Forward slash ( \ ) is a special symbol in JScript/JavaScript and its purpose is to indicate that the next character must be used as is.

        It is required in your case because """ (three quotes) are parsed by JScript runtime as an empty string (first two quotes) and the beginning of the next string (third quote). Quite often this results in incorrect syntax and runtime error.

        "\"" is parsed as a string (first and third quotes) that consists of a single double quote character (\" sequence) which is correct and thus does not cause runtime error.

         

        P.S. Considering, that single and double quotes are interchangeable in JScript, the above may be coded as

        '"'

        (single quote, double quote, single quote). This will work but is not always convenient.

        Read JScript documentation about strings, special characters and escaping for more details and better explanation.

         

        P.P.S.

        WshShell.Run("powershell -file B:\PickupTest.ps1");

        This line is also incorrect in JScript/JavaScript because slash is not doubled. Must be:

        WshShell.Run("powershell -file B:\\PickupTest.ps1");

        (note double slash).

         

         

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Thanks very much Alex, I will try and get back. script is running with the space but sometimes does not. So I will trying to find the permanent way to resolve space between 'My Test' which as per Powershell forum could be fix using '&'.

  • TestQA1's avatar
    TestQA1
    Frequent Contributor

    Great explanation, thanks. I'm using Javascript. 

  • Hi TestQA1 

     

    I hope you are keeping well.

     

    In the file path you may need to add the following character ` in between My Tests to be as follows;

     

    WshShell.Run("powershell -file \"" + pathvar + "\\My` Tests\\PickupTest.ps1")

     

    Please let me know if this resolves your query or if you have any additional questions.

     

    Thanks,

    Shane

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Thanks Shane. I will try and get back to you.

    • TestQA1's avatar
      TestQA1
      Frequent Contributor

      Didn't work. But I think '&' will solve the problem only if I can figure out how to use it in WshShell.Run("powershell -file \"" + pathvar + "\\My Tests\\PickupTest.ps1")