使用 shell_exec 将 Powershell 输出到 PHP 变量
Powershell output to PHP variable using shell_exec
我有一个输出视频文件时长的 powershell 脚本。 运行 这个脚本给了我预期的结果。
$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
在 php 文件中,我试图将此输出保存到一个变量中。
<?php
$var = shell_exec("powershell -File C:\my\path\to\psFile.ps1 2>&1");
echo "<pre>$var</pre>";
?>
我从 shell_exec 获得的字符串输出是您从 cmd 启动 powershell 时看到的文本。 Windows PowerShell
版权所有 (C) 2016 Microsoft Corporation。保留所有权利。对如何提取视频时长有什么建议吗?
使用您的 PS 代码
$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
我可以使用 PS -File
和 -Command
获取文件长度。我添加了一些您可能想要或需要的其他标志。您不需要使用重定向 2>&1
将变量从 PS 获取到 PHP。这很可能是您获得徽标的原因。
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");
以所有三种方式返回 $Length
对我都有效
$Length
return $Length
Write-Output $length
我有一个输出视频文件时长的 powershell 脚本。 运行 这个脚本给了我预期的结果。
$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
在 php 文件中,我试图将此输出保存到一个变量中。
<?php
$var = shell_exec("powershell -File C:\my\path\to\psFile.ps1 2>&1");
echo "<pre>$var</pre>";
?>
我从 shell_exec 获得的字符串输出是您从 cmd 启动 powershell 时看到的文本。 Windows PowerShell 版权所有 (C) 2016 Microsoft Corporation。保留所有权利。对如何提取视频时长有什么建议吗?
使用您的 PS 代码
$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
我可以使用 PS -File
和 -Command
获取文件长度。我添加了一些您可能想要或需要的其他标志。您不需要使用重定向 2>&1
将变量从 PS 获取到 PHP。这很可能是您获得徽标的原因。
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");
以所有三种方式返回 $Length
对我都有效
$Length
return $Length
Write-Output $length