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

}
}
}

Advertisement

One comment

  1. I found this script recently – thanks for posting it! Just have one question/comment about the statement:
    if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))}
    else {$PercentFree = 0}
    For my situation, this wasn’t quite what I wanted. I changed the line to be:
    if ($disk.size -gt 0) {$PercentFree = [Math]::round((($disk.freespace/$disk.size) * 100))}
    else {$PercentFree = 999}
    My logic being that if a drive has disk space greater than 0, then you do want to calculate percentage free. But, if the drive has 0 disk space, setting the percent free to 0 automatically caused an event to be raised later in the script. By setting the percent free to 999 (or any arbitrary value greater than the $threshHold value) it results in the script not raising an event for disks that have no space.

    The reason why I needed to do this is that all the users in our org have Google Drive File Stream installed, and the Windows drive that is mapped by that software reports to WMI that the drive’s total space is zero. I don’t want the script to raise an error on those drives. So far the modification is working well for my needs. Hope this tip might be useful to some.

    Thanks again!

    Like

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 )

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