powershell如何发送html邮件,Powershell检查邮件队列设置阈值,通过html形式进行邮件告警...

#导入 Powershell Exchange 模块

Add-PSSnapin microsoft.exchange*

#取值所有Exchange Server的名称,创建一个新的数组

$computername = (Get-TransportService).name

$sum = 0 #将失败队列总数归0

$array=@()

[System.Collections.ArrayList]$arraylist = $array

#定义字符类型

[String] $body = $null

#输出HTML报告

$report = "d:\QueueCheck\QueueCheck.html"

if((test-path $report) -like $false)

{

new-item $report -type file

}

#创建HTML

$msg = $report

Clear-Content $msg

Add-Content $msg ""

Add-Content $msg "

"

Add-Content $msg "

Add-Content $msg "

Error Check"

Add-Content $msg "

"

Add-Content $msg "

Server Name"

Add-Content $msg "

Error Count"

Add-Content $msg "

"

#检查每一个Server的队列状态

foreach($name in $computername){

#HTML 另起一行

Add-Content $msg "

"

Add-Content $msg "

$name"

$count = (Get-Message -ResultSize Unlimited -Queue $name\submission | where {$_.subject -like "*Error*" -and $_.LastError -ne $null}).count

$null = $arraylist.Add($count)

$sum+=$count

if($count -gt 200){

Add-Content $msg "

$count"

}

else{

Add-Content $msg "

$count"

}

Add-Content $msg "

"

}

$max = ($arraylist | Measure-Object -Maximum).Maximum

#关闭HTML

Add-Content $msg "

"

Add-Content $msg ""

Add-Content $msg ""

#定义邮件内容

$body = Get-Content $report

#发送邮件

if($max -gt 200 -or $sum -gt 1500){

Send-MailMessage -SmtpServer xxx -From xxx -To xxx -Cc xxx -Subject QueueCheck -BodyAsHtml $body

}

#判断ErrorQueue状态

if ($max -gt 200 -or $sum -gt 1500)

{

$status = "Error"

}

else {

$status = "OK"

}

#$message = "Total=$sum,Max=$max,Status=$status"

#连接MySQL

[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")

$mySQLDataDLL = "C:\mysql\MySql.Data.dll"

[void][system.reflection.Assembly]::LoadFrom($mySQLDataDLL)

$Server = "xxx.xxx.xxx.xxx"

$Database = "xxx"

$user = "xxx"

$Password = "xxx"

$charset = "utf8"

$connectionString = "server=$Server;uid=$user;pwd=$Password;database=$Database;charset=$charset;SslMode=none"

$connection = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)

$connection.Open()

#执行MySQL

$insert = "INSERT INTO sys_detect (name,status,type) VALUES('ErrorQueue','$status','queue');"

$insertcommand = New-Object MySql.Data.MySqlClient.MySqlCommand

$insertcommand.Connection = $connection

$insertcommand.CommandText = $insert

$insertcommand.executenonquery()

#关闭连接

$connection.Close()

其实是一个很简单的powershell脚本,同时有制作html的用法,powersehll操作mysql的用法,powershell发送邮件的用法

效果图如下:

189208957_1_20200427074448194.png

如果你希望进一步调整html的样式,可以通过http://www.w3school.com.cn/html/ 进行一定的学习,然后在线测试

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值