2010-03-02 53 views
2

如果我運行:PHP到PowerShell中使用了shell_exec

$output = shell_exec('powershell "get-service "dhcp""'); 

我得到展示運行DHCP服務的完美輸出,但如果我運行:

$output = shell_exec('powershell "get-user "testing""'); 

我什麼也沒得到。

我沒有看到我在這裏做什麼的任何區別 - 以及爲什麼get-service會工作,但get-user不會。如果我用cmd運行它,它可以很好地工作。有任何想法嗎?

我認爲這些問題可能是Apache正在運行該命令並且沒有權限。情況會是這樣嗎? apache是​​否以不同的用戶身份運行?如果是這樣,它無法訪問。

+0

難道這個問題幾個小時前已經發布了嗎? – Franz 2010-03-02 01:03:36

+0

@Franz - 這是你指的帖子嗎? http://stackoverflow.com/questions/2353551/php-powershell-command – dugas 2010-03-02 01:22:12

+1

Get-User不是內置命令。你能說一點這個cmdlet,比如它來自哪個模塊或snapin嗎?或者它是你寫的命令?順便說一句,我會像cmd.exe那樣運行這個'powershell -command「&{get-user testing}」'。請注意,通常不需要在PowerShell中引用字符串參數。 – 2010-03-02 01:23:46

回答

2

嘗試將錯誤輸出重定向到標準輸出以查看是否可以看到錯誤。

$output = shell_exec('powershell "get-user "testing" 2>&1"'); 

http://www.aboutdebian.com/nettools.txt

//Normally, the shell_exec function does not report STDERR messages. 
//The "2>&1"   option tells the system 
//to pipe STDERR to STDOUT so if there is an error, we can see it. 
$fp = shell_exec("$command 2>&1"); 
0

exec()和了shell_exec(採取這個片段)不是自然非常詳細。 exec()允許你設置第三個變量並獲得執行狀態,但是大多數故障被分配爲「1」,並且如果二進制文件不可執行,則無法知道它是否爲權限錯誤。

輸入一個項目,該項目允許PHP獲取並與真實的PowerShell動態交互。在這裏獲得:https://github.com/merlinthemagic/MTS

下載您只需使用下面的代碼後:

$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell'); 

$strCmd1 = 'get-user "testing"'; 
$return1 = $shellObj->exeCmd($strCmd1); 

返回會給你的PowerShell命令返回或錯誤,就如同您坐在控制檯。此外,您可以針對$ shellObj發出任何您喜歡的命令,該環境在PHP腳本的整個生命週期中都得到維護。因此,不要將腳本文件綁定到腳本文件中,只需使用exeCmd()方法逐個發佈它們,這樣您還可以處理返回和任何異常。