1

Web サーバー (Windows 2008R2、IIS) で直接実行すると正常に完了する Powershell スクリプトがあります。現在、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 を実行するためにドメイン管理者のユーザー名/パスワードを渡して、スクリプトを正常に実行できるようにすることはできますか?

4

1 に答える 1

1
于 2012-12-30T21:44:52.383 に答える