Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 616
  • Last Modified:

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.
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"
}

Open in new window

0
siber1
Asked:
siber1
  • 3
  • 3
  • 2
1 Solution
 
I QasmiCommented:
#*=============================================================================
#* 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\lowdisk.txt"
$i = 0
$users = "some.email@address.com"
$computers = import-csv "D:\Scripts\computerlistall.txt"
echo "ServerName        Drive Letter    Drive Size    Free Space    Percent Free" >> "D:\Scripts\Powershell\PAC\lowdisk.txt"
echo "----------        ------------    ----------    ----------    ------------" >> "D:\Scripts\Powershell\PAC\lowdisk.txt"
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\lowdisk.txt"
            $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($smtpServer)
 
        $emailFrom = ¿fromuser@domain.com¿
 
       $subject = ¿Email Subject¿
 
        foreach ($line in Get-Content ¿D:\Scripts\lowdisk.txt¿)
 
{
 
            $body += ¿$line `n¿
 
        }
 
        $smtp.Send($EmailFrom,$user,$subject,$body)
 
        $body = ¿¿
 
    }
 
}
0
 
I QasmiCommented:
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.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
siber1Author Commented:
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.
0
 
siber1Author Commented:
Mike do you have a way to accomplish this?
0
 
chrismerrittCommented:
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.

$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
	}
}

Open in new window

0
 
chrismerrittCommented:
Sorry, realised I left the wrong server list in there (had . to signify my local host). I added your server list .txt document back in:

$MasterArray = @()

$ServerList = Get-Content "C:\scripts\serverliste2010.txt"

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
	}
}

Open in new window

0
 
siber1Author Commented:
Chris this is brilliant. Exactly what i was looking for.
many thanks!

S.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now