Powershell output to PHP variable using shell_exec

烂漫一生 提交于 2019-12-10 23:03:05

问题


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?


回答1:


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 


来源:https://stackoverflow.com/questions/42298608/powershell-output-to-php-variable-using-shell-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!