Forum Discussion

TestQA1's avatar
TestQA1
Frequent Contributor
10 months ago

How to pass parameters in WshShell.Run

Hi all,

I hope you all are doing well. 

I use powershell files alot in Javascript test complete scripts. 

But I am struggling to pass paramaters in string. 

var testingP = "some path";

var source = "some path";

var destination= "des path";

WshShell.Run("powershell -file \"" + testingP + "ABCD.ps1\"" & source & destination);

The powerhsell file executes if I dont add & source & destination but I need to pass parameters.

My PS file:

[CmdletBinding()]
param (
   $source, 
   $destination
)

function copy($source, $destination) {
    Copy-Item –Path $source -Destination $destination -Recurse
}

thanks

  • Use Log.Message() so that you can see what the output is going to be like. For example,

    Also, ensure you know the difference between + and & within a string.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Use Log.Message() so that you can see what the output is going to be like. For example,

    Also, ensure you know the difference between + and & within a string.

  • hi cold you please use below fromat try like this:

    param1 = "value1"
    param2 = "value2"

    result_string = "The parameters are: {} and {}".format(param1, param2)
    print(result_string)

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      This works fine if using Python.

      For JavaScript,

      function t()
      {
          param1 = "value1"
          param2 = "value2"
      
          result_string = `The parameters are: ${param1} and ${param2}`
          Log.Message(result_string)
      }
  • TestQA1's avatar
    TestQA1
    Frequent Contributor

    Thanks rraghvani and Babulu

    The arguments were passing fine but I have  function 'copy' in powerhsell script that is not able to accept the arguments for some reason. If I don't add the function and use Copy-Item directly, it works. So the issue is not from string side.

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      This is not correctly defined,

      WshShell.Run("powershell -file \"" + testingP + "ABCD.ps1\"" & source & destination);