dtmm0148603 2018-07-16 15:43
浏览 119

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

Firstly I apologise but I am pretty new to PHP and PowerShell, we all have to start somewhere! I am creating a utility where everyday IT tasks can be performed from a central web based console. I have managed to query and report on things like password expiry by executing PowerShell scripts but have got stuck on unlocking accounts. I query AD and return a list of locked users with a button next to each user to unlock them. This button posts to a php page which runs another powershell script to unlock the user. php page is:

    <?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>";
?>

As you can see I'm trying to capture any output but at the moment the page is just hanging.

PowerShell script is:

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

If I echo the command before passing it to PS it looks fine and can be executed directly from PS.

Edit: This is something to do with exec (or shell_exec) causing an issue when the PS script is setting credentials. If I remove that part of the script i.e.

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

it runs and returns that the script failed due to

Insufficient access rights to perform the operation

Has anyone come across this before, I have searched for anything on this to no avail. Thanks!

Further edit After a bit more testing it is this PS code that doesn't work

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

If I change the method to

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

it works with no problems. Plain text passwords in files are obviously not something I want to use. Can someone test and see if they get the same result?

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-07-18 09:18
    关注

    So it turns out that it was all just me being a newb. Executing the script via the php page must not be running it as my account. My account was the one that set the credentials and stored it in the file so is the only one that can decrypt it. I have changed the convertto-securestring method to use keys instead of default and it now works.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集