gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:17:50 24 4
gpt4 key购买 nike

我从以下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>`

但是我找不到一种在每个Box中用一个值分隔输出的方法。

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命令。这非常非常慢,您的页面完全不会响应。

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

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

该命令将变为:

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": ""
}
]

如您所见, 我实际上还没有填写的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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com