Forum Discussion

jkrolczyk's avatar
jkrolczyk
New Contributor
23 days ago

How to run a powershell script from TestComplete with parameters ?

Maybe I am missing this in the documentation?

Running PowerShell Scripts From TestComplete | TestComplete Documentation

If possible, I was trying to find the syntax to go by for passing in single or multiple parameters.

 

 

 

  • jkrolczy's avatar
    jkrolczy
    23 days ago

     

    function Test()
    {  
      // Create a WshShell object
      var WshShell = new ActiveXObject("WScript.Shell");  
      
      // Path to your PowerShell script
      var scriptPath = "C:\\qTestReport\MyScript.ps1";  

      // Parameters to pass to the script
      var param1 = "Bob";
      var param2 = "12";

      // Construct the command line string
      var command = "powershell -File \"" + scriptPath + "\" " + param1 + " " + param2;
      
      Log.Message(command);

      // Execute the PowerShell script with parameters
      WshShell.Run(command, 0, true); 
    }

     

    MyScript.ps1

    param 
    (
        [string]$Name,
        [int]$Age
    )

    $jsonBody = "C:\qTestReport\ESP\qTestResults\finalBody.txt"

    Write-Output "Hello, $Name! You are $Age years old."

    $Name | Out-File -FilePath $jsonBody
    Add-Content -Path $jsonBody $Age


    But file is not getting generated with passed in values


  • rraghvani's avatar
    rraghvani
    Champion Level 3

    The example shown in the link that you have provided,

    // Run one command.
    getActiveXObject("WScript.Shell").Run("powershell -command echo Test");

    powershell is the executable, and -command echo Test is the parameter. The entire command is enclosed in quotes, meaning its a string. Therefore, you can do string manipulation

     

  • jkrolczy's avatar
    jkrolczy
    Regular Contributor

    Going to try this:

    function Test()
    {  
      // Create a WshShell object
      var WshShell = new ActiveXObject("WScript.Shell");  
      
      // Path to your PowerShell script
      var scriptPath = "C:\\path\\to\\your\\script.ps1";  

      // Parameters to pass to the script
      var param1 = "value1";
      var param2 = "value2";

      // Construct the command line string
      var command = "powershell -File \"" + scriptPath + "\" " + param1 + " " + param2;

      // Execute the PowerShell script with parameters
      WshShell.Run(command, 0, true); 
    }

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      Providing your command is correct, it will work. You can check your string is correctly formed, if you were to output the command to the log,

      Log.Message(command)
      • jkrolczy's avatar
        jkrolczy
        Regular Contributor

         

        function Test()
        {  
          // Create a WshShell object
          var WshShell = new ActiveXObject("WScript.Shell");  
          
          // Path to your PowerShell script
          var scriptPath = "C:\\qTestReport\MyScript.ps1";  

          // Parameters to pass to the script
          var param1 = "Bob";
          var param2 = "12";

          // Construct the command line string
          var command = "powershell -File \"" + scriptPath + "\" " + param1 + " " + param2;
          
          Log.Message(command);

          // Execute the PowerShell script with parameters
          WshShell.Run(command, 0, true); 
        }

         

        MyScript.ps1

        param 
        (
            [string]$Name,
            [int]$Age
        )

        $jsonBody = "C:\qTestReport\ESP\qTestResults\finalBody.txt"

        Write-Output "Hello, $Name! You are $Age years old."

        $Name | Out-File -FilePath $jsonBody
        Add-Content -Path $jsonBody $Age


        But file is not getting generated with passed in values