我已经很久没有在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
谢谢大家的帮助
发布于 2018-03-13 04:32:31
所以我对你的问题做了一些很大的尝试,试图达到这个目的,我在整个过程中添加了一些评论,加入了一些参数,以使列表、字符串评估和其他几个部分更加灵活。
$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
https://stackoverflow.com/questions/49247231
复制相似问题