How can i create a low disk space alert on Windows Server 2012 R2 Datacenter

I have a Windows Server 2012 R2 Datacenter virtual machine running in Azure.

Recently, one of my disks ran out of space.

How can i setup a notification system to alert me when one of the disk drives runs low on disk space?

The only thing that comes to mind is to write a powershell script, but i'm wondering if there is a better way to do this, e.g via something on the Server Manager/Dashboard of Windows Server? I can't see any way to do this via Azure either.

Thanks!


This right here will do ya:

Event ID 2013 (Disk Is At Or Near Capacity) not getting logged

To summarize, make sure you're logging disk space alerts in your event log:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters]
"DiskSpaceThreshold"=dword:0000000a
"LowDiskSpaceMinimum"=dword:00000000

(You must add both of those, not just one or the other.)

Then you must attach a task to the event. And here is some XML that you can import into task scheduler that will attach said task to said event:

<?xml version="1.0" encoding="UTF-8"?>
<Task xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task" version="1.3">
   <RegistrationInfo>
      <Date>2013-02-05T14:37:17.165247</Date>
      <Author>[YourDomain]\[YourUserName]</Author>
      <Description>Send an emailed warning when a low disk space event is recorded.</Description>
   </RegistrationInfo>
   <Triggers>
      <EventTrigger>
         <Enabled>true</Enabled>
         <Subscription>&amp;lt;QueryList&amp;gt;&amp;lt;Query Id="0" Path="System"&amp;gt;&amp;lt;Select Path="System"&amp;gt;*[System[Provider[@Name='srv'] and EventID=2013]]&amp;lt;/Select&amp;gt;&amp;lt;/Query&amp;gt;&amp;lt;/QueryList&amp;gt;</Subscription>
      </EventTrigger>
   </Triggers>
   <Principals>
      <Principal id="Author">
         <UserId>S-1-5-20</UserId>
         <RunLevel>LeastPrivilege</RunLevel>
      </Principal>
   </Principals>
   <Settings>
      <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
      <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
      <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
      <AllowHardTerminate>true</AllowHardTerminate>
      <StartWhenAvailable>true</StartWhenAvailable>
      <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
      <IdleSettings>
         <StopOnIdleEnd>true</StopOnIdleEnd>
         <RestartOnIdle>false</RestartOnIdle>
      </IdleSettings>
      <AllowStartOnDemand>true</AllowStartOnDemand>
      <Enabled>true</Enabled>
      <Hidden>false</Hidden>
      <RunOnlyIfIdle>false</RunOnlyIfIdle>
      <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
      <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
      <WakeToRun>false</WakeToRun>
      <ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
      <Priority>7</Priority>
   </Settings>
   <Actions Context="Author">
      <SendEmail>
         <Server>smtpServer.YourCompany.co.uk</Server>
         <Subject>Low disk space warning on server: [ServerName]</Subject>
         <To>[email protected]</To>
         <From>[email protected]</From>
         <Body>Disk space is running low on server: [ServerName] - please investigate.</Body>
         <HeaderFields />
         <Attachments />
      </SendEmail>
   </Actions>
</Task>

You need to edit the SMTP server and other variables to suit your needs. This will fire off an email to you whenever that event (low disk space) occurs on your server.

It's worth noting that Server 2008, 2012, etc., should automatically default to generating this alert at 10% so there's probably no need to modify the registry unless you want something custom instead of 10%.


Alternatively, you can do it with Powershell.

$Threshold = 10 #Percent

Foreach($Disk In Get-CimInstance Win32_LogicalDisk | Where DriveType -EQ 3)
{
    $PercentFree = [Math]::Round(($Disk.FreeSpace / $Disk.Size) * 100, 1)
    If ($PercentFree -LT $Threshold)
    {
        Send-MailMessage -From $From -To $To -Subject "Low Disk Space on $Servername" -Body "Low Disk Space on $Servername" -SmtpServer $SMTPServer
    }
}

Schedule that and run it at an interval. (I just mocked that up off the top of my head but you get the idea.)