nakaji
8/31/2016 - 8:45 AM

Azure Cloud Backupの実行結果確認通知(メール)

Azure Cloud Backupの実行結果確認通知(メール)

#
# Azure Cloud Backupの実行結果確認通知(メール)
#

# 基本設定
$from = "admin@example.com"
$to = "caution@example.com"
$password =  "xxxx"
$smtp = "smtp.example.com"
$port = "587"

# 当日のイベントログ取得
$events = Get-WinEvent CloudBackup | ?{ $_.TimeCreated.ToString("yyyyMMdd") -eq [DateTime]::Now.ToString("yyyyMMdd")}

# 件名の編集
$status = "正常"
$events | ?{ $_.LevelDisplayName -eq "警告" } | %{ $status="警告" }
$events | ?{ $_.LevelDisplayName -eq "エラー" } | %{ $status="エラー" }
$subject = "【${status}】AzureBackup結果通知"

# 本文の編集
$body = "{0,-19} {1,2} {2,-16} {3}`n" -f "TimeCreated","Id","LevelDisplayName","Message"
$events | %{ $body += "{0,-19} {1,2} {2,-16} {3}`n" -f $_.TimeCreated,$_.Id,$_.LevelDisplayName,$_.Message }

$subject
$body

# メール送信
$sc = New-Object Net.Mail.SmtpClient($smtp, $port)
$sc.Credentials = New-Object Net.NetworkCredential($from, $password)
$sc.Send($from, $to, $subject, $body)