2017-08-10 47 views
0

我尝试了几个脚本来通过电子邮件发送HDD或RAM的状态,但其不能正常工作, 第一次使用PowerShell。在低磁盘或RAM上发送电子邮件提醒

的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 

发送邮件这个脚本wo RKS,

$SMTPServer = "smtp.gmail.com" 
$SMTPPort = "587" 
$Username = "[email protected]" 
$Password = "zxc" 

$to = "[email protected]" 
$cc = "[email protected]" 
$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停产,但人们仍然有这样做的方式,不幸的是我没有得到它的工作。

回答

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   = '[email protected]' 
    Cc   = '[email protected]' 
    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到每天运行一次。

+0

所以,我需要一些更多的东西添加到这个脚本您发布,它不是完全正确的。用户名缺失,密码或您发布的内容是我需要根据帮助信息进行调整的参考。 – Admir

+0

@Admir他给你一个起点,解释不同的功能。 – TheIncorrigible1

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指定的powershell脚本作为SYSTEM与DarkLite1的答案一起使用。

相关问题