以PHP中的特定用户身份运行Powershell脚本


Run Powershell script as a particular user in PHP

我正在php中创建一个VBS,该VBS将保存到网络上的远程服务器,然后我创建一个powershell脚本来运行它并从php执行。

$ITIMHOS = "ABD-DEF-123";    
//create the ps1
    $fs = fopen("''''spc-nsa-001''nscap''RunVBS.ps1","a"); //create batch file
    fwrite($fs,"Start-Process cmd.exe /c'''''spc-nsa-001''nscap''JP-Computer-import-".$ITIMHOS.".vbs'"); //write batch file contents
    fclose($fs);

VBS必须在远程服务器上运行,这就是我这样做的原因,它必须由特定用户运行,这也是我遇到的问题。我目前使用的是:

 $psRun = '''''spc-nsa-001''c$''windows''syswow64''WindowsPowerShell''v1.0''powershell.exe'; //64bit server, 32bit web server
 $ps1File = '''''spc-nsa-001''nscap''runVBS.ps1';
exec($psRun.' -command '.$ps1File);

有人能建议/帮助我如何与其他用户(DOMAIN''user.name,P4$$word)一起运行

您可以使用Invoke命令或Enter PSSession。以下是Invoke命令的示例

$computername = "remote computer"
$cred = Get-Credential
Invoke-Command -ComputerName $computername -Credential $cred -ScriptBlock {
    #command that you will run on remote system
}

请注意,为了使用这些cmdlet,必须配置WinRM。欲了解更多详情,请点击此处-https://technet.microsoft.com/en-us/library/hh849694.aspx