簡體   English   中英

使用shell_exec傳遞管理員用戶名和密碼

[英]Passing Admin username & password using shell_exec

我有一個Powershell腳本,當我直接在Web服務器(Windows 2008R2,IIS)上運行時,該腳本成功完成。 我現在正嘗試從php頁面啟動腳本。 腳本啟動,我可以在任務管理器中看到該過程。

當我從PHP頁面使用shell_exec調用腳本時,它失敗並顯示錯誤

"Exception calling "FindOne" with "0" argument(s): "An operations error occurred."

從任務管理器中可以看到該腳本以用戶IUSR的身份運行。 我很確定這是腳本失敗的原因,要成功完成腳本,需要以域管理員身份運行。

我正在使用以下命令調用腳本

$ username =頁面上當前登錄的用戶(已通過AD驗證)
$ newPword1 =新用戶密碼

$query = shell_exec("powershell -command $psScriptPath '$username' '$newPword1' < NUL");

Powershell腳本:

#*=============================================================================
#* PARAMETER DECLARATION
#*=============================================================================

param([string]$username,[string]$pword)

#*=============================================================================
#* INITIALISE VARIABLES
#*=============================================================================
# Increase buffer width/height to avoid Powershell from wrapping the text before
# sending it back to PHP (this results in weird spaces).
$pshost = Get-Host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
$newsize.height = 3000
$newsize.width = 400
$pswindow.buffersize = $newsize


#*=============================================================================
#* SCRIPT BODY
#*=============================================================================

$root = [ADSI]'LDAP://<server>/<DNtoOU>'
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectCategory=person)(sAMAccountName=$username))"
$user = $searcher.findone()

$userDN = $user.path

$user=[ADSI]$($userDN)
$user.SetPassword($pword)
$user.setinfo()
write-host "success"

是否可以使用上述命令傳遞域管理員用戶名/密碼來運行powershell,從而使腳本成功運行?

幾種方法之一是使用PowerShellRunAs

編輯:有文檔記錄,您可以按原樣使用powershell腳本,也可以對其進行修改以適合您的需要(例如, 使用密碼文件)。

更詳細地,代替這個:
shell_exec("powershell -command $psScriptPath …")
您可以使用此:
shell_exec("powershell -command "Start-Process powershell.exe -argumentlist '-command $psScriptPath …' -Credential 'TheDomain\\TheUser'")
但是要避免出現密碼提示,請按照文檔使用PowerShellRunAs:
shell_exec("powershell -command "Start-Process powershell.exe -argumentlist '-command $psScriptPath …' -Credential (.\\PowerShellRunAs.ps1 -get contoso\\svc_remoterestart \\\\fileserver\\share\\file.pwd)")

另外,您可以將start-process … -credential …合並到您自己的腳本中。 這樣,您可以像以前一樣使shell_exec調用保持簡單,並且只有一部分腳本將在具有不同(較高)特權的用戶下運行。

如果您按原樣使用腳本,請記住首先使用-set而不是-get來運行它,以便設置密碼文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM