我研究了 Google,尝试了几种不同的选项,但没有一个能提供我想要的结果。此脚本生成了包含我所需数据的电子邮件,但无论阈值如何,它都会发送电子邮件。我希望得到帮助,知道我需要更改什么,以便脚本仅在驱动器低于阈值时发送电子邮件。
$freeSpaceThreshold = 5GB
#$computerName = "mycomputer"
$drive = "C:"
$driveData = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
if ($driveData.FreeSpace -lt $freeSpaceThreshold)
{
$from = "[email protected]"
$to = "[email protected]"
$subject = "Low Disk Space!"
$body = "Free Space Remaining: " + "$($CDisk.FreeSpace)GB" + " Drive" + $_.deviceid
$smtpServer = "smtp.domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($from,$to,$subject,$body)
}
答案1
5GB
解析为5368709120
相同的值,但以字节而不是千兆字节为单位。
PS C:\Windows\system32> $freeSpaceThreshold = 5GB
PS C:\Windows\system32> $freeSpaceThreshold
5368709120
当您在比较中使用它时,您需要按照除比较值相同的方式除它。例如($_.FreeSpace / 1GB)
和($freeSpaceThreshold / 1GB )
。或者您根本就不除,因为 freespace 属性也是字节。
另外,在格式化之前先进行计算。我会这样做:
$freeSpaceThreshold = 5GB
$Computer = "MyComputer"
# The Disks Variable will contain all Disks that are below the freeSpaceTreshold
$Disks = Get-CimInstance Win32_LogicalDisk -ComputerName $Computer |
Where-Object { $_.DriveType -eq 3 -and $_.FreeSpace -lt $freeSpaceThreshold }
if ($Disks) {
# Put your Formatting and E-Mail Code here.
}
这样你就会得到正确的结果。
答案2
首先,为什么你不使用内置的 PowerShell 邮件 cmdlet与使用.Net 邮件命名空间相比?
其次,您在主体中使用了未声明的变量,并且 $PSItem(又名 $_.)在您编写的代码帖子中根本没有传递。
# Your declared variable
$freeSpaceThreshold = 5GB
#$computerName = "mycomputer"
$drive = "C:"
# Variables you are trying to use that don't exist from the above
"$($CDisk.FreeSpace)GB" + " Drive" + $_.deviceid
最后,您会得到一切,因为这正是您所要求的。
Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
注意事项:如果可以控制的话,请避免使用包含空格的属性名称、文件名。您只会因为一堆引用内容而感到不必要的麻烦。
所以,就按照这个方法试试吧。 (这种格式只是我的阅读习惯。所以,你可以使用你喜欢的格式。)
根据您对以下 auth 和 SSL 的评论进行了更新
$freeSpaceThreshold = 500
$computerName = $env:COMPUTERNAME
$drive = 'C:'
$driveData = Get-WmiObject Win32_LogicalDisk -ComputerName $computerName -Filter DriveType=3 |
Select-Object DeviceID,
@{
'Name' = 'Size'
'Expression' = {[math]::truncate($PSItem.size / 1GB)}
},
@{
'Name' = 'Freespace'
'Expression' = {[math]::truncate($PSItem.freespace / 1GB)}
} | Where-Object -Property Size -lt $freeSpaceThreshold
<#
# Results
DeviceID Size Freespace
-------- ---- ---------
C: 476 120
D: 476 357
#>
if ($driveData.FreeSpace -lt $freeSpaceThreshold)
{
$SendMailMessageSplat = @{
From = "$env:USERNAME@$env:USERDNSDOMAIN"
To = "$env:USERNAME@$env:USERDNSDOMAIN"
Subject = "Warning hostname $computerName Low Disk Physical Space"
Body = "Free Space Remaining: $($driveData.FreeSpace)GB Drive $($driveData.deviceid)"
SmtpServer = "smtp.$env:USERDNSDOMAIN"
UseSsl = $true
Port = 587
Credential = Get-Credential
}
Send-MailMessage @SendMailMessageSplat
}
答案3
我终于得到了一个可行的脚本。这就是我一直在寻找的答案。希望它能帮助其他人。
$minGbThreshold = 29;
$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)
}
}
}