Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / PowerShell
Tip/Trick

PowerShell file size scanner

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Mar 2013CPOL 10.5K   2  
Code sample to monitor file size on server.

Introduction

This article describes how to monitor the file size on a server with PowerShell.

Background

First we need to get the folder size recursively, then store it in a log file. After some time, we scan again and compare the two log files.

Using the code 

$workingFolder = $args[0] 
#chemin du fichier a scanner
$startFolder = "C:\"
#ecart de taille a surveiller en MB(Mo)
$sizeDiff = 10

#smtp
$smtp = "localhost"
#destinataire
$to = "to@example.com"
#emmeteur
$from = "file_scan@example.com"


$date = Get-Date -format "yyyy-MM-dd H:m:s"
if (Test-Path  ("$workingFolder\1.log"))
{
    if (Test-Path  ("$workingFolder\2.log")){ Remove-Item "$workingFolder\2.log" }
    Rename-Item "$workingFolder\1.log" "$workingFolder\2.log"
}

"Date;Path;Size">> "$workingFolder \1.log"
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$date;$startFolder;" + "{0:N0}" -f ($colItems.sum / 1MB) >> "$workingFolder \1.log"

$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
{
    Try {        
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum -ErrorAction SilentlyContinue)
        "$date;" + $i.FullName + ";" + "{0:N0}" -f ($subFolderItems.sum / 1MB) >> "$workingFolder\1.log"
    }
    Catch [system.exception]
    {        
         " exception " + $i.FullName
    }
}

if (Test-Path  ("$workingFolder\2.log"))
{
    $csv_new = import-csv "$workingFolder\1.log" -delimiter ";"
    $csv_old = import-csv "$workingFolder\2.log" -delimiter ";"

    foreach($new in $csv_new)    {
        $diff += $csv_old | where {$_.Path -eq $new.Path -and $_.Size + 
           $diffSize -lt $new.Size -and $_.Size -gt 0} | 
           %{ $_.Date + " : " + $new.Path +" old size : " + 
              $_.Size + " new size : " + $new.Size + "`r`n" }
    }
    
    if($diff.length -gt 0)
    {
        "Sending mail"
        send-mailmessage -to $to -from $from -subject "File size report" -SmtpServer $smtp -body $diff
    }
} 

How does it work?

It works as a scheduled task and logs the folder size (startFolder and subdirectory). If between two scans, the folder size has grown more than diffSize, it sends a mail alert. 

The batch code is:

powershell %~dp0\file_scanner.ps1 %~dp0 

Points of Interest 

This code shows how powerful PowerShell is, and it follows the KISS philosophy. Some parts of this code are from Technet (http://technet.microsoft.com/en-us/library/ff730945.aspx). 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
Just studying everytime !

Comments and Discussions

 
-- There are no messages in this forum --