2016-10-17 55 views
0

我有一個HTML表單與一些PHP來執行一個PowerShell腳本。在php中的shell_exec的問題

所有工作正常,除了textarea輸入字段。如果我在textarea框中做了一行文本,它可以正常工作並啓動PowerShell腳本。如果我插入多行文本(包括回車符),它只會佔用第一個單詞/行並切除其餘部分。

下面是啓動PowerShell腳本命令:

在textarea的箱一個字:

powershell -command file.ps1 -textarea 'test' 

用方框

powershell -command file.ps1 -textarea 'firstword 

多個單詞如上所示,當TextArea字段中有多行時,它會將其剝離並僅發送第一個字。這會導致PowerShell腳本失敗,因爲終止符丟失。

任何想法?

下面是代碼:

if($isValid) { 
$customerid  = $_POST["DLState"]; 
$changesubject = $_POST['subject']; 
$starttime  = $_POST['starttime']; 
$endtime  = $_POST['endtime']; 
$changedetails = $_POST['details']; 
$psScriptPath = "*****"; 

$query = shell_exec("powershell -command $psScriptPath -username '$user' -subject '$changesubject' -orgid '$customerid' -textarea '$changedetails' -edt '$endtime' -sdt '$starttime' < NUL"); 
    } 

HTML文本區域標籤

<textarea id="txtarea" name="details" cols="40" rows="6"></textarea> 

回答

0

如果我正確理解你的腳本和所有的用戶名/主題/ ORGID的/ etc是$ psScriptPath腳本屬性然後你可以嘗試這樣運行:

$query = shell_exec("powershell -command &quot;& {$psScriptPath -username '$user' -subject '$changesubject' -orgid '$customerid' -textarea '$changedetails' -edt '$endtime' -sdt '$starttime'}&quot;"); 

至少這對我很好用:

測試腳本:

PS C:\> gc c:\1.ps1 
param($App) 
& $App 

從CMD外殼調用:

C:\powershell -noprofile -command "& {c:\1.ps1 -App 'Notepad.exe'}" 
+0

當改變我行你上面的格式,我收到以下錯誤PHP: – user137045

+0

PHP解析錯誤:語法錯誤,意外「{」在C:\的index.php上線73 – user137045

+0

是的,我想我們'重新丟失了一些雙引號 - 「」「 –

0

好像Windows外殼不喜歡回車/換行,所以我base64編碼字符串,並然後在進入PowerShell後對其進行解碼。現在一切OK。

$changedetails = $_POST['details']; 
$changedetailsencoded = base64_encode($changedetails);