【问题标题】:Send Email alert on low Disk or RAM在磁盘或 RAM 不足时发送电子邮件警报
【发布时间】:2017-08-10 10:11:22
【问题描述】:

我尝试了几个脚本来通过电子邮件发送 HDD 或 RAM 的状态,但它不起作用, 第一次使用 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 已停止发送电子邮件警报,但人们仍有办法做到这一点,不幸的是我没有让它发挥作用。

【问题讨论】:

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


    【解决方案1】:

    一些可以帮助您开始的事情:

    # 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 以每天运行一次。

    【讨论】:

    • 所以,我需要在您发布的这个脚本中添加更多内容,它并不完全正确。用户名丢失,密码或您发布的内容是参考,我需要根据帮助消息进行调整。
    • @Admir 他给你一个起点,解释不同的功能。
    【解决方案2】:
    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 的答案。

    【讨论】:

      【解决方案3】:

      经过多次编写可能的脚本并对其进行处理,由于一个又一个脚本的要求越来越高,功能要求使我最终使用了以下工具的免费选项。

      有更多的需要

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

      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 2021-11-11
        • 2021-04-25
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        相关资源
        最近更新 更多