2012-06-29 40 views
6

Ok, điều này có thể đã được trả lời, nhưng tôi mới và đang cố gắng tìm hiểu, vì vậy tôi đoán tôi không "nhận" nó. Tôi có một biến có chứa một bảng thông tin (tôi đã nhận nó từ một truy vấn SQL). Tôi có thể xuất biến thành màn hình và xem bảng, nhưng khi tôi cố gắng xây dựng một email HTML với nó trong cơ thể, nó bị cắt xén.Bảng hiển thị PowerShell Trong Email HTML

Đây là mã:

# 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) 

Kết quả trên màn hình trông như thế này:

35 

FirstName LastName EMail        Count 
--------- -------- -----        ----- 
John  Doe  [email protected]      51 
Jane  Doe  [email protected]      20 

... nhưng cơ thể email trông như thế này:

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 

... và vân vân. Tôi đang làm gì sai?

Trả lời

10

Đây là cách tôi muốn làm điều đó:

# Create a DataTable 
$table = New-Object system.Data.DataTable "TestTable" 
$col1 = New-Object system.Data.DataColumn Name,([string]) 
$col2 = New-Object system.Data.DataColumn Dept,([string]) 
$table.columns.add($col1) 
$table.columns.add($col2) 

# Add content to the DataTable 
$row = $table.NewRow() 
$row.Name = "John" 
$row.Dept = "Physics" 
$table.Rows.Add($row) 
$row = $table.NewRow() 
$row.Name = "Susan" 
$row.Dept = "English" 
$table.Rows.Add($row) 

# Create an HTML version of the DataTable 
$html = "<table><tr><td>Name</td><td>Dept</td></tr>" 
foreach ($row in $table.Rows) 
{ 
    $html += "<tr><td>" + $row[0] + "</td><td>" + $row[1] + "</td></tr>" 
} 
$html += "</table>" 

# Send the email 
$smtpserver = "smtpserver.domain.com" 
$from = "[email protected]" 
$to = "[email protected]" 
$subject = "test" 
$body = "Hi there,<br />Here is a table:<br /><br />" + $html 
Send-MailMessage -smtpserver $smtpserver -from $from -to $to -subject $subject -body $body -bodyashtml 
+1

+1 để sử dụng gửi thư với -bodyAsHTML –

+0

Tôi đã thử điều đó, nhưng tôi gặp lỗi. Tôi đã chia sẻ toàn bộ mã, có thể bạn có thể thấy những gì tôi đang làm sai? –

+0

OK, điều đầu tiên là loại bỏ: '$ DataSet.Tables | Định dạng-Bảng -Auto' vì điều đó không làm gì cả. Bạn cần phải lấy '$ DataSet' và nhận được một DataTable,' $ table = $ DataSet.Tables [0] '. Một khi bạn có DataTable sử dụng ví dụ của tôi để tự tạo chuỗi $ html. –

2

Bạn có thể thử (không kiểm tra):

$message.Body = $message.Body + ($DataSet.Tables | format-Table -auto | convertto-html) 
+0

Đó dường như không giúp đỡ, bây giờ tất cả tôi nhận được trong email là một danh sách các số GUID giống như ... –

+0

điều này sẽ làm việc, nhưng bạn đã chọn các cột cụ thể mà bạn muốn trong bảng của bạn vì đối tượng datatable bao gồm tất cả các loại thuộc tính được tích hợp sẵn bên cạnh các cột tùy chỉnh của bạn. tức là $ table | chọn col1, col2, colN | convertto-html –

3

Tôi nghĩ rằng đây là một giải pháp tốt hơn (xem ref 1)

$DataSet.Tables[0] |select * -ExcludeProperty RowError, RowState, HasErrors, Name, Table, ItemArray | ConvertTo-Html 

Với ExcludeProperty, tất cả chúng ta đều có thể loại trừ các cột ồn ào khỏi bảng của chúng tôi mà không cần chỉ định đặc biệt những cột nào chúng tôi muốn nó

(1) http://sqlblog.com/blogs/jonathan_kehayias/archive/2010/05/25/reinventing-the-wheel-automating-data-consistency-checks-with-powershell.aspx

0

câu trả lời tổng quát:

$body+=($variable|Select-Object property1,property2|ConvertTo-Html)

+0

Điều này không cung cấp câu trả lời cho câu hỏi. Khi bạn có đủ [danh tiếng] (https://stackoverflow.com/help/whats-reputation), bạn sẽ có thể [nhận xét về bất kỳ bài đăng nào] (https://stackoverflow.com/help/privileges/comment); thay vào đó, [cung cấp câu trả lời không yêu cầu làm rõ từ người hỏi] (https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- i-do-thay thế). - [Từ đánh giá] (/ đánh giá/bài đăng chất lượng thấp/16774381) – OmG

Các vấn đề liên quan