Disk Usage Warning Email

This script will send a disk usage warning email to the configured email address, once free space threshold met  and also it will move those files to another location/computer/server when Delete Threshold met so the space can be created at source location, need to configure it with computer schedule tasks to run at specified time.

#——————————————–
# Application Settings
#——————————————–

$FreeSpaceThreshold_Percent = 70
$DeleteThreshold_Percent = 75
$TXTLocation = “c:\temp1”
$TXTFileName = “SizeCheck.txt”
$DaysToKeep = 500
$FolderOnCDrive = “c:\temp”
$FolderOnServer = “c:\temp1”

#——————————————–
# SMTP Server Settings
#——————————————–

# $SMTP = SMTP Server Name or IP Address.
# $To = Email Id to whom Email need to be send.
# $From = From email id.
# $Subject = Subject Line.
# $Body = Email Text.

$SMTP = “Exchange-Server”
$To = “info@winteltools.com”
$From = “Generic Email ID”
$Password = “Generic Password”
$SMTPPort = 25

$FilesDeleted_Subject = “Files moved to Network Location”
$Warning_Subject = “Warning Server Drive Space”

$FilesDeleted_BodyText = “This is to Notify”
$Warning_BodyText = “FYI”

#——————————————–

$Drive = gwmi win32_logicaldisk | where-object {($_.drivetype -eq “3”) -and ($_.deviceid -eq “C:”)}
$DriveSize = “{0:N2}” -f ($Drive.size/1GB)
$Freespace = “{0:N2}” -f ($Drive.FreeSpace/1GB)
$FreespacePercent = [int] (($Drive.FreeSpace * 100) / $Drive.size)

$DriveInfo = “Drive Size is $DriveSize GB `r`n”
$DriveInfo += “Free Space is $Freespace GB `r`n”
$DriveInfo += “$FreespacePercent% `r`n”

$FilesMoved = “File Name `t`t Modified Date `r`n”
$FilesMoved += “__________________________________________________`r`n”
if ($FreespacePercent -lt $DeleteThreshold_Percent)
{
try {
$AllFiles = Get-ChildItem $FolderOnCDrive
foreach ($file in $Allfiles)
{
$ModifiedDate = Get-Date $file.lastwritetime
$TodayDate = Get-Date
$TS = New-TimeSpan -Start $ModifiedDate -End $TodayDate
if ($TS.days -gt $DaysToKeep )
{
$Source = $FolderOnCDrive + “\” + $file
$Destination = $FolderOnServer + “\” + $file
$job = Start-Job {Copy-Item $args[0] $args[1] -ErrorAction stop } -ArgumentList $Source,$Destination
$Ret = Wait-Job $job
#Remove-Item $Source
$FilesMoved += “$file `t`t $ModifiedDate `r`n”
}
}
} catch { $CopyError += $_.Exception.Message }
#send-MailMessage -SmtpServer $SMTP -To $To -From $From -Subject $Subject -Body $Body -BodyAsHtml -Priority high
}
elseif ($FreespacePercent -lt $FreeSpaceThreshold_Percent)
{

#Send Warning Email
$Warning_Subject += ” $FreespacePercent%”
$Warning_BodyText += $DriveInfo
#send-MailMessage -SmtpServer $SMTP -To $To -From $From -Subject $Warning_Subject -Body $Warning_BodyText -BodyAsHtml -Priority high -credential (new-object System.Net.NetworkCredential(“user”,”pass”,”domain”))
}
$FileLoc = $TXTLocation + “\” + $TXTFileName
$FilesMoved | Out-File $FileLoc

Leave a Reply