簡體   English   中英

當PowerShell生成HTML時,如何從-Fragment中排除表,colgroup和第一個tr?

[英]How to exclude table, colgroup and first tr from -Fragment when PowerShell generates HTML?

原來我已經有tablethead (類似於 first tr )、 tbody和 first tr 問題在於命令$tabela += $resultado | ConvertTo-Html -Fragment $tabela += $resultado | ConvertTo-Html -Fragment ,它將 output tablecolgroup和現有表中$table中的第一個tr

$relatorio = $null
$tabela = $null
$data = Get-Date -format "dd/MM/yyyy" 
$arquivo = "relatorio-de-usuarios.html"
$total = (Get-ADUser -filter *).count
$dominio = (Get-ADDomain).Forest
$responsavel = "teste"
$empresa = "teste"
$logotipo = "logo.jpg"

Import-Module ActiveDirectory

$titulo = "<h3>TOTAL DE USUARIOS - <span>$total</span></h3>"

$usuarios = @(Get-ADUser -filter * -Properties Department, Name, SamAccountName, lastLogonTimestamp, lastLogon, msDS-UserPasswordExpiryTimeComputed, Enabled, Created, MemberOf)

$resultado = @($usuarios | Select-Object @{n="Departamento";e={[string]$_.Department}}, @{n="Funcionario";e={[string]$_.Name}}, @{n="Nome de Logon";e={[string]$_.SamAccountName}}, @{n='lastLogonTimestamp';e={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} , @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}, @{n='Expiraηγo de senha';e={[DateTime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}, @{n="Habilitada";e={[string]$_.Enabled}}, @{n="Data de Criaηγo";e={[string]$_.Created}})

$resultado = $resultado | Sort-Object "Funcionario" 
$tabela += $resultado | ConvertTo-Html -Fragment

$inicio =
'
<!DOCTYPE html>
<html lang="pt-BR">
'

$estilo =
'
<head>
  <meta charset="UTF-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width = device-width, initial-scale = 1.0">
  <link rel="stylesheet" src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
  <title>Teste</title>
</head>
'

$formatacao =
'
<body>

  <table cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <p alt="pular linha"></p>
      <img src="' + $logotipo + '" alt="height-altura e width-largura">

      <h1>Active Directory - Relatório de Login</h1>
      <h3>Empresa:"' + $empresa + '"- Domínio:"' + $dominio + '" - Relatório:"' + $data + '" - Responsável:"' + $responsavel + '"</h3>
    </td>
  </tr>
  </table>

  ' + $titulo + '
  <table id="tabela">
    <thead>
      <tr>
        <th>Departamento</th>
        <th>Funcionários</th>
        <th>Nome de Logon</th>
        <th>Logon1</th>
        <th>Logon2</th>
        <th>Expiração de senha</th>
        <th>Habilitada</th>
        <th>Data de Criação</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th><input type="text" id="txtColuna1" /></th>
        <th><input type="text" id="txtColuna2" /></th>
        <th><input type="text" id="txtColuna3" /></th>
        <th><input type="text" id="txtColuna4" /></th>
        <th><input type="text" id="txtColuna5" /></th>
        <th><input type="text" id="txtColuna6" /></th>
        <th><input type="text" id="txtColuna7" /></th>
        <th><input type="text" id="txtColuna8" /></th>
      </tr>
    </tbody>
    ' + $tabela + '
  </table>

  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
  <script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="main.js"></script>
</body>
'

$fim = '</html>'

$relatorio = $inicio + $estilo + $formatacao + $fim
$relatorio | Sort-Object Funcionario | Out-File $arquivo -Encoding Utf8

ConvertTo-Html -Fragment 將創建整個表格。 如果只需要行,您需要自己執行此操作。 你可以做這樣的事情

$tableRows = ($resultado | ForEach-Object {
    '<tr><td>' + $_.Departamento + '</td>' +
    '<td>' + $_.'Nome de Logon' + '</td>' +
    '<td>' + $_.lastLogonTimestamp + '</td>' +
    '<td>' + $_.LastLogon + '</td>' +
    '<td>' + $_.'Expiraηγo de senha' + '</td>' +
    '<td>' + $_.'Habilitada' + '</td>' +
    '<td>' + $_.'Data de Criaηγo' + '</td></tr>' 
} )-join ""

然后您可以在輸入行之后將表行注入表中

  <table id="tabela">
    <thead>
      <tr>
        <th>Departamento</th>
        <th>Funcionários</th>
        <th>Nome de Logon</th>
        <th>Logon1</th>
        <th>Logon2</th>
        <th>Expiração de senha</th>
        <th>Habilitada</th>
        <th>Data de Criação</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th><input type="text" id="txtColuna1" /></th>
        <th><input type="text" id="txtColuna2" /></th>
        <th><input type="text" id="txtColuna3" /></th>
        <th><input type="text" id="txtColuna4" /></th>
        <th><input type="text" id="txtColuna5" /></th>
        <th><input type="text" id="txtColuna6" /></th>
        <th><input type="text" id="txtColuna7" /></th>
        <th><input type="text" id="txtColuna8" /></th>
      </tr>   
      ' + $tableRows + '
    </tbody>
  </table>

或者,您可以使用表格片段並按照您的要求跳過前 3 行和最后一行,而不是所有這些

$tabela = $tabela | Select-Object -Skip 3 -First ($tabela.Count - 4)

或者你可以抓住以<tr><td>開頭的行

$tabela = $tabela -match "^<tr><td>"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM