我目前正在构建一个小应用程序,用于从HTML表单中检索信息(实际上是多个复选框),并将这些信息作为args传递给powershell exec命令.
目前我得到的是这样的数组:
Array
(
[0] => Group1
[1] => Group2
[2] => Group3
[3] => Group4
[4] => Group5
[5] => Group6
)
起初我想把这个数组放在CSV文件中,然后从powershell脚本中读取这个CSV,但我想知道是否有一种方法可以直接在shell_exec中使用这个数组作为arg(powershell格式化的数组@("Group1","Group2")
我的php(其中$arg2是上面的数组):
<?php
$Psscript = "C:\Scripts\script.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");
?>
有没有人成功地让这样的事情工作?
感谢您的任何建议,如果您需要更多的信息,请不要犹豫,询问;)
编辑:当前php
$Psscript = "C:\Scripts\script.ps1";
$arg1 = $_POST["arg1"];
$arg2 = $_POST["arg2"];
$groups = '@("'. implode('","', $_POST["groups"]) .'")';
echo $groups;
$result = shell_exec("powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command $Psscript $arg1 $arg2 $groups 2>&1");
echo $result;
}
?>
Powershell:
###### Args ######
$arg1 = $args[0];
$arg2 = $args[1];
$groups = $args[2];
$fullname = "$arg1.$arg2"
if (!($session)) {$session = New-PSSession -ComputerName 'srv01' -ConfigurationName "Microsoft.PowerShell32" -Credential $cred}
Invoke-Command -Session $sessionAD -ScriptBlock {
Import-Module ActiveDirectory
foreach ($group in $using:groups)
{
Add-ADGroupMember -Identity "$group" -Members "$using:fullname"
}
}
Remove-PSSession $sessionAD