服务器管理 - 需要脚本来监控服务器上的可用空间
问题描述:
需要脚本来监控服务器上的可用空间,如果可用内存空间达到一定的阈值发送警报邮件.
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.