Create Event Log for Low Disk Space

EventLog_LowDiskSpace

<#
##########################################################################################
– The script is used to create an event log under System log with custom source named “DiskAlert”

– The script checks for disk space on all drives for a server, and in return creates an event log under System log, source “DiskAlert”

– Low disk space threshold can be changed below as per requirement
##########################################################################################
Created by Talha Qamar
Date Created 21st May 2017
http://www.talhaqamar.com
#>
# Change the Low Disk Space Threshold in Percentage (Example: 10 for 10 %)
$threshHold = 10

$serverName = $env:COMPUTERNAME
$pingServer = “select * from win32_pingstatus where address = ‘$serverName'”
$pingResult = Get-WmiObject -query $pingServer
if ($pingResult.protocoladdress) {

# Get the Disks for this computer
$getDisks = get-wmiobject Win32_LogicalDisk -computername $serverName -Filter “DriveType = 3”

# For each disk calculate the free space
foreach ($disk in $getDisks) {
if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))}
else {$PercentFree = 0}

$Drive = $disk.DeviceID
“$serverName – $Drive – $PercentFree”

# Create an eventlog for any drives found to be less than the specified threshold
if ($PercentFree -le $threshHold) {
$chkEventSource = Get-Eventlog -LogName system | Where-Object {$_.Source -eq “DiskAlert”}
if (!$chkEventSource)
{
New-EventLog –LogName System –Source “DiskAlert”
}
if ($chkEventSource)
{
Write-EventLog –LogName System –Source “DiskAlert” –EntryType Warning –EventID 1 -Message “The disk space on server $serverName, drive $Drive is less than $threshHold percent. The current free space on $Drive is $PercentFree percent.”
}

}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s