服务器管理 - 需要脚本来监控服务器上的可用空间 [英] Server Management - Need script to monitor free space on server

查看:37
本文介绍了服务器管理 - 需要脚本来监控服务器上的可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要脚本来监控服务器上的可用空间,如果可用内存空间达到一定的阈值发送警报邮件.

Need script to monitor free space on server and if the free memory space goes done certain threshold send alert mail.

PS- 我认为解决方案是 Power Shell + Windows Timer Job.不过,我是 Power Shell 的新手.

PS- I think the solution will be Power Shell + Windows Timer Job. I am new to Power Shell though.

推荐答案

您可以使用如下命令获得可用磁盘空间:

You can get free disk space using a command like this:

([wmi]"\\$computer\root\cimv2:Win32_logicalDisk.DeviceID='$drive'").FreeSpace

您可以使用以下功能发送电子邮件:

And you cand send an email using the function below:

function Send-EMail 
{ 
    param (
        [parameter(Mandatory = $false)][string] $EmailTo = "<Your destination email>",
        [parameter(Mandatory = $false)][string] $EmailFrom = "<The sending email address>",
        [parameter(Mandatory = $false)][string] $EmailSubject = "Disk space problem",
        [parameter(Mandatory = $false)][string] $SMTPServer = "<your smtp server>"
    )

    $MailMessage = New-Object System.Net.Mail.MailMessage  
    $MailMessage.From = ($EmailFrom)  
    $MailMessage.To.Add($EmailTo) 
    $MailMessage.Subject = $EmailSubject 
    $MailMessage.Body = $EmailBody 
    $MailMessage.IsBodyHTML = $true 

    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)   
    $SMTPClient.Send($MailMessage) 
} 

现在将这两个函数组合在一个 PowerShell 脚本中,您可以 使用 Windows 调度程序进行调度.

Now combine these two functions in a PowerShell script that you can schedule with Windows scheduller.

这篇关于服务器管理 - 需要脚本来监控服务器上的可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆