2012-06-29 21 views
6

これは答えられたかもしれませんが、私は新しく学びたいので、私はそれを「取得」していないと思います。私は情報のテーブルを含んでいる変数を持っています(SQLクエリから取得しました)。私は変数を画面に出力してテーブルを見ることができますが、HTMLメールをボディに入れようとすると文字化けします。PowerShellの表示表HTML形式の電子メール

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

出力画面上で次のようになります:

35 

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

...しかし、電子メールの本文は、次のようになります。

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 

ここ

は、コードがあります...等々。私は間違って何をしていますか?

答えて

10

は、ここで私はそれを行うだろう方法は次のとおりです。

# 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と-bodyAsHTMLのsend-mailmessageの使用 –

+0

私はそれを試みましたが、エラーが発生しています。私は完全なコードを共有しました、多分あなたは私が間違っているのを見ることができますか? –

+0

OK、最初に取り除くのは: '$ DataSet.Tables | Format-Table -Auto' これは何もしないためです。 '$ DataSet'を取得し、DataTable、' $ table = $ DataSet.Tables [0] 'を取得する必要があります。 DataTableを作成したら、私の例を使って手動で$ html文字列を作成します。 –

2

あなたは(テストしていません)試すことができます。

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

今、私は電子メールで取得していますすべてのGUIDのような番号のリストである、助けていないようでした... –

+0

これは機能しますが、データテーブルオブジェクトには、カスタム列以外のすべての種類のプロパティが組み込まれているため、テーブルで必要な特定の列を選択しています。 すなわち $ table | col1、col2、colNを選択します。 convertto-html –

3

私はこれがより良い解決策だと思います(参考文献1を参照)

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

ExcludePropertyを使用すると、私たちはテーブルから雑音のある列を除外できます。特別な列を指定する必要はありません。

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

0

一般答え:

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

+0

これは質問に対する答えを提供しません。十分な[評判](https://stackoverflow.com/help/whats-reputation)があれば、[投稿にコメントする]ことができます(https://stackoverflow.com/help/privileges/comment)。代わりに、[質問者からの明確化を必要としない回答を提供する](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- i-do-代わりに)。 - [レビューから](/レビュー/低品質の投稿/ 16774381) – OmG

関連する問題