Michael Leonard
asked on
need assistance with a powershell diskspace alerting script
hello, i have the following script setup to notify on low disk space, however, i need a way to set different thresholds per disk. for example, i want to alert on 20% space on the C: partition but 50% space on all other drives.
i've attached the current script. any help would be much appreciated.
thx - S.
i've attached the current script. any help would be much appreciated.
thx - S.
Get-Content c:\scripts\serverliste2010.txt | ForEach-Object {Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_ -Filter "drivetype=3"} | %{
Add-Member -InputObject $_ -MemberType noteproperty -Name freepercent -Value ($_.freespace/$_.size*100) -PassThru
} | ?{$_.freepercent -lt 50} | ForEach-Object {
$message = "Low disk space on server $($_.__server) on disk $($_.name), actual free size $([int] $_.freepercent)%"
Send-MailMessage -Body $message -From E2010-diskspacealert@mydomain.com -To alerts@mydomain.com -SmtpServer 10.x.x.x -Subject "Low disk space notification"
}
This is a script that will monitor the physical drives of any machines that you have in an input file. It will send out notifcations if the free space is below a threshhold that is defined by you.
ASKER
iQasmi, i'm not sure if you read my original question, i already have a script that monitors and alerts on diskspace, [see the code i posted]
what i am looking for is a way to modify this code to set different thresholds on a per disk basis.
for example. i want to be alerted on 20% space on the c: drive but 50% threshold on all other drives.
thanks,
S.
what i am looking for is a way to modify this code to set different thresholds on a per disk basis.
for example. i want to be alerted on 20% space on the c: drive but 50% threshold on all other drives.
thanks,
S.
ASKER
Mike do you have a way to accomplish this?
Try this, much neater :)
At the moment it just writes to host when it finds a disk under your criteria, if you're happy then you can remove the write-hosts and uncomment the Send-MailMessage lines.
$MasterArray is actually a collection of all disks at the end, it's not really needed but I prefer to work with sets of data, and it gives you the option to do something with it later.
At the moment it just writes to host when it finds a disk under your criteria, if you're happy then you can remove the write-hosts and uncomment the Send-MailMessage lines.
$MasterArray is actually a collection of all disks at the end, it's not really needed but I prefer to work with sets of data, and it gives you the option to do something with it later.
$MasterArray = @()
$ServerList = "."
foreach ($Server in $ServerList)
{
$ServerDrives = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $Server -Filter "drivetype=3"
foreach ($ServerDrive in $ServerDrives)
{
$TempArray = @()
$TempArray = "" | Select Server, DeviceID, FreeSpaceGB, SizeGB, PercentFree, VolumeName
$ServerDriveFreeSpaceGB = ($ServerDrive.FreeSpace /1GB).ToString("N2").Replace(",","")
$ServerDriveSizeGB = ($ServerDrive.Size /1GB).ToString("N2").Replace(",","")
[string]$TempArray.Server = $Server
[string]$TempArray.DeviceID = $ServerDrive.DeviceID
[decimal]$TempArray.FreeSpaceGB = $ServerDriveFreeSpaceGB
[decimal]$TempArray.SizeGB = $ServerDriveSizeGB
[decimal]$TempArray.PercentFree = ($ServerDriveFreeSpaceGB / $ServerDriveSizeGB).ToString("P2").Replace(" %","")
[string]$TempArray.VolumeName = $ServerDrive.VolumeName
if ($TempArray.PercentFree -lt 20 -and $TempArray.DeviceID -eq "C:")
{
$Message = "Low disk space on server $($TempArray.Server) on disk $($TempArray.DeviceID), actual free size $($TempArray.FreeSpaceGB) GB / $($TempArray.SizeGB) GB - $($TempArray.PercentFree) %"
write-host $Message
#Send-MailMessage -Body $Message -From E2010-diskspacealert@mydomain.com -To alerts@mydomain.com -SmtpServer 10.x.x.x -Subject "Low disk space notification"
}
if ($TempArray.PercentFree -lt 50 -and $TempArray.DeviceID -ne "C:")
{
$Message = "Low disk space on server $($TempArray.Server) on disk $($TempArray.DeviceID), actual free size $($TempArray.FreeSpaceGB) GB / $($TempArray.SizeGB) GB - $($TempArray.PercentFree) %"
write-host $Message
#Send-MailMessage -Body $Message -From E2010-diskspacealert@mydomain.com -To alerts@mydomain.com -SmtpServer 10.x.x.x -Subject "Low disk space notification"
}
$MasterArray += $TempArray
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Chris this is brilliant. Exactly what i was looking for.
many thanks!
S.
many thanks!
S.
#* Script Name: Diskmonitor.ps1
#* Created: 02-25-2009
#* Author: Colin Smiht
#* Company:
#* Email:
#* Web: http://sysadminsmith.com
#*========================
#* Purpose: This script will gather disk information about all the servers listed in your computerlistall file. This is a csv file.
#* It will also send out notifications via email to anyone you want based on low disk parameters that you provide
#*
#*========================
##########################
### Gather Disk Information ###
##########################
Clear-Content "D:\Scripts\Powershell\PAC
$i = 0
$users = "some.email@address.com"
$computers = import-csv "D:\Scripts\computerlistal
echo "ServerName Drive Letter Drive Size Free Space Percent Free" >> "D:\Scripts\Powershell\PAC
echo "---------- ------------ ---------- ---------- ------------" >> "D:\Scripts\Powershell\PAC
foreach ($line in $computers)
{
$computer = $line.hostname
$ip = $line.ip
$computer
$ip
}
$drives = Get-WmiObject -ComputerName $computer Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}
foreach($drive in $drives)
{
$size1 = $drive.size / 1GB
$size1
$size = "{0:N2}" -f $size1
$size
$free1 = $drive.freespace / 1GB
$free = "{0:N2}" -f $free1
$ID = $drive.DeviceID
$a = $free1 / $size1 * 100
$b = "{0:N2}" -f $a
##########################
## Determine if any disks low ##
##########################
if (($ID -eq "D:") -or ($ID -eq "S:") -or ($ID -eq "T:") -or ($ID -eq "C:") -and ($free1 -lt 1))
{
echo "$computer $ID $size $free $b" >> "D:\Scripts\Powershell\PAC
$i++
#[char]10 | Out-File -append ./low.txt
}
}
##########################
## Send Notification if alert $i is greater then 0 ##
##########################
if ($i -gt 0)
{
foreach ($user in $users)
{
echo ¿Sending Email notification ro $user¿
$smtpServer = ¿smtp server¿
$smtp = New-Object Net.Mail.SmtpClient($smtpS
$emailFrom = ¿fromuser@domain.com¿
$subject = ¿Email Subject¿
foreach ($line in Get-Content ¿D:\Scripts\lowdisk.txt¿)
{
$body += ¿$line `n¿
}
$smtp.Send($EmailFrom,$use
$body = ¿¿
}
}