php - 下拉菜单中单独的 Powershell 输出

标签 php powershell

我通过以下 Powershell 命令获取打印机信息:

<?php
$Comment = Shell_Exec ('powershell.exe -Command "Get-Printer -ComputerName servername | select Comment | ConvertTo-Html | Format-Table"');          
?>

并尝试将它们放入下拉菜单中:

<select id="comment">
  <option value="comment"><?php echo $Comment; ?></option>
</select>`

但我找不到一种方法将输出与每个框中的一个值分开。

shell 调用的输出/数据如下所示:

Comment                                                         
-------                                                         
2483                                                                                                                 
2066                                                                                                                                               1737

最佳答案

如果你想创建一个选择下拉菜单,你需要创建几个像这样的条目:

<label for="printer">Choose a printer:</label>

<select id="printer" name="printer">
  <option value="printer-1">Printer 1</option>
  <option value="printer-2">Printer 2</option>
  <option value="printer-x">The last printer</option>
</select>

但无论如何,我会避免 PHP 在每次页面加载时调用一些 Powershell 命令。这非常非常慢,并且您的页面根本不会使用react。

如果您需要更新此列表,请考虑使用不时使用 cron 任务(计划任务)编写的配置文件。然后页面中的 PHP 代码应该通过需要此配置文件来加载打印机数组。

例如,您可以通过将打印机列表保存为 PHP 可以轻松读取的 JSON 格式来生成打印机列表。

命令将变为:

powershell.exe -Command "Get-Printer -ComputerName yourprintserver | select Name,DriverName,PortName,Comment | ConvertTo-Json"

输出示例:

[
    {
        "Name":  "PDF-cadwork",
        "DriverName":  "PDF-XChange 4.0 Lite",
        "PortName":  "PDF-XChange4",
        "Comment":  ""
    },
    {
        "Name":  "OKI C9800(PS)",
        "DriverName":  "OKI C9600(PS)",
        "PortName":  "IP_192.168.11.99",
        "Comment":  ""
    },
    {
        "Name":  "OKI C9800(PCL)",
        "DriverName":  "OKI C9800(PCL)",
        "PortName":  "IP_192.168.11.99",
        "Comment":  ""
    },
    {
        "Name":  "Microsoft XPS Document Writer",
        "DriverName":  "Microsoft XPS Document Writer v4",
        "PortName":  "PORTPROMPT:",
        "Comment":  ""
    },
    {
        "Name":  "HP LaserJet M570 PS",
        "DriverName":  "HP Universal Printing PS",
        "PortName":  "IP_192.168.11.96",
        "Comment":  ""
    },
    {
        "Name":  "HP Color LaserJet CM6030 MFP PCL6",
        "DriverName":  "HP Color LaserJet CM6030 MFP PCL6",
        "PortName":  "192.168.11.95",
        "Comment":  ""
    },
    {
        "Name":  "Canon iPF510",
        "DriverName":  "Canon iPF510",
        "PortName":  "IP_192.168.11.97",
        "Comment":  ""
    }
]

如您所见,实际上我还没有填写评论字段,但这将是您的情况。

假设您的计划任务随后将该文件保存到 printers.json 中。为此,只需使用 > 运算符将结果重定向到您的文件:

powershell.exe -Command "Get-Printer -ComputerName yourprintserver | select Name,DriverName,PortName,Comment | ConvertTo-Json" > C:\path-to-php-site\config\printers.json

然后我们可以在 PHP 中加载它:

<?php
// Get the list of printers from the automatically updated JSON file.
// Caution: PowerShell creates the file in "UCS-2 LE BOM" format.
//          PHP wants to read UTF-8 so we'll have to convert it.

// Read the JSON created by the scheduled task running PowerShell.
$printers_json_ucs2_le_bom = file_get_contents('printers.json');

// Convert to UTF-8 encoding.
$printers_json_utf8_bom = mb_convert_encoding(
    $printers_json_ucs2_le_bom, // Source content.
    'UTF-8',                    // Destination encoding.
    'UCS-2LE'                   // Source encoding.
);

// Remove the BOM marker which is still there.
$printers_json_utf8 = preg_replace('/\x{FEFF}/u', '', $printers_json_utf8_bom);

// Decode the JSON to a PHP variable.
$printers = json_decode($printers_json_utf8);

/**
 * Get the HTML code to create a select drop down to choose a printer.
 *
 * @param array $printers The list of printers.
 * @param string $name The name of the field in the form.
 * @param string $id THe id of the fiedl in the form.
 * @param string $lable The label to display before the select drop down.
 *
 * @return string The HTML code to put in your form.
 */
function get_printers_select(array $printers, $name = 'printer', $id = 'printer', $label = 'Select your printer')
{
    $html = '';
    if (!empty($label)) {
        $html .= '<label for="' . $id . '">' . htmlspecialchars($label) . "</label>\n";
    }
    $html .= '<select id="' . $id . '" name="' . $name . "\">\n";
    foreach ($printers as $printer) {
        $html .= '  <option value="' . htmlentities($printer->Comment) . '">' .
                 htmlspecialchars($printer->Comment) . "</option>\n";
    }
    $html .= "</select>\n";
    return $html;
}

// Your HTML and <form> should come here...

print get_printers_select($printers);

如果我将 $printer->Comment 替换为 $printer->Name,则会输出此 HTML:

<label for="printer">Select your printer</label>
<select id="printer" name="printer">
  <option value="PDF-cadwork">PDF-cadwork</option>
  <option value="OKI C9800(PS)">OKI C9800(PS)</option>
  <option value="OKI C9800(PCL)">OKI C9800(PCL)</option>
  <option value="Microsoft XPS Document Writer">Microsoft XPS Document Writer</option>
  <option value="HP LaserJet M570 PS">HP LaserJet M570 PS</option>
  <option value="HP Color LaserJet CM6030 MFP PCL6">HP Color LaserJet CM6030 MFP PCL6</option>
  <option value="Canon iPF510">Canon iPF510</option>
</select>

关于php - 下拉菜单中单独的 Powershell 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60225689/

相关文章:

javascript - 使用 Laravel 测试时是否启用了 JS?

php - MySQL 不断返回一行,即使数据库中没有任何内容与查询匹配

php - 在数据库中保存带逗号的数字

php - 重写规则导致403禁止错误

powershell - 加入对象需要哪个版本的 PowerShell ISE?

powershell - 在 PowerShell 中将新行写入文本文件

powershell - powershell:从字符串末尾删除文本

c# - 如何从 C# 中的 Powershell 管道读取错误?

php - mysql表行数限制?

azure - 如何将 azure cli list 命令的值获取到变量中?