Ok, das könnte beantwortet worden sein, aber ich bin neu und versuche zu lernen, also denke ich, dass ich es nicht "bekomme". Ich habe eine Variable, die eine Tabelle von Informationen enthält (ich habe es aus einer SQL-Abfrage). Ich kann die Variable auf dem Bildschirm ausgeben und sehe die Tabelle, aber wenn ich versuche, eine HTML-E-Mail damit im Körper zu erstellen, wird es verstümmelt. HierPowerShell Anzeigetabelle in HTML E-Mail
ist der Code:
# This code sets up the HTML table in a more friendly format
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
# This code defines the search string in the IssueTrak database tables
$SQLServer = "Blah"
$SQLDBName = "Blah"
$SQLUsername = "Blah"
$SQLPassword = "Blah"
$SQLQuery = "SELECT u.FirstName,u.LastName,u.EMail,COUNT(UserID) as Count
FROM dbo.Issues i with(nolock)
JOIN DBO.Users u with(nolock) on u.UserID = i.NextActionBy
where i.Status='Open'
group by u.FirstName,u.LastName,u.EMail
order by Count(*) desc"
# This code connects to the SQL server and retrieves the data
$SQLConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; uid = $SQLUsername; pwd = $SQLPassword"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
# This code outputs the retrieved data
$DataSet.Tables | Format-Table -Auto
# This code emails a report regarding the results of the retrieved data
$smtpServer = "Blah"
$smtpFrom = "Blah"
$smtpTo = "Blah"
$messageSubject = "IssueTrak Open Issues By Next Action Report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = "Here is a listing of open issues in IssueTrak, sorted by Next Action.<br><br>"
$message.Body = $message.Body + $html
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
Die Ausgabe auf dem Bildschirm sieht wie folgt aus:
35
FirstName LastName EMail Count
--------- -------- ----- -----
John Doe [email protected] 51
Jane Doe [email protected] 20
... aber die E-Mail-Körper sieht wie folgt aus:
Here is a listing of open issues in IssueTrak, sorted by Next Action.
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
...und so weiter. Was mache ich falsch?
+1 für die Verwendung von send-mailmessage mit -bodyAsHTML –
Ich habe das versucht, aber ich bekomme Fehler. Ich habe den vollständigen Code geteilt, vielleicht kannst du sehen, was ich falsch mache? –
OK, die erste Sache ist loswerden: '$ DataSet.Tables | Format-Tabelle -Auto' weil das nichts macht. Sie müssen '$ DataSet' nehmen und eine DataTable bekommen,' $ table = $ DataSet.Tables [0] '. Sobald Sie die DataTable haben, verwenden Sie mein Beispiel, um die $ html-Zeichenfolge manuell zu erstellen. –