2016-09-19 6 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 <[email protected]>" -Subject "Microsoft Update Session" -Body $MailBody -SmtpServer "your.smtpserver.com" 
+0

나는 또한 위와 같은 메시지를 받았다. 게시하기 전에 알아 내려고 할 때 나는 그것을 본 것을 안다. Send-MailMessage : 'System.Object []'을 'Body'매개 변수에 필요한 'System.String'유형으로 변환 할 수 없습니다. 지정된 메소드는 지원되지 않습니다. 줄 : 19 자루 : 127 – rdd2