首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用shell_exec传递管理员用户名和密码

使用shell_exec传递管理员用户名和密码
EN

Stack Overflow用户
提问于 2012-12-30 21:24:35
回答 1查看 3.5K关注 0票数 1

我有一个Powershell脚本,当我直接在Web服务器(Windows2008R2,IIS)上运行时,它成功地完成了。我现在正在尝试从php页面启动脚本。脚本启动后,我可以在任务管理器中看到这个过程。

当我从php页面使用shell_exec调用脚本时,它会失败,从而导致错误。

代码语言:javascript
复制
"Exception calling "FindOne" with "0" argument(s): "An operations error occurred."

我可以从任务管理器中看到脚本正在以用户IUSR的身份运行。我很肯定这就是脚本失败的原因,因为要成功完成它需要作为域管理运行。

我使用以下命令调用脚本

$username =页面上当前登录的用户(针对AD进行身份验证)

$newPword1 =新用户密码

代码语言:javascript
复制
$query = shell_exec("powershell -command $psScriptPath '$username' '$newPword1' < NUL");

Powershell脚本:

代码语言:javascript
复制
#*=============================================================================
#* 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,从而允许脚本成功运行?

EN

回答 1

Stack Overflow用户

发布于 2012-12-30 21:44:52

几种方法之一是使用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运行它,以便设置密码文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14094806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档