skip to Main Content

Until recently my PHP script calling a Powershell script with arguments was working fine, but now the string supposedly is in this format @("grp1","grp2") on the Powershell side it is now grp1 grp2

The echo on PHP’s side before submission to Powershell is @("grp1","grp2")

The $_POST["groups"] is an array.

Here is my PHP code:

<?php

$ADJoinGroups = "C:ScriptsAD_Joingrps.ps1";

$entity = $_POST["entity"];
$fName = $_POST["fName"];
$lName = $_POST["lName"];
$desc = $_POST["description"];
$password = "#####";

$groups = '@("'.implode('","',$_POST["groups"]).'")';

echo '<pre>'; print_r($groups); echo '</pre>';

if (isset($_POST['cGrps'])) {
$res1 = shell_exec("powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command $ADJoinGroups $fName $lName $groups 2>&1");
echo $res1;
}

?>

The Powershell script looks as follows:

###### Args ######
$fName = $args[0];
$lName = $args[1];
$groups = $args[2];

Write-output $groups;

Does anyone know why this happens? And, do you have a way to revert it to @("grp1","grp2") from grp1 grp2 in Powershell?

Many thanks 🙂

2

Answers


  1. Chosen as BEST ANSWER

    Thanks @Professor Abronsius you made me on the right path. Single quotes around my $groups made it

    shell_exec("powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command $ADJoinGroups $fName $lName '$groups' 2>&1");```
    

  2. The test scripts I ran to emulate the original script with the difference of the groups portion of the commandline string being enclosed within double quotes and group names within single quotes.

    <?php
    
        $ADJoinGroups = "C:dataArchivesScriptsPowershellAD_Joingrps.ps1";
        
        $_POST["entity"]='Corporate Chief';
        $_POST["description"]='Something...or...other';
        $_POST["fName"]='John';
        $_POST["lName"]='Smith';
        $_POST['groups']=array( 'printusers', 'powerusers', 'cor-c-dc-telstar1', 'cor-c-dc-frontline' );
        
        
        $entity = $_POST["entity"];
        $fName = $_POST["fName"];
        $lName = $_POST["lName"];
        $desc = $_POST["description"];
        $password = "#####";
        
        # individual groups within single quotes
        $groups=sprintf( "@('%s')", implode( "','", $_POST['groups'] ) );
        
        # entire groups portion within double-quotes
        $cmd=sprintf('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command %s %s %s "%s" 2>&1', $ADJoinGroups, $fName, $lName, $groups );
        $res=shell_exec( $cmd );
        
        
        
        printf( '<pre>%s</pre>', print_r( $res,true ) );
    
    ?>
    

    Powershell test script:

    function adUserGroups{
        [CmdletBinding()]
        param (
            [Parameter(Mandatory=$true)][String] $fName,
            [Parameter(Mandatory=$true)][String] $lName,
            [Parameter(Mandatory=$true)][Array] $groups
        )
        Write-output $fName, $lName;
        
        foreach( $group in $groups ){
            write-output "Add to Group->$group"
        }
        
    }
    
    adUserGroups -fName ($args[0]) -lName ($args[1]) -groups ($args[2])
    

    Output in browser:

    John
    Smith
    Add to Group->printusers
    Add to Group->powerusers
    Add to Group->cor-c-dc-telstar1
    Add to Group->cor-c-dc-frontline
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search