Forum Discussion

mortenb123's avatar
mortenb123
Contributor
10 years ago

powershell parameters in jscript

var otp = powershell_GetOTP("test4-back-01.bptest.local", "OTP:\s\d{4}.*\+4750505050");

How do I pass parameters to powershell without that much quoting?.

I currently run with default parameters in readlog.ps1:

 

function powershell_GetOTP(servername, rxfilter) {
  var oShell = Sys.OleObject("WScript.Shell");
  //this is not working how do we send parameters
  //var oExec = oShell.Exec("powershell C:\\dist\\ps\\readlog.ps1 -server servername -filter rxfilter");
  //setting default works
  var oExec = oShell.Exec("powershell C:\\dist\\ps\\readlog.ps1");
  oExec.StdIn.Close();
  var strOutput = oExec.StdOut.ReadAll();
  strOutput = aqString.Trim(strOutput, aqString.stAll);
  aqString.ListSeparator = "\r\n";
  Log.Message("Readlog.ps returned");
  for (var i = 0; i < aqString.GetListLength(strOutput); i++)
  {
    Log.Message(aqString.GetListItem(strOutput, i));
  }
  return strOutput;
}

Thanks

 

2 Replies

  • Hi Mortenb123,

     

    Please see Dmitry's reply here. Let me quote it here:

    To use quoted parameters, it is necessary, firstly, to add two quotation marks before the filename and after the last parameter, secondly, add the backslash symbol before and after the quoted parameter.

     

     

    • mortenb123's avatar
      mortenb123
      Contributor

      Hi

       

      I was unable to parse the regex correctly, so I changed the script just sending the phone-num and building the

      regexp in the powershell program, then it works fine, but it means I cant use our generic readlog.ps script.

       

      This is a generic parsing issue, since it did not work in cmd.exe either. PS is very convenient directly parsing any regexp.

       

      PS C:\dist\ps> .\getotp.ps1 -server "test1-back-01.bptest.local" -phone "95505050"
      7407

      PS C:\dist\ps> .\readlog.ps1 -server "test1-back-01.bptest.local" -filter "OTP:\s\d{4}.*\+4795505050"
      7407

      in cmd.exe (same from cygwin shell):
      c:\dist\ps>powershell  C:\dist\ps\readlog.ps1 -server "test1-back-01.bptest.local" -filter "OTP:\\s\\d\{4\}.\*\\+4795505050"
      At line:1 char:83
      + ... TP:\\s\\d\{4\}.\*\\+4795505050
      +                    ~
      Missing property name after reference operator.

       

      var otp = powershell_GetOTP("test1-back-01.bptest.local", "95505050");

       

       

      function powershell_GetOTP(servername, phone) {
        var script = "C:\\dist\\ps\\getotp.ps1";
        var oShell = Sys.OleObject("WScript.Shell");
        var ex = "powershell " + script + " -server \"" + servername + "\" -phone \"" + phone + "\"";
        Log.Message("executing:" + ex);
        var oExec = oShell.Exec(ex);
        oExec.StdIn.Close();
        var strOutput = oExec.StdOut.ReadAll();
        strOutput = aqString.Trim(strOutput, aqString.stAll);
        aqString.ListSeparator = "\r\n";
        Log.Message(script + " returned:");
        for (var i = 0; i < aqString.GetListLength(strOutput); i++)
        {
          Log.Message(aqString.GetListItem(strOutput, i));
        }
        return strOutput;
      }