在低磁盘或RAM上发送电子邮件警报

时间:2017-08-10 10:11:22

标签: powershell email windows-server-2012-r2 alerts

我尝试了几个脚本来通过电子邮件发送硬盘或内存的状态,但它无法正常工作, 第一次使用PowerShell。

Windows Server 2012 R2

脚本会被事件触发(当内存不足时)并发送包含详细信息的电子邮件。

获取我使用的磁盘统计信息

Get-EventLog -LogName System | Where-Object {$_.EventID -eq 2013}

如何将此事件添加到电子邮件中并使其显示在邮件中,我尝试为其命名,如

$event Get-EventLog -LogName System | Where-Object {$_.EventID -eq 2013}

但我不知道如何将其添加到邮件正文中,而不是像java或

$message.body = $body + $event 

发送电子邮件此脚本有效,

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "username@gmail.com"
$Password = "zxc"

$to = "help@x.com"
$cc = "help@x.ae"
$subject = "Low Disk Space"
$body = "The Server Disk is Low on memory"

$message = New-Object System.Net.Mail.MailMessage
$message.Subject = $subject
$message.Body = $body
$message.To.add($to)
$message.Cc.add($cc)
$message.From = $username

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message)
Write-Host "Mail Sent"

我读到电子邮件提醒已被MS停止,但人们仍然有办法实现,不幸的是我没有让它发挥作用。

3 个答案:

答案 0 :(得分:2)

有助于您开始使用此功能:

# We first need to know which command to use
Get-Command '*mail*'

# We use the splatting technique to provide the parameters
$Params = @{
    SmtpServer = 'smtp.gmail.com'
    Port       = '587'
    From       = $username
    To         = 'help@x.com'
    Cc         = 'help@x.ae'
    Subject    = 'Low Disk Space'
    Body       = 'The Server Disk is Low on memory.'
}

# Get-Help explains what this CmdLet does
Get-Help Send-MailMessage

# Get-Help can also give you examples on how to use the CmdLet
Get-Help Send-MailMessage -Examples

# Retrieve only events of the last 24 hours and select the first one
$Today = Get-Date
$Past = $Today.AddDays(-1)
$Event = Get-EventLog -LogName System -After $Past | Where-Object {$_.EventID -eq 6013} | Select-Object -First 1

# Add the event to the mail body
$Params.Body += ' ' + $Event.Message

# Send the mail
Send-MailMessage @Params

然后可以将此脚本添加到Task-Scheduler以每天运行一次。

答案 1 :(得分:0)

SCHTASKS /Create /RU "SYSTEM" /SC DAILY /ST 17:30 /TN DailyHDDMemReport /TR "powershell -NoProfile -NoLogo -ExecutionPolicy Unrestricted -File 'C:\Temp\file.ps1'" /F

这将创建一个每日任务,运行在5:30作为SYSTEM指定的powershell脚本以及DarkLite1的答案。

答案 2 :(得分:0)

经过多次编写脚本并进行处理后,由于脚本后脚本需求增加,功能要求最终使我使用了以下工具的免费选项。

有超过需要的

https://www.manageengine.com/network-monitoring/

此致