I want to run powershell through PHP program. In powershell I have to run the command "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" to generate hash value. Shyam.jpeg is a file which is located in the directory C:\wamp64\www\Dedup. Here is my code
<?php
$psPath = "C:\\Windows\\SysWOW64\WindowsPowerShell\v1.0\\powershell.exe";
$psDIR = "C:\\wamp64\\www\\Dedup";
$psScript = "SHYAM.JPEG";
$runScript = $psDIR. $psScript;
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
But it is not working. I am struggling to make it work. When I tried "Get-FileHash -Algorithm sha256 .\SHYAM.jpeg" command in powerShell it works fine. Please help me out.
The output is C:\Windows\SysWOW64\WindowsPowerShell
v1.0\powershell.exeGet-FileHash -Algorithm sha256./C:\wamp64\www\DedupSHYAM.JPEG
Missing a backslash escape in the path to PowerShell, no spaces, wrong folder path, it's not going to work.
Try something like:
<?php
$psPath = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe";
$fileDir = "C:\\wamp64\\www\\Dedup";
$fileName = "SHYAM.JPEG";
$runCMD = "$psPath -Command \"Get-FileHash -Algorithm SHA256 -Path '$fileDir\\$fileName' | Select-Object -ExpandProperty Hash\"";
$out= shell_exec($runCMD);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
I haven't tried to exec it, but that at least prints a sensible looking command.
Replace the ./ with a space and add a space before the Get-FileHash.
$runCMD = $psPath.'Get-FileHash -Algorithm sha256./'.$psDIR.$psScript;
To
$runCMD = $psPath.' Get-FileHash -Algorithm sha256 '.$psDIR.$psScript;
Related
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
I have a PowerShell script hosted on a server. I am calling PowerShell script from Php as below:
<?php
header('Content-Type: text/plain');
$csv = file_get_contents('http://domaincom/wp-content/uploads/csv-samples.csv');
echo $csv;
shell_exec('pwsh -File http://domaincom/wp-content/uploads/pscript.ps1');
$psPath = "powershell.exe";
$psDIR = "http://domaincom/wp-content/uploads/";
$psScript = "pscript.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1";
echo "\$psPath $psPath <br>";
echo "\$psDIR $psDIR <br>";
echo "\$psScript $psScript <br>";
echo "\$runScript $runScript <br>";
echo "\$runCMD $runCMD <br>";
exec( $runCMD,$out,$ret);
echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";
?>
Script executes and I can see csv-samples.csv results in browser, but the powershell script doesn't execute. I get below message in browser:
Site,URL,Category
Sitepoint,http://www.sitepoint.com/,Web development
Html.it,http://www.html.it/,Web development
Wamboo,http://www.wamboo.it/,Web development$psPath powershell.exe <br>$psDIR http://domaincom/wp-
content/uploads/ <br>$psScript pscript.ps1 <br>$runScript http://domaincom/wp-
content/uploads/pscript.ps1 <br>$runCMD powershell.exe http://domaincom/wp-
content/uploads/pscript.ps1 2>&1 <br><pre>Array
(
[0] => sh: 1: powershell.exe: not found
)
127</pre>
Your help is very much appreciated.
Thank You.
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
My PHP script (in Linux) is not printing my values on screen, however it prints a Warning message.
e.g.
This is my php code:
$stream = ssh2_exec($connection, 'powershell "c:\remoteScript.ps1" -$NotificationID "5359f76f888ddf35b889dedf"');
$string = stream_get_contents($stream);
echo "Powershell output: ". $string;
This is my powershell code:
Write-Warning "This is a test"
And this is what PHP is writing:
Powershell output: WARNING: You should update your PowerShell to
PowerShell 2.0 version.
QUESTION: How can I catch the the stream returned by Powershell?
SOLUTION: we need to tell ssh2_exec whether to wait/block until the remote process finishes search for "block" on this page to see an example.
$stream = ssh2_exec($connection, 'powershell \'C:\scripts\remoteVeeamClientSummary.ps1\' -Client \''. $Client. '\' -NotificationID \''. $NotificationID. '\'');
stream_set_blocking($stream, true);
return stream_get_contents($stream);
and powershell:
Write-Output $result
I was trying to use wscript.shell through COM objects with php to pass some cmd commands to cURL library (the DOS version). here is what I use to perform this task:
function windExec($cmd,$mode=''){
// Setup the command to run from "run"
$cmdline = "cmd /C $cmd";
// set-up the output and mode
if ($mode=='FG'){
$outputfile = uniqid(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;
}
now when I run this function like:
windExec("\"C:/Documents and Settings/ermac/Desktop/my project/curl\" http://www.google.com/", 'FG');
curl doesn't run because there is a problem with the path. but when I remove the spaces from the path it works great.
windExec("\"C:/curl\" http://www.google.com/", 'FG');
so my question is how can I escape these spaces in wscript.shell commands?
is there anyway I can fix this?
thanks in advance :)
nvm I found a solution:
there:
windExec("cd C:/Documents and Settings/ermac/Desktop/my project/libs & curl.exe -L http://www.google.com/", 'FG');