Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Powershell script as a particular user in PHP

Tags:

php

powershell

I am creating a VBS in php that is being saved to a remote server on the network, I am then creating a powershell script to run it and executing from 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);

the VBS has to be run on that remote server which is why I'm doing it this way, and it has to be run by a specific user and that's what i'm having trouble with. I currently use this:

 $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);

Can anyone advise/help with how I can run it with a different user (DOMAIN\user.name, P4$$word)

like image 605
Maff Avatar asked May 27 '26 18:05

Maff


1 Answers

You can use Invoke-command or Enter-PSSession. Here is an example with Invoke-command

$computername = "remote computer"
$cred = Get-Credential
Invoke-Command -ComputerName $computername -Credential $cred -ScriptBlock {

    #command that you will run on remote system
}

Note that in order to use these cmdlet WinRM must be configured. Fore more details see here - https://technet.microsoft.com/en-us/library/hh849694.aspx

like image 52
kekimian Avatar answered May 30 '26 07:05

kekimian