PSotD: Emailing Users When Quota Warning Met

If you have file servers, you most likely run user quotas. Not running quotas would allow users to quickly fill up the entire drive.

I didn’t really like the reporting features on the server so with a bit of PowerShell – I extended it:

Here is the script:

========================================================================

Add-PSSnapin Quest.ActiveRoles.ADManagement

$Users = Get-EventLog -ComputerName SERVERNAME -LogName System -Source ntfs | where {$_.eventID -eq 36} |  Select-Object Username -Unique | Sort-Object Username

$Directory=”DIRECTORY”

$emailFrom = “SERVICE ACCOUNT”

$subject = “Your Largest Files”

$smtpServer = “MAILSERVER”

 

Foreach ($User in $Users) {

$emailto = get-qaduser $Name | Select-Object email | out-string

$Name = $User.UserName.TrimStart(“DOMAIN\”)

$body = “You are receiving this email because your U drive is nearly full. The attached document ($Name.Txt) lists your 25 largest files. If these files are not needed, please delete them and empty your Recycle Bin. For additional ways to regain space, please see the attached PowerPoint. <enter> ”

Get-ChildItem $Directory -recurse -file | ? {$($_ | Get-Acl).Owner -match $name } |

Sort-Object length -Descending |

Select-Object Directory,Name -first 25 |

format-table -AutoSize |

Out-String -Width 4096 |

Out-file “.\$name.txt”

Send-MailMessage -to $emailTo -from $emailFrom -Subject $subject -Body $body -Attachments “.\$name.txt”, “.\Managing Your Files.pptx” -SmtpServer $smtpServer

 }

=======================================================================

The end result is that any user that has hit their quota warning will get an automated email listing their top 25 files (sorted by size)!! Pretty nifty!

Share