javascript - PowerShell生成HTML时如何从-Fragment中排除表,colgroup和first tr?
问题描述
原来我已经table
, thead
(类似于 first tr
)tbody
和 first tr
。问题出在命令中,$tabela += $resultado | ConvertTo-Html -Fragment
它将首先从现有表中输出。table
colgroup
tr
$table
$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>"
推荐阅读
- java - 如何从字符串生成器中提取 json 对象?
- java - 使用 OneToOne 关系一次保存两个实体
- html - 如何以与本网站示例相同的方式进行此 div 块转换
- java - Java 正则表达式每次在字符串中找到数字时添加空格
- javafx - 数据未出现在 javafx 上
- c# - 如何使用按钮单击事件创建对象并将它们添加到列表中?
- python - PyQt5 QListView 已停止从 QSqlTableModel 加载条目,我不知道为什么
- bash - 如何使用 bash 命令更改 XAMPP DocumentRoot?
- ios - iOS 13 快捷方式:地图操作搜索本地企业的问题
- linux - 如何在文件中显示 bash shell 的所有命令?Linux