繁体   English   中英

如果尝试设置凭据,则使用exec运行PowerShell脚本会挂起

[英]Using exec to run PowerShell script hangs if I try to set credentials

首先,我很抱歉,但是我对PHP和PowerShell还是很陌生,我们都必须从某个地方开始! 我正在创建一个实用程序,可以从中央基于Web的控制台执行日常IT任务。 我已经通过执行PowerShell脚本来查询和报告诸如密码到期之类的事情,但是在解锁帐户时遇到了麻烦。 我查询AD,并返回锁定用户的列表,每个用户旁边都有一个按钮来解锁他们。 此按钮发布到php页面,该页面运行另一个powershell脚本来解锁用户。 php页面是:

    <?php    

    // Get the variables submitted by POST in order to pass them to the PowerShell script:
    $lockeduser = $_POST["unlock"];

    // Path to the PowerShell script.
    $psScriptPath = "C:\\code\\psphp\\ps\\unlock.ps1 $lockeduser 2>&1";

    // Execute the PowerShell script:
    exec("powershell -command $psScriptPath",$out,$ret);

    echo "<pre>";
    print_r ($out);
    print_r ($ret);
    echo "</pre>";
?>

如您所见,我正在尝试捕获任何输出,但此刻页面刚刚挂起。

PowerShell脚本为:

param([string]$lockeduser)
Import-Module ActiveDirectory
$adminacc = "*myadminaccount*"
$encrypted = Get-Content c:\password1.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($adminacc, $encrypted)
Unlock-ADAccount -Identity $lockeduser -Credential $credential

如果我在将命令传递给PS之前回显该命令,则它看起来不错,可以直接从PS执行。

编辑:这与exec(或shell_exec)有关,当PS脚本设置凭据时会引起问题。 如果我删除脚本的那部分,即

param([string]$lockeduser)
Import-Module ActiveDirectory
Unlock-ADAccount -Identity $lockeduser

它运行并返回脚本由于以下原因而失败

访问权限不足,无法执行操作

有没有人遇到过这个问题,我已经搜索了这个问题,但是没有任何结果。 谢谢!

进一步编辑经过更多测试后,此PS代码不起作用

$encrypted = Get-Content c:\password1.txt | ConvertTo-SecureString

如果我将方法更改为

$password = ConvertTo-SecureString "My Password" -AsPlainText -Force

它没有问题。 文件中的纯文本密码显然不是我想要使用的密码。 有人可以测试一下,看看是否得到相同的结果吗?

事实证明,这仅仅是我成为新手。 通过php页面执行脚本不得将其作为我的帐户运行。 我的帐户是用于设置凭据并将其存储在文件中的帐户,因此是唯一可以对其进行解密的帐户。 我已将convertto-securestring方法更改为使用键而不是默认值,并且现在可以使用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM