gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:10:13 26 4
gpt4 key购买 nike

脚本最初由 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 = @("toSingle@company.com")

#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

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

最佳答案

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

$minGbThreshold = 10;
$computers = $env:COMPUTERNAME;
$smtpAddress = "smtp.yourdomain.com";
$toAddress = "anyone@gmail.com";
$fromAddress = "someone@gmail.com";
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)
}
}
}

关于windows - 磁盘存储警报电子邮件 - Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62131519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com