2016-09-19 23 views
-1

首先,我不是很懂PS。需要下面的腳本輸出併發送到電子郵件

我在科技網站的評論中發現了以下腳本,它完成了我想要的任何內容,但想知道是否可以將輸出生成到電子郵件正文中併發送到指定的任何地址。腳本如下。

$Session = New-Object -ComObject "Microsoft.Update.Session" 
$Searcher = $Session.CreateUpdateSearcher() 
$historyCount = $Searcher.GetTotalHistoryCount() 
$Searcher.QueryHistory(0, $historyCount) | Select-Object Date,  
@{name="Operation"; expression={switch($_.operation){ 
    1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, 
@{name="Status"; expression={switch($_.resultcode){ 
    1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 
    4 {"Failed"}; 5 {"Aborted"} 
}}} 

回答

0

就像這樣,但您需要在Send-MailMessage命令中添加正確的信息。

這裏是發送-MAILMESSAGE的TechNet有關可能的參數和內容的詳細信息不https://technet.microsoft.com/en-us/library/hh849925.aspx

$Session = New-Object -ComObject "Microsoft.Update.Session" 

$Searcher = $Session.CreateUpdateSearcher() 

$historyCount = $Searcher.GetTotalHistoryCount() 

$result = $Searcher.QueryHistory(0, $historyCount) | Select-Object Date,@{name="Operation"; expression={switch($_.operation){ 
    1 {"Installation"}; 
    2 {"Uninstallation"}; 
    3 {"Other"}}}}, 
    @{name="Status"; expression={switch($_.resultcode){ 
    1 {"In Progress"}; 
    2 {"Succeeded"}; 
    3 {"Succeeded With Errors"}; 
    4 {"Failed"}; 
    5 {"Aborted"}}}} 

$html = "Dear Person, <br/>" 
$html += "Here are the windows update results <br/>"; 
$html += "<table>" 
$html += "<tr>" 
$html += "<th>Date of Install</th>" 
$html += "<th>Operation</th>" 
$html += "<th>Status</th>" 
$html += "</tr>" 

foreach ($line in $result) 
{ 
    $date = $line.Date.ToShortDateString() 
    $op = $line.Operation 
    $stat = $line.Status 
    $html += "<tr>" 
    $html += "<td> $date </td>" 
    $html += "<td> $op </td>" 
    $html += "<td> $stat </td>" 
    $html += "</tr>" 
} 
$html += "</table>" 
Send-MailMessage -SmtpServer "smtp.server" -Body $html -BodyAsHtml -From "[email protected]" -To "[email protected]" -Subject "subject" 
+0

我覺得我幾乎得到了這樣的事情,但我得到同樣的錯誤信息 - 發送-MAILMESSAGE:無法將「System.Object的[]」的類型「System.String」要求的參數「博DY」。指定的方法不受支持。 在線:19 char:127 – rdd2

+0

嘗試將-Body $結果更改爲-Body $ Result.ToString() – Kage

+0

不要擔心不起作用。我做了一個新的編輯嘗試, – Kage

0

如果你被允許直接通過SMTP發送郵件,你可以把你的輸出到一個變量,並將其發送與Send-MailMessage

$Session = New-Object -ComObject "Microsoft.Update.Session" 
$Searcher = $Session.CreateUpdateSearcher() 
$historyCount = $Searcher.GetTotalHistoryCount() 
$MailBody = $Searcher.QueryHistory(0, $historyCount) | Select-Object Date, 

@{name="Operation"; expression={switch($_.operation){ 

    1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}}, 

@{name="Status"; expression={switch($_.resultcode){ 

    1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 

    4 {"Failed"}; 5 {"Aborted"} 

}}} 


Send-MailMessage -From "Your Name <[email protected]>" -To "Receiver1 <[email protected]>", "Receiver2 <Re[email protected]>" -Subject "Microsoft Update Session" -Body $MailBody -SmtpServer "your.smtpserver.com" 
+0

我也在上面得到了同樣的信息,當我在發佈之前試圖找出它,我知道我已經看到它。 Send-MailMessage:無法將'System.Object []'轉換爲參數'Body'所需的類型'System.String'。指定的方法不受支持。 在線:19 char:127 – rdd2

相關問題