我编写了一个 powershell 代码来为选定的用户在电子邮件正文中生成一个表格。由于某种原因执行的结果包括“RowError”、“RowState”、“Table”、“ItemArray”和“HasErrors”列。我不知道这些隐藏列是来自我的代码正在读取的 SQL 视图,还是我自己的代码在处理的每一行中生成它们。我尝试让 PowerShell 忽略 HTML 正文中的列,但发送的每封测试电子邮件都没有看到任何变化。
代码
# Database connection parameters
$server = 'BIG-YELLOWSERVER'
$database = 'THECookies'
# SQL query
$query = "SELECT * FROM dbo.[All_My_Cookies] ORDER BY Chips, 'Dough'"
# Create a new SQL connection object using Windows Authentication
$connectionString = "Server=$server;Database=$database;Integrated Security=True;"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $connectionString
# Open SQL connection
$sqlConnection.Open()
# Execute the query and retrieve the data
$command = $sqlConnection.CreateCommand()
$command.CommandText = $query
$result = $command.ExecuteReader()
# Load data into a DataTable
$table = New-Object System.Data.DataTable
$table.Load($result)
# Remove unwanted columns
$columnsToRemove = @("RowError", "RowState", "Table", "ItemArray", "HasError")
foreach ($column in $columnsToRemove) {
if ($table.Columns[$column]) {
$table.Columns.Remove($column) | Out-Null
}
}
# Close the SQL connection
$sqlConnection.Close()
# Lets attach the report for good measure
$csvFileName = "4KV Grid CVO Summary $(Get-Date -Format 'yyyy-MM-dd').csv"
$table | Export-Csv -Path $csvFileName -NoTypeInformation
# Define the CSS styles for the HTML tables
$style = @"
<style>
th {
background-color: #b0c4de;
color: black;
font-family: Arial, sans-serif;
font-size: 8pt;
}
td, th {
border: 1px solid #ddd;
padding: 1px;
color: black;
font-family: Arial, sans-serif;
font-size: 8pt;
}
tr:nth-child(even) {
background-color: #ffffff;
color: black;
font-family: Arial, sans-serif;
font-size: 8pt;
}
tr:nth-child(odd) {
background-color: #e6e6e6;
color: black;
font-family: Arial, sans-serif;
font-size: 8pt;
}
tr:hover {
background-color: #ddd;
}
</style>
"@
# Group the data by 'Region' and create HTML tables
$groupedTables = $table | Group-Object -Property Region | ForEach-Object {
$region = $_.Name
$regionHeader = "<h2>$region Region</h2>"
$regionTable = $_.Group | ConvertTo-Html -Fragment
$regionHeader + $regionTable
}
# Join all HTML strings into one and include the CSS styles
$htmlContent = $style + ($groupedTables -join "`n")
# Convert the data to HTML format and store in a variable
$htmlTable = $htmlContent
$emailBody = $introMessage + "The Ultimate Cookie report.`nPlease confirm receipt of this report. Thanks`n`n" + $htmlTable
# Email parameters
$smtpServer = "exchsmtp.cookiemonster.net"
$from = "[email protected]"
# Official Recipients
# $to = @("[email protected]")
$subject = "The Ultimate Cookie Report"
# Send the email
Send-MailMessage -From $from -To $to -Subject $subject -Body $emailBody -BodyAsHtml -SmtpServer $smtpServer -Attachments $csvFileName
数据库视图
SELECT TOP (1000) [Cookies]
,[Bigbird TVE]
,[Smurfberries in Effect]
,[White Chocolates]
,[VR1]
,[VR1%]
,[VR2]
,[VR2%]
,[VR3]
,[VR3%]
,[Previous Day Change]
,[Physical Pies]
,[TimeStamp]
,[reportDate]
FROM [CookieReport].[dbo].[I_Like_Cookies]
期望的结果
尝试从
RowError
中删除 $table.Columns
等不会给您带来任何好处,因为它们不是列值 - 它们是附加到表中包含的 DataRow
每个实例的属性。
ConvertTo-Html
不知道表中的属性和行元数据中的属性之间的区别,因此我们需要显式地将列名称传递给其 -Property
参数 - 以及我们可以从 $table.Columns
获取的内容:
$columnNames = $table.Columns |ForEach-Object ColumnName
# Group the data by 'Region' and create HTML tables
$groupedTables = $table | Group-Object -Property Region | ForEach-Object {
$region = $_.Name
$regionHeader = "<h2>$region Region</h2>"
$regionTable = $_.Group | ConvertTo-Html -Fragment -Property $columnNames
$regionHeader + $regionTable
}