自从我编写脚本或编写除SQL以外的任何内容以来已经很久了,并且由于约束需要获得一个脚本,该脚本会向我显示所有存储(包含挂载点),且不到10%,到目前为止我已经来到这里但是我是仍然坚持如何做迭代器。
我正在努力将电子邮件格式化为任何帮助吗?
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)}}
$emailFrom="example@example.com"
$emailTo="test@example.com"
function get-mountpoints {
$volumes = Get-WmiObject win32_volume -Filter "DriveType='3'" -ComputerName someComputer| Where-Object {$_.DriverLetter -eq $null}
if(($volumes | Select VolumeName, Label, $TotalGB, $FreeGB, $FreePerc | ?{$_."Free(%)" -lt 20} | Measure-Object).Count -ne 0)
{
$volumes | Select VolumeName, Label, $TotalGB, $FreeGB, $FreePerc | ?{$_."Free(%)" -lt 20} | Format-Table -AutoSize
$Subject="Disk $volumes.Label on $hostname has less than $volumes.$FreeGB GB of free space left, which is $volumes.$FreePerc %" | ?{$_."Free(%)" -lt 20}
Send-MailMessage -From $emailFrom -To $emailTo -Subject $Subject -Body $Subject -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.mail.com" -Port 25
}
}
get-mountpoints
这是我提出的最终解决方案。
$emailFrom = "mail@mail.com"
$emailTo = "tomail@mail.com"
function Get-Mountpoints {
param(
$ComputerName,
$FreePercentage,
$Filter ="DriveType='3'"
)
$volumes = Get-WmiObject win32_volume -Filter $Filter -ComputerName $ComputerName |
Where-Object {$_.DriverLetter -eq $null -and ([math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0)) -lt $FreePercentage}
if($volumes) {
# You might want to use the header of the body below, whatever.
$Subject = "Issue for space on server $ComputerName"
# When referencing parameter values in a string, you need to tell powershell to evaluate your string in a special way
# Wrapping things in a $() essentially is an order of operations for evaluation, the more nested, the earlier you will be evaluated
# You can also use this $() when your variable name abuts a character, like in the % below
$Body = "Hostname: $ComputerName `r`n" + [System.Environment]::NewLine # a "shortcut" for system anonymous newlines
$Body +=
# If you comment out $Volumes and replace it with the next line, you will get two rows output into $Body
$Volumes |
# @( [pscustomobject]@{Label = "C"; FreeSpace = 10;}, [pscustomobject]@{Label = "C"; FreeSpace = 10;}) |
Foreach-Object {
"Disk $($_.Label) has less than $([math]::round(($_.FreeSpace / 1073741824),2)) GB of free space left, which is $([math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) * 100),0))% `r`r"
}
# In PowerShell, these messages will be supressed unless the console runs $VerbosePreference="Continue" indicating you would like to see more verbose output
# I added the -WhatIf so you wont send anything
#$VerbosePreference="Continue"
Write-Verbose "From: $emailFrom To: $emailTo"
Write-Verbose $Subject
Write-Verbose $Body
Send-MailMessage -From $emailFrom -To $emailTo -Subject $Subject -Body $Body -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.mail.com" -Port 25
}
}
# Why not maintain a list of computers?
$ComputerList = @(
"localhost"
)
$ComputerList | ForEach-Object {
Get-Mountpoints -ComputerName $_ -Filter "DriveType='3'" -FreePercentage 20
} # I Added some parameters so you can make it a little more flexible
感谢大家的帮助
答案 0 :(得分:1)
所以我对你的问题采取了一些主要的自由,并试图达到目的,我在整个过程中添加了评论,添加了一些参数以使其更加灵活,演示了列表,字符串评估以及其他一些部分。
$emailFrom = "example@example.com"
$emailTo = "test@example.com"
function Get-Mountpoints {
param(
$ComputerName,
$FreePercentage,
$Filter ="DriveType='3'"
)
$Volumes = Get-WmiObject win32_volume -Filter $Filter -ComputerName $ComputerName |
Where-Object {$_.DriverLetter -eq $null -and $_."Free(%)" -lt $FreePercentage}
if($Volumes){
# You might want to use the header of the body below, whatever.
$Subject = "To Be Determined"
# When referencing parameter values in a string, you need to tell powershell to evaluate your string in a special way
# Wrapping things in a $() essentially is an order of operations for evaluation, the more nested, the earlier you will be evaluated
# You can also use this $() when your variable name abuts a character, like in the % below
$Body = "Hostname: $ComputerName" + [System.Environment]::NewLine # a "shortcut" for system anonymous newlines
$Body +=
# If you comment out $Volumes and replace it with the next line, you will get two rows output into $Body
$Volumes |
# @( [pscustomobject]@{Label = "C"; FreeSpace = 10;}, [pscustomobject]@{Label = "C"; FreeSpace = 10;) |
Foreach-Object {
"Disk $($_.Label) has less than $([math]::round(($_.FreeSpace + .1 / 1073741824),2)) GB of free space left, which is $([math]::round(((($_.FreeSpace + .1 / 1073741824)/($_.Capacity / 1073741824)) * 100),0))%" + [System.Environment]::NewLine
} # $_ indicates the current iterated on item
# In PowerShell, these messages will be supressed unless the console runs $VerbosePreference="Continue" indicating you would like to see more verbose output
# I added the -WhatIf so you wont send anything
Write-Verbose "From: $emailFrom To: $emailTo"
Write-Verbose $Subject
Write-Verbose $Body
#Send-MailMessage -From $emailFrom -To $emailTo -Subject $Subject -Body $Body -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.mail.com" -Port 25
}
}
# Why not maintain a list of computers?
$ComputerList = @(
"localhost"
)
$ComputerList | ForEach-Object {
Get-Mountpoints -ComputerName $_ -Filter "DriveType='5'" -FreePercentage 99
} # I Added some parameters so you can make it a little more flexible