skip to Main Content

I’m currently building a little app to retrieve information from an HTML Form (multiples checkboxes actually) and pass this information as args to a powershell exec command.

Currently I get this kind of array as a result :

Array
(
    [0] => Group1
    [1] => Group2
    [2] => Group3
    [3] => Group4
    [4] => Group5
    [5] => Group6
)

At first I wanted to put this array in a CSV file and then read this CSV from the powershell script but I wonder if there is a way to use this array directly in my shell_exec as an arg (powershell formatted array @("Group1","Group2"))

My php (where $arg2 is the above array):

<?php
$Psscript = "C:Scriptsscript.ps1";
$arg1 = $_POST["data1"];
$arg2 = $_POST["data2"];
echo '<pre>'; print_r($arg2); echo '</pre>';

shell_exec("powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command $Psscript $arg1 $arg2");
?>

Has anybody succeeded in making thing like this work ?

Thank you for any suggestions and please don’t hesitate to ask if you need more information 😉

2

Answers


  1. Using implode plus a bit of string concatenation on either end you can fairly easily produce the format you specified, ready for insertion into the powershell command string:

    $arg2 = array
    (
        0 => "Group1",
        1 => "Group2",
        2 => "Group3",
        3 => "Group4",
        4 => "Group5",
        5 => "Group6"
    );
    
    echo '@("'.implode('","', $arg2).'")';
    

    outputs:

    @("Group1","Group2","Group3","Group4","Group5","Group6")
    

    Demo: https://3v4l.org/lhn38

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search