none
Calling a Powershell script with multi params from ASP.NET using Process.Start() RRS feed

  • Question

  • Hi, 

    I've been trying to call a Powershell script with multi params from an ASP.NET file in the following way.

    Process.Start(@"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe", @"-File ""E:\\Testman.ps1"" ""'MAC1','MAC2'"" ""4.0"" ""xxx.xxx.xxx.xx""");

    (Please note that here I have two inputs for the macList)

    But while running, this is taking MAC1,MAC2 as a single param only. Here's the parameter list of Testman.ps1:

    Param
    (
    [Parameter(Mandatory=$True,Position=1)]
    [String[]]$macList,
    [Parameter(Mandatory=$True,Position=3)]
    [String[]]$version,
    [Parameter(Mandatory=$True,Position=4)]
    [String]$server
    )

    Please help me fixing this issue.

    Thanks in advance,

    /Aravind

    Tuesday, November 18, 2014 7:22 AM

Answers

  • Here is how to stage the parameter stringin C#:

    string exe = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe";
    string template = @"-File {0} -maclist {1} -version {2} -server {3}";
    string file = @" E:\Testman.ps1";
    string maclist = "MAC1,MAC2";
    string version = "4.0";
    string server = "xxx.xxx.xxx.xxx";
    string params = String.Format(template, file, maclist, version, server);
    Process.Start( exe, params);
    
    THe "maclist" can be asemblied from an array uisng the String.Join() method.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, November 18, 2014 11:04 AM
    • Proposed as answer by jrv Wednesday, November 19, 2014 10:11 AM
    • Marked as answer by a4aravind Wednesday, November 19, 2014 11:07 AM
    Tuesday, November 18, 2014 11:03 AM
  • Hi jrv,

    For me, changing the -File switch to -Command did the trick. 

    string template = @"-Command {0} -maclist {1} -version {2} -server {3}";

    Courtesy: http://stackoverflow.com/questions/27009495/calling-a-powershell-script-with-multi-params-from-asp-net-using-process-start/27009602#27009602

    Thanks.

    • Marked as answer by a4aravind Wednesday, November 19, 2014 8:45 AM
    Wednesday, November 19, 2014 8:44 AM

All replies

  • You can try changing:

    [Parameter(Mandatory=$True,Position=1)]
    [String[]]$macList,

    To

    [Parameter(Mandatory=$True,Position=1)]

    [Object[]]$macList,

    [string] won't accept any array. 

    Tuesday, November 18, 2014 10:09 AM
  • Your question iss about C# and not about PowerShell.

    You are calling with a string.  YOu will not get an array.  [string[]] is a string arrray you need to declare it as such in C#.

    string s1 = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe";
    string s2=@"-File E:\Testman.ps1 -maclist MAC1,MAC2 -version 4.0 -server xxx.xxx.xxx.xx";
    Process.Start( s1, s2);

    Note that the argumenets can all be inone string and should not contain quotes. PowerShell knows howto parse this string. After -maclist we only want a comma separated list of strings. Just pretend you are typing it a commandline.

    To buid a string in C# use the string formatter.

    in powerShell  '@' denotes a collection (array or hash) but in C it declares a static string.  This menas youdon't need double slashes.

    For C# questions post in C# developers forrum.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, November 18, 2014 11:04 AM
    Tuesday, November 18, 2014 10:50 AM
  • You can try changing:

    [Parameter(Mandatory=$True,Position=1)]
    [String[]]$macList,

    To

    [Parameter(Mandatory=$True,Position=1)]

    [Object[]]$macList,

    [string] won't accept any array. 


    Nice guess but it has nothing to do with this.  [string[]] is a legitimate array in PowerShell.  A declaration with [string[]] array will accept a string or an array of strings or attempt to convert an array of anything into an array of strings.

    ¯\_(ツ)_/¯

    Tuesday, November 18, 2014 10:53 AM
  • Here is how to stage the parameter stringin C#:

    string exe = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe";
    string template = @"-File {0} -maclist {1} -version {2} -server {3}";
    string file = @" E:\Testman.ps1";
    string maclist = "MAC1,MAC2";
    string version = "4.0";
    string server = "xxx.xxx.xxx.xxx";
    string params = String.Format(template, file, maclist, version, server);
    Process.Start( exe, params);
    
    THe "maclist" can be asemblied from an array uisng the String.Join() method.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, November 18, 2014 11:04 AM
    • Proposed as answer by jrv Wednesday, November 19, 2014 10:11 AM
    • Marked as answer by a4aravind Wednesday, November 19, 2014 11:07 AM
    Tuesday, November 18, 2014 11:03 AM
  • Hi jrv,

    Thanks but still it's taking it as a single input only. And sorry i didn't get that, what you meant by 'THe "maclist" can be asemblied from an array uisng the String.Join() method'.

    Thanks,

    /Aravind

    Wednesday, November 19, 2014 4:47 AM
  • Sorry - I ran it and it works as I noted.

    You can assemble a string from an array with a join.

    In all cases this is not a scripting issue. You need to post C# quesitons in the C# developers forum.


    ¯\_(ツ)_/¯

    Wednesday, November 19, 2014 4:51 AM
  • Hi jrv,

    For me, changing the -File switch to -Command did the trick. 

    string template = @"-Command {0} -maclist {1} -version {2} -server {3}";

    Courtesy: http://stackoverflow.com/questions/27009495/calling-a-powershell-script-with-multi-params-from-asp-net-using-process-start/27009602#27009602

    Thanks.

    • Marked as answer by a4aravind Wednesday, November 19, 2014 8:45 AM
    Wednesday, November 19, 2014 8:44 AM
  • What is in your argument?  It looks like a script and not a file pathname.


    ¯\_(ツ)_/¯

    Wednesday, November 19, 2014 10:11 AM