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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。