磁盘存储警报电子邮件-Powershell

问题描述 投票:0回答:1

最初由GhillieHammer创建的脚本

我添加了其他功能来为我工作。

# $drives = @("D","E","F");
$drives = $null

# The minimum disk size to check for raising the warning
$minSize = 20GB
$MathminSize = [math]::round($minSize/1GB,2)
$minString = ($MathminSize).ToString()
$minString = $minString + "GB"

$email_to_addressArray = @("");
# $email_to_addressArray = @("[email protected]")

#Set a couple needed variables
$SendIt = $Null
$ThisHost = $env:computername
$IPAddy = Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) | Select ip

#Check for $Drives query mode and make an array of them
if ($drives -eq $null -Or $drives -lt 1) {

    $localVolumes = Get-WmiObject win32_volume;
    $drives = @();

    foreach ($vol in $localVolumes) {

        if ($vol.DriveType -eq 3 -And $vol.DriveLetter -ne $null ) {

          $drives += $vol.DriveLetter[0];

        }

    }

}

# Enumerate through the array of drives
# check to see if any are below minimum
# if so, set a flag saying as much and then
# add them and their information to the array we'll be adding to the email
foreach ($d in $drives) {

    $disk = Get-PSDrive $d;
    $MathDiskFree = [math]::round($disk.Free/1GB,2)
    $MathDiskUsed = [math]::round($disk.Used/1GB,2)
    $MathDisktotal = [math]::round($MathDiskFree + $MathDiskUsed)
    $MathDiskPerc = ($MathDiskFree / $MathDiskUsed).tostring("P")

    if ($disk.Free -lt $minSize) {

        $SendIt = 1
        $space += ("Free space on drive " + $d + " = " + $MathDiskFree + "GB. This is equal to only " + $MathDiskPerc + "  of the " + $MathDisktotal + "GB total space available on this drive.<br>")

    }

}
# Check the flag to see if it's set, meaning there's at least one drive below minimum free space, and if so, fire off the email(s)
If ($SendIt -eq 1) {

    # Enumerate through the array of email addresses and fire off a formatted email to each
    foreach ($toAddress in $email_to_addressArray) {

$User = "diskspace@"
$File = (Get-Content C:\Temp\pw.txt | ConvertTo-SecureString)

$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, $File

        $To = $toAddress
        $from = ""
        $EmailSubject = "WARNING: one or more disk on $ThisHost low on space"
        $smtp = "auth.smtp.1and1.co.uk"

        $DefaultMessage="
            <p>Dear colleague,</p>
            <p>There is a hard drive space issue on $ThisHost IPv4: $IPAddy </p>
            <p>$space<br></p>
            <p>This message only fires off if one or more disks have less than $minString GB of free space left.</p>
            <p>Sincerely,<br>
              Robot Monitor.<br><br>
            </p>"

        $MailMessage = @{
                To = $To
                From = $from
                # BCC = $Bcc
                Subject = $EmailSubject
                Body = $DefaultMessage
                priority = "High"
                Smtpserver = $smtp
                Credential = $MyCredential
                ErrorAction = "SilentlyContinue" 
            }

        Send-MailMessage @MailMessage -bodyashtml

    }

} 

脚本运行良好,但似乎无法停止生成以下信息

enter image description here

似乎无法找到或理解它是如何生成更多文本行的。任何见解将不胜感激!

windows powershell storage
1个回答
0
投票

请尝试使用以下更简化的代码。我在需要它的机器上将它与计划任务一起使用。仅当磁盘空间低于阈值时才发送电子邮件。您可以对其进行修改以与您拥有的其他驱动器一起使用。目前尚不清楚如何获得多行输出,但是对其进行修改将有助于解决问题并简化代码。

$minGbThreshold = 10;
$computers = $env:COMPUTERNAME;
$smtpAddress = "smtp.yourdomain.com";
$toAddress = "[email protected]";
$fromAddress = "[email protected]";
foreach($computer in $computers)
{    
    $disks = Get-WmiObject -ComputerName $computer -Class Win32_LogicalDisk -Filter "DriveType = 3";
    $computer = $computer.toupper();
    $deviceID = $disk.DeviceID;
    foreach($disk in $disks)
    {
        $freeSpaceGB = [Math]::Round([float]$disk.FreeSpace / 1073741824, 2);
        if($freeSpaceGB -lt $minGbThreshold)
        {
            $smtp = New-Object Net.Mail.SmtpClient($smtpAddress)
            $msg = New-Object Net.Mail.MailMessage
            $msg.To.Add($toAddress)
            $msg.From = $fromAddress
            $msg.Subject = “Diskspace below threshold ” + $computer + "\" + $disk.DeviceId
            $msg.Body = $computer + "\" + $disk.DeviceId + " " + $freeSpaceGB + "GB Remaining";
            $smtp.Send($msg)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.