Running Powershell completely headless - php
i want to send a "Ballon Notification" to a remote computer in my network. Every machine in the network runs on windows 10.
The event needs to be triggered from a website which is build with php.
I've accomplished this by using the following code:
php
$powershell_path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
$script_path = "[..path to the ps1 script..]\\test.ps1";
$exec = $powershell_path." -executionPolicy Unrestricted ".$script_path." -computer_name ".$args['remote_computer'];
shell_exec($exec." 2>&1");
test.ps1
param(
[parameter(mandatory=$true)][string]$computer_name
)
function Send-Balloon {
Param(
[parameter(mandatory=$true)][string]$To,
[parameter(mandatory=$true)][string]$balloon_title,
[parameter(mandatory=$true)][string]$balloon_text,
[parameter(mandatory=$false)][int]$show_time,
[parameter(mandatory=$false)][string]$scripts_path,
[parameter(mandatory=$false)][string]$script_name
)
if(!$show_time) { $show_time = 15000 }
if(!$scripts_path) { $scripts_path = "C:\temp\" }
if(!$script_name) { $script_name = "task.ps1" }
$remote_computer = $To
if(!(Test-Connection -ComputerName $remote_computer -Count 1 -ErrorAction SilentlyContinue)) {
Write-Warning "$remote_computer could not be reached"
break
}
$str = #"
Add-Type -AssemblyName System.Windows.Forms
`$balloon = [System.Windows.Forms.NotifyIcon]::new()
`$path = (Get-Process -id `$pid).Path
`$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon(`$path)
`$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
`$balloon.BalloonTipTitle = '$balloon_title'
`$balloon.BalloonTipText = '$balloon_text'
`$balloon.Visible = `$true
`$balloon.ShowBalloonTip(100000)
"#
$script = [scriptblock]::Create($str)
$script | Out-File $scripts_path\$script_name
$remote_path = "\\$remote_computer\c$\temp"
if(!(Test-Path $remote_path)) {
New-Item -ItemType Directory -Path $remote_path
}
Copy-Item -Path $scripts_path\$script_name -Destination $remote_path
$task_scriptblock = {
$schedule_script = 'C:\temp\task.ps1'
$seconds = 2
$a = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle hidden -Command $schedule_script"
$tr = New-ScheduledTaskTrigger -Once -At ((Get-Date) + (New-TimeSpan -Seconds $seconds))
$p = New-ScheduledTaskPrincipal -UserId (Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -expand UserName)
$tn = "!random_task_name_" + (-join ((65..90) + (97..122) | Get-Random -Count 10 | % {[char]$_}))
$t = Register-ScheduledTask -TaskName $tn -Trigger $tr -Action $a -Principal $p
Start-Sleep ($seconds + 1)
Get-ScheduledTask -TaskName $tn | Unregister-ScheduledTask -Confirm:$false
Remove-Item -Path $schedule_script
}
Invoke-Command -ComputerName $remote_computer -ScriptBlock $task_scriptblock
}
Send-Balloon -To $computer_name -balloon_title "test" -balloon_text "just a test"
This code copies the script from $str into the tempfolder of a given remote computer. Afterwards it tolds the remote computer to execute the created file. After executing it deletes the ps1 file.
This works like intendet and (mostly) flawless. My problem now is, that powershell with '-WindowStyle hidden' is not really hidden. For a split second you can see the powershell console popping up and instantly vanishing.
I've read that this is the 'normal' behaviour and can be counteracted by calling the ps1-file from another context like a vbs script.
vbs
command = "powershell.exe -nologo -command [path to the ps1-file]"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
The thing is, that this seems to be overcomplication everything as i do have a php script to run a ps1 script to copy a ps1 and a vbs file. Then call the copied vbs script, which calls the copied ps1 to show a simple notification.
Is there a simple method to call the ps1 script via php on a remote computer without seeing the powershell console? Or even better: is there is simpler way to trigger 'balloon notifications' on a remote computer which i haven't thought of yet?
Related
Php exec Powershell script Azure AZ commands do not run, all the other functions do
My PHP script exec('powershell.exe -ExecutionPolicy Unrestricted -NoProfile -InputFormat none -file "..\..\scripts\addcustomer.ps1"', $output); AZ function in the script: $file = “$PSScriptRoot\Azure\pass.txt” $azureuser = “user#contoso.com” $Password = Get-Content $file | ConvertTo-SecureString $credential = New-Object System.Management.Automation.PsCredential($azureuser, $Password) Login-AzAccount -Credential $credential New-AzDnsRecordSet -Name "$name" -RecordType A -ZoneName "contoso.co" -ResourceGroupName "RG" -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "$ipaddress") The $output does not display any output of this function. I confirm that if I run the script manually, everything works. Many thanks.
Use exec($your_command, $output, $error_code) and see what $error_code contains. It may just be because powershell.exe isn't in the PATH env variable in PHP. Try to put the full path to your PowerShell executable, typically something like this: <?php // A default powershell path in case "where powershell" doesn't work. define('POWERSHELL_EXE', 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'); // Find the path to powershell with the "where" command. $powershell_exe = exec('where powershell'); if ($powershell_exe === false) { $powershell_exe = POWERSHELL_EXE; } // Also check that the path to your script can be found. // If it doesn't then you can use realpath() to get the // full path to your script. $cmd = $powershell_exe . ' -ExecutionPolicy Unrestricted -NoProfile -InputFormat none' . ' -file "..\..\scripts\addcustomer.ps1"'; $last_line = exec($cmd, $full_output, $error_code); // Then check if $last_line !== false and check $error_code to see // what happened. var_export([ '$cmd' => $cmd, '$last_line' => $last_line, '$full_output' => $full_output, '$error_code' => $error_code, ]); Another important point: Has the user running your PHP code enought rights to do what you are doing in your PS1 script? You may need to run your PowerShell script with elevation privilege or another user. I never did that but perhaps this topic could help: PHP command to execute powershell script as admin
Powershell output to PHP variable using shell_exec
I have a powershell script which outputs a video file duration. Running this script gives me the expected result. $Folder = 'C:\my\path\to\folder' $File = 'sample1_1280_720.mp4' $LengthColumn = 27 $objShell = New-Object -ComObject Shell.Application $objFolder = $objShell.Namespace($Folder) $objFile = $objFolder.ParseName($File) $Length = $objFolder.GetDetailsOf($objFile, $LengthColumn) Write-Output $Length In a php file, I'm trying to save this output to a variable. <?php $var = shell_exec("powershell -File C:\my\path\to\psFile.ps1 2>&1"); echo "<pre>$var</pre>"; ?> The string output I get from shell_exec is the text you see when you start powershell from cmd. Windows PowerShell Copyright (C) 2016 Microsoft Corporation. All rights reserved. Any suggestions on how to extract the video duration?
Using your PS code $Folder = 'C:\my\path\to\folder' $File = 'sample1_1280_720.mp4' $LengthColumn = 27 $objShell = New-Object -ComObject Shell.Application $objFolder = $objShell.Namespace($Folder) $objFile = $objFolder.ParseName($File) $Length = $objFolder.GetDetailsOf($objFile, $LengthColumn) $Length I'm able to get the file length using PS -File and -Command. I added a few other flags you may want or need. You shouldn't need to use redirection 2>&1 to get your variable from PS to PHP. It is most likely the reason you are getting the logo. function PowerShellCommand($Command) { $unsanitized = sprintf('powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "%s"', $Command); return shell_exec($unsanitized); } function PowerShellFile($File) { $unsanitized = sprintf('powershell.exe -NonInteractive -NoProfile -ExecutionPolicy Bypass -File "%s"', $File); return shell_exec($unsanitized); } // Can use relative paths echo PowerShellCommand("./psFile.ps1"); // Be sure to escape Windows paths if needed echo PowerShellFile("C:\\my\\path\\to\\folder\\psFile.ps1"); Returning $Length in all three ways work for me $Length return $Length Write-Output $length
running matlab script from php
I'm working on a project which needs to run matlab script through php.. I have searched for the solution a lot but didnt get any solution..here is code sample..its working fine on command line but not through webbrowser php code <?php if(isset($_POST['filepath'])) { $filename = "test1.txt"; $inputDir = "G:\soft"; $outputDir1 = "C:\output".$filename; $command = "matlab -sd ".$inputDir." -r\ phpcreatefile('".$outputDir1."\\".$filename."')"; exec($command); echo "The following command was run: ".$command."<br/>"; echo $filename." was created in ".$outputDir1."<br/>"; } ?> function phpcreatefile(filepath) % Open the file %filepath='c:\text1.txt'; fid = fopen(filepath, 'wt'); for i = 1:100 % Create random number randNumber = [num2str(rand(1)) '\n']; % Write number to file fprintf(fid, randNumber); end % Close file fclose(fid); % Quit MATLAB quit force
How can I use exec with cmd in windows? [duplicate]
This question already has answers here: PHP exec to launch a cmd window with parameters (1 answer) How to Gammu sendsms php shell_exec windows 7 and xampp? (1 answer) Closed 8 years ago. How can I run this: shell_exec('"C:\Program Files\gammu\bin\gammu.exe" --sendsms TEXT 06706177529 -text "halooo"'); With cmd? If i run this in cmd it works very well. So i would like to run it with exec and cmd. I found something like cmd c/ , but how it works?
You will need to use COM. $shell = new COM("WScript.Shell"); $exec = $shell->Run($cmd, 0, false); That's a simple example, if you head over to http://php.net/function.exec and take a look at the comments a few people have posted a solution. Take a look at this comment http://www.php.net/manual/fr/function.exec.php#57538, a nice function is provided to wrap around the above code. I'll paste it here just in case. <?php define ('EXEC_TMP_DIR', 'C:\tmp'); function windExec($cmd,$mode=''){ // runs a command line and returns // the output even for Wind XP SP2 // example: $cmd = "fullpath.exe -arg1 -arg2" // $outputString = windExec($cmd, "FG"); // OR windExec($cmd); // (no output since it runs in BG by default) // for output requires that EXEC_TMP_DIR be defined // Setup the command to run from "run" $cmdline = "cmd /C $cmd"; // set-up the output and mode if ($mode=='FG'){ $outputfile = EXEC_TMP_DIR . "\\" . time() . ".txt"; $cmdline .= " > $outputfile"; $m = true; } else $m = false; // Make a new instance of the COM object $WshShell = new COM("WScript.Shell"); // Make the command window but dont show it. $oExec = $WshShell->Run($cmdline, 0, $m); if ($outputfile){ // Read the tmp file. $retStr = file_get_contents($outputfile); // Delete the temp_file. unlink($outputfile); } else $retStr = ""; return $retStr; }
php execute a background process
I need to execute a directory copy upon a user action, but the directories are quite large, so I would like to be able to perform such an action without the user being aware of the time it takes for the copy to complete. Any suggestions would be much appreciated.
Assuming this is running on a Linux machine, I've always handled it like this: exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile)); This launches the command $cmd, redirects the command output to $outputfile, and writes the process id to $pidfile. That lets you easily monitor what the process is doing and if it's still running. function isRunning($pid){ try{ $result = shell_exec(sprintf("ps %d", $pid)); if( count(preg_split("/\n/", $result)) > 2){ return true; } }catch(Exception $e){} return false; }
Write the process as a server-side script in whatever language (php/bash/perl/etc) is handy and then call it from the process control functions in your php script. The function probably detects if standard io is used as the output stream and if it is then that will set the return value..if not then it ends proc_close( proc_open( "./command --foo=1 &", array(), $foo ) ); I tested this quickly from the command line using "sleep 25s" as the command and it worked like a charm. (Answer found here)
You might want to try to append this to your command >/dev/null 2>/dev/null & eg. shell_exec('service named reload >/dev/null 2>/dev/null &');
I'd just like to add a very simple example for testing this functionality on Windows: Create the following two files and save them to a web directory: foreground.php: <?php ini_set("display_errors",1); error_reporting(E_ALL); echo "<pre>loading page</pre>"; function run_background_process() { file_put_contents("testprocesses.php","foreground start time = " . time() . "\n"); echo "<pre> foreground start time = " . time() . "</pre>"; // output from the command must be redirected to a file or another output stream // http://ca.php.net/manual/en/function.exec.php exec("php background.php > testoutput.php 2>&1 & echo $!", $output); echo "<pre> foreground end time = " . time() . "</pre>"; file_put_contents("testprocesses.php","foreground end time = " . time() . "\n", FILE_APPEND); return $output; } echo "<pre>calling run_background_process</pre>"; $output = run_background_process(); echo "<pre>output = "; print_r($output); echo "</pre>"; echo "<pre>end of page</pre>"; ?> background.php: <? file_put_contents("testprocesses.php","background start time = " . time() . "\n", FILE_APPEND); sleep(10); file_put_contents("testprocesses.php","background end time = " . time() . "\n", FILE_APPEND); ?> Give IUSR permission to write to the directory in which you created the above files Give IUSR permission to READ and EXECUTE C:\Windows\System32\cmd.exe Hit foreground.php from a web browser The following should be rendered to the browser w/the current timestamps and local resource # in the output array: loading page calling run_background_process foreground start time = 1266003600 foreground end time = 1266003600 output = Array ( [0] => 15010 ) end of page You should see testoutput.php in the same directory as the above files were saved, and it should be empty You should see testprocesses.php in the same directory as the above files were saved, and it should contain the following text w/the current timestamps: foreground start time = 1266003600 foreground end time = 1266003600 background start time = 1266003600 background end time = 1266003610
If you need to just do something in background without the PHP page waiting for it to complete, you could use another (background) PHP script that is "invoked" with wget command. This background PHP script will be executed with privileges, of course, as any other PHP script on your system. Here is an example on Windows using wget from gnuwin32 packages. The background code (file test-proc-bg.php) as an exmple ... sleep(5); // some delay file_put_contents('test.txt', date('Y-m-d/H:i:s.u')); // writes time in a file The foreground script, the one invoking ... $proc_command = "wget.exe http://localhost/test-proc-bg.php -q -O - -b"; $proc = popen($proc_command, "r"); pclose($proc); You must use the popen/pclose for this to work properly. The wget options: -q keeps wget quiet. -O - outputs to stdout. -b works on background
Well i found a bit faster and easier version to use shell_exec('screen -dmS $name_of_screen $command'); and it works.
Here is a function to launch a background process in PHP. Finally created one that actually works on Windows too, after a lot of reading and testing different approaches and parameters. function LaunchBackgroundProcess($command){ // Run command Asynchroniously (in a separate thread) if(PHP_OS=='WINNT' || PHP_OS=='WIN32' || PHP_OS=='Windows'){ // Windows $command = 'start "" '. $command; } else { // Linux/UNIX $command = $command .' /dev/null &'; } $handle = popen($command, 'r'); if($handle!==false){ pclose($handle); return true; } else { return false; } } Note 1: On windows, do not use /B parameter as suggested elsewhere. It forces process to run the same console window as start command itself, resulting in the process being processed synchronously. To run the process in a separate thread (asynchronously), do not use /B. Note 2: The empty double quotes after start "" are required if the command is a quoted path. start command interprets the first quoted parameter as window title.
Can you arrange to fork off a separate process, and then run your copy in the background? It's been a while since I did any PHP, but the function pcntl-fork looks promising.
Use this function to run your program in background. It cross-platform and fully customizable. <?php function startBackgroundProcess( $command, $stdin = null, $redirectStdout = null, $redirectStderr = null, $cwd = null, $env = null, $other_options = null ) { $descriptorspec = array( 1 => is_string($redirectStdout) ? array('file', $redirectStdout, 'w') : array('pipe', 'w'), 2 => is_string($redirectStderr) ? array('file', $redirectStderr, 'w') : array('pipe', 'w'), ); if (is_string($stdin)) { $descriptorspec[0] = array('pipe', 'r'); } $proc = proc_open($command, $descriptorspec, $pipes, $cwd, $env, $other_options); if (!is_resource($proc)) { throw new \Exception("Failed to start background process by command: $command"); } if (is_string($stdin)) { fwrite($pipes[0], $stdin); fclose($pipes[0]); } if (!is_string($redirectStdout)) { fclose($pipes[1]); } if (!is_string($redirectStderr)) { fclose($pipes[2]); } return $proc; } Note that after command started, by default this function closes the stdin and stdout of running process. You can redirect process output into some file via $redirectStdout and $redirectStderr arguments. Note for windows users: You cannot redirect stdout/stderr to nul in the following manner: startBackgroundProcess('ping yandex.com', null, 'nul', 'nul'); However, you can do this: startBackgroundProcess('ping yandex.com >nul 2>&1'); Notes for *nix users: 1) Use exec shell command if you want get actual PID: $proc = startBackgroundProcess('exec ping yandex.com -c 15', null, '/dev/null', '/dev/null'); print_r(proc_get_status($proc)); 2) Use $stdin argument if you want to pass some data to the input of your program: startBackgroundProcess('cat > input.txt', "Hello world!\n");
You might try a queuing system like Resque. You then can generate a job, that processes the information and quite fast return with the "processing" image. With this approach you won't know when it is finished though. This solution is intended for larger scale applications, where you don't want your front machines to do the heavy lifting, so they can process user requests. Therefore it might or might not work with physical data like files and folders, but for processing more complicated logic or other asynchronous tasks (ie new registrations mails) it is nice to have and very scalable.
A working solution for both Windows and Linux. Find more on My github page. function run_process($cmd,$outputFile = '/dev/null', $append = false){ $pid=0; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!'; $cmd = 'wmic process call create "'.$cmd.'" | find "ProcessId"'; $handle = popen("start /B ". $cmd, "r"); $read = fread($handle, 200); //Read the output $pid=substr($read,strpos($read,'=')+1); $pid=substr($pid,0,strpos($pid,';') ); $pid = (int)$pid; pclose($handle); //Close }else{ $pid = (int)shell_exec(sprintf('%s %s %s 2>&1 & echo $!', $cmd, ($append) ? '>>' : '>', $outputFile)); } return $pid; } function is_process_running($pid){ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!'; //tasklist /FI "PID eq 6480" $result = shell_exec('tasklist /FI "PID eq '.$pid.'"' ); if (count(preg_split("/\n/", $result)) > 0 && !preg_match('/No tasks/', $result)) { return true; } }else{ $result = shell_exec(sprintf('ps %d 2>&1', $pid)); if (count(preg_split("/\n/", $result)) > 2 && !preg_match('/ERROR: Process ID out of range/', $result)) { return true; } } return false; } function stop_process($pid){ if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {//'This is a server using Windows!'; $result = shell_exec('taskkill /PID '.$pid ); if (count(preg_split("/\n/", $result)) > 0 && !preg_match('/No tasks/', $result)) { return true; } }else{ $result = shell_exec(sprintf('kill %d 2>&1', $pid)); if (!preg_match('/No such process/', $result)) { return true; } } }
Thanks to this answer: A perfect tool to run a background process would be Symfony Process Component, which is based on proc_* functions, but it's much easier to use. See its documentation for more information.
Instead of initiating a background process, what about creating a trigger file and having a scheduler like cron or autosys periodically execute a script that looks for and acts on the trigger files? The triggers could contain instructions or even raw commands (better yet, just make it a shell script).
If using PHP there is a much easier way to do this using pcntl_fork: http://www.php.net/manual/en/function.pcntl-fork.php
I am heavily using fast_cgi_finish_request() In combination with a closure and register_shutdown_function() $message ='job executed'; $backgroundJob = function() use ($message) { //do some work here echo $message; } Then register this closure to be executed before shutdown. register_shutdown_function($backgroundJob); Finally when the response was sent to the client you can close the connection to the client and continue working with the PHP process: fast_cgi_finish_request(); The closure will be executed after fast_cgi_finish_request. The $message will not be visible at any time. And you can register as much closures as you want, but take care about script execution time. This will only work if PHP is running as a Fast CGI module (was that right?!)
If you are looking to execute a background process via PHP, pipe the command's output to /dev/null and add & to the end of the command. exec("bg_process > /dev/null &"); Note that you can not utilize the $output parameter of exec() or else PHP will hang (probably until the process completes).
PHP scripting is not like other desktop application developing language. In desktop application languages we can set daemon threads to run a background process but in PHP a process is occuring when user request for a page. However It is possible to set a background job using server's cron job functionality which php script runs.
For those of us using Windows, look at this: Reference: http://php.net/manual/en/function.exec.php#43917 I too wrestled with getting a program to run in the background in Windows while the script continues to execute. This method unlike the other solutions allows you to start any program minimized, maximized, or with no window at all. llbra#phpbrasil's solution does work but it sometimes produces an unwanted window on the desktop when you really want the task to run hidden. start Notepad.exe minimized in the background: <?php $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run("notepad.exe", 7, false); ?> start a shell command invisible in the background: <?php $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run("cmd /C dir /S %windir%", 0, false); ?> start MSPaint maximized and wait for you to close it before continuing the script: <?php $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run("mspaint.exe", 3, true); ?> For more info on the Run() method go to: http://msdn.microsoft.com/library/en-us/script56/html/wsMthRun.asp Edited URL: Go to https://technet.microsoft.com/en-us/library/ee156605.aspx instead as the link above no longer exists.
New answer to an old question. Using this library, the following code would spawn an asynchronous/parallel PHPThread to do background work. Must have pcntl, posix, and socket extensions Designed for/tested in CLI mode. EZ code sample: function threadproc($thread, $param) { echo "\tI'm a PHPThread. In this example, I was given only one parameter: \"". print_r($param, true) ."\" to work with, but I can accept as many as you'd like!\n"; for ($i = 0; $i < 10; $i++) { usleep(1000000); echo "\tPHPThread working, very busy...\n"; } return "I'm a return value!"; } $thread_id = phpthread_create($thread, array(), "threadproc", null, array("123456")); echo "I'm the main thread doing very important work!\n"; for ($n = 0; $n < 5; $n++) { usleep(1000000); echo "Main thread...working!\n"; } echo "\nMain thread done working. Waiting on our PHPThread...\n"; phpthread_join($thread_id, $retval); echo "\n\nOur PHPThread returned: " . print_r($retval, true) . "!\n";
From PHP official documentation(php.net) <?php function execInBackground($cmd) { if (substr(php_uname(), 0, 7) == "Windows"){ pclose(popen("start /B ". $cmd, "r")); } else { exec($cmd . " > /dev/null &"); } } ?>
I know it is a 100 year old post, but anyway, thought it might be useful to someone. You can put an invisible image somewhere on the page pointing to the url that needs to run in the background, like this: <img src="run-in-background.php" border="0" alt="" width="1" height="1" />