1

Getting error using ConvertFrom-Json in Powershell for an incoming PHP parameter (json_eoncoded array) using the EXEC function in PHP

PHP Code:


$psPath = "powershell.exe -InputFormat none -ExecutionPolicy ByPass  -NoProfile ";
$psDIR = "C:\\xampp\\htdocs\\";
$psScript = "ps_scripts.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1 "; 


$parameter_2 = "test";
$array = array("Peter"=>24, "Ben"=>"Hello");
$encoded_data = json_encode($array);
exec($runCMD . "example_function_name \"" . $encoded_data . "\"" . " \"" . $parameter_2 . "\"", $output, $retval);

Powershell Code: ps_scripts.ps1

$arguments = $args;
function example_function_name
{
  $sData = $arguments[1] | ConvertFrom-Json
  echo $sData;
}

# Invoking the function
$sb = (get-command $args[0] -CommandType Function).ScriptBlock
invoke-command -scriptblock $sb 

Error:

Array ( [0] => At line:1 char:70 [1] => + ... htdocs\ps_scripts.ps1 example_function_name {Peter:24,Ben:Hell ... [2] => + ~ [3] => Missing argument in parameter list. [4] => + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException [5] => + FullyQualifiedErrorId : MissingArgument [6] => ) 1
Manu
  • 23
  • 5

0 Answers0