HTML-таблица в выводе powershell - PullRequest
0 голосов
/ 06 ноября 2018

У меня проблема с выводом html. Мой код с циклом, который последовательно берет IP-адрес из списка и составляет следующую таблицу. Мне нужны еще таблицы из данных других коммутаторов.

enter image description here

Моя проблема в том, что я не знаю точно, как повторить только таблицу + ее заголовок, а затем написать и выпустить ее в одном HTML-файле.

Это мой HTML-код таблицы, которую я называю переменной.

$Table = @" 
<table 
width: 912px;
border-collapse: collapse;
border-width: 2px;
border-style: solid;
border-color: grey;
font: 14pt Calibri;
color: black;
margin-bottom: 10px;
text-align: center;>

<h2>Switch 1</h2>
<div class=subtitle>
THIS is table from switch 1
</div>

<tbody>
<tr>   
<th
height="18" width="90"; 
<td>
Adresa IP</span>
     </td>
     </th>

<th
height="18" width="90"; 
<td>
Počet portů</span>
     </td>
     </th>

<th
height="18" width="90"; 
<td>
Obsazené porty</span>
     </td>
     </th>

<th
height="18" width="90"; 
<td>
Volné porty</span>
     </td>
     </th>

<th
height="18" width="440"; 
<td>
Neaktivní porty</span>
     </td>
     </th>
</tr>

     <tr>
     <td 
height="18" width="90";
border-bottom-width: 2px; 
border-bottom-style: inset;">
    <span 
style="color: black; 
font-family: Calibri; 
font-size: 11pt; ">
$_</span>
     </td>

     <td 
height="18" width="90"; 
border-bottom-width: 2px; 
border-bottom-style: inset;">
    <span 
style="color:black; 
font-family: Calibri; 
font-size: 12pt; ">
$PocetPortu</span>
      </td>

      <td 
height="18" width="90"; 
border-bottom-width: 2px; 
border-bottom-style: inset;">
    <span 
style="color:black; 
font-family: Calibri; 
font-size: 11pt; ">
$OP
</span>
      </td>

      <td 
height="18" width="90"; 
border-bottom-width: 2px; 
border-bottom-style: inset;">
     <span 
style="color:black; 
font-family: Calibri; 
font-size: 11pt; ">
$PP</span>
     </td>

     <td 
height="18" width="440"; 
border-bottom-width: 2px; 
border-bottom-style: inset;">
     <span 
style="color:black; 
font-family: Calibri; 
font-size: 11pt; ">
$vypis</span>
     </td>
</tbody>
</table>
"@

Только одна, последняя таблица, всегда появляется на странице вывода, и у меня есть HTML-код, захваченный в цикле, где я получаю значения в переменные.

Команда вызывается с помощью:

$html | ConvertTo-Html -Head $Top_Head, $Style -Body $Table  -PostContent $Post_text | Out-File C:\test\test.html

Invoke-Expression C:\test\test.html

Спасибо.

1 Ответ

0 голосов
/ 06 ноября 2018

Выведите ваши данные на объект, а затем используйте ConvertTo-Html, чтобы выполнить тяжелую работу и создайте HTML для таблицы.

Это не позволит вам использовать встроенный CSS, который у вас уже есть, но всю информацию о стиле можно переместить в одно место с помощью <style></style>

Я не знаю, как вы получаете информацию о переключателе, поэтому я жестко закодировал значения, чтобы использовать в качестве примера значения:

# array containing a value for input into code to get switch information
$switches = '192.168.7.1','10.0.7.1'

foreach ($switch in $switches) {

    # your code to get switch information
    # output values from this code go in object below
    # example values used as we don't have this code

    $switch_info = [PSCustomObject]@{
        'Adresa IP' = $switch
        'Počet portů' = '48'
        'Obsazené porty' = '38'
        'Volné porty' = '10'
        'Neaktivní porty' = '7 8 13 14'
    }

    $table_title = "<div class=subtitle>This is table from switch $($switches.IndexOf($switch) + 1)</div>"
    $table_html = $switch_info | ConvertTo-Html -Fragment | Out-String

    $tables += $table_title
    $tables += $table_html
}

$Style = @'
<style>
table{
width: 912px;
border-collapse: collapse;
border-width: 2px;
border-style: solid;
border-color: grey;
font: 14pt Calibri;
color: black;
margin-bottom: 10px;
text-align: center;
}
th {
    background-color: #0000ff;
    color: white;
}
</style>
'@

$Top_Head = '<div>TopHead</div>'
$Post_text = '<div>PostText</div>'

ConvertTo-Html -Head $Top_Head,$Style -Body '<h2>Switch Information</h2>',$tables -PostContent $Post_text | Out-File C:\test\test.html

РЕДАКТИРОВАТЬ: Только что видел имена ваших переменных, вы, вероятно, можете использовать их так:

    $switch_info = [PSCustomObject]@{
        'Adresa IP' = $switch
        'Počet portů' = $PocetPortu
        'Obsazené porty' = $OP
        'Volné porty' = $PP
        'Neaktivní porty' = $vypis
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...