Tuesday, June 27, 2017

Automate Folder Cleanup

Here is a script I wrote to help automate the scrubbing\cleanup of my security camera footage.

     In its current unmodified configuration, it is set to keep the security_Camera folder on my K:\ drive above 10% free space. This is accomplished by scanning the folding or drive to figure out the used space percentage, if the used space percentage is less than the configured $FreeSpaceThreshold then it will remove the oldest file, rescan, if needed and remove the oldest file again. This will continue to happen until the $PercentageFree is over the threshold.

Download the script from the Microsoft Script Repository!


Function Automated-FolderCleanup {

<# Matthew Kerfoot 6-26-17
.Synopsis
Cleans up aged files.

.DESCRIPTION
Monitors a folder and deletes the oldest files until the percent free Space reaches the configured threshold.

.EXAMPLE
PS:\>Automate-FolderCleanup -Path "K:\Security_Camera" -FreeSpaceThreshold "10"

.FUNCTIONALITY
This script was put together to scrub the oldest files from my security camera footage storage space.

.NOTES

Version : 3.0+

Author/Copyright : © Matthew Kerfoot - All Rights Reserved

Email/Blog/Twitter : mkkerfoot@gmail.com www.TheOvernightAdmin.com @mkkerfoot

Disclaimer : THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK
OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
While these scripts are tested and working in my environment, it is recommended
that you test these scripts in a test environment before using in your production environment
Matthew Kerfoot further disclaims all implied warranties including, without limitation, any
implied warranties of merchantability or of fitness for a particular purpose. The entire risk
arising out of the use or performance of this script and documentation remains with you.
In no event shall Matthew Kerfoot, its authors, or anyone else involved in the creation, production,
or delivery of this script/tool be liable for any damages whatsoever (including, without limitation,
damages for loss of business profits, business interruption, loss of business information, or other
pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation,
even if Matthew Kerfoot has been advised of the possibility of such damages.

Assumptions : ExecutionPolicy of AllSigned (recommended), RemoteSigned or Unrestricted (not recommended)

Author's notes : In its current unmodified configuration, it is set to keep the security_Camera `
folder on my K:\ drive above 10% free space. This is accomplished by scanning the folding `
or drive to figure out the used space percentage, if the used space percentage is less `
then the configured $FreeSpaceThreshold then it will remove the oldest file, rescan, `
if needed and remove the oldest file again. This will continue to happen until the `
$PercentageFree is over the threshold.
#>

    [Cmdletbinding(SupportsShouldProcess)]
        Param(
                [Parameter(Mandatory = $false,
                           ValueFromPipelineByPropertyName = $true,
                           Position = 0,
                           HelpMessage = "Please enter the full path of the folder or drive you would like to monitor and clean")]
                           [string[]]$Path = "K:\Security_Camera",
                [Parameter(Mandatory = $false,
                           ValueFromPipelineByPropertyName = $true,
                           Position = 1,
                           HelpMessage = "What free space percentage do you want to set the threshold for?")]
                           [int]$FreeSpaceThreshold = "10", # What percentage do you want the drive to free up until?
                [Parameter(Mandatory = $false,
                           ValueFromPipelineByPropertyName = $true,
                           Position = 2,
                           HelpMessage = "Please enter the full path to where you would like a transaction log stored.")]
                           $TranscriptLog = "C:\windows\Temp\Automated-FolderCleanup.log"
        )
        Begin{
                Start-Transcript -Path $TranscriptLog
                [string]$Drive = Split-Path -Qualifier $Path
                [WMI]$Disk = "Win32_LogicalDisk.DeviceID='$Drive'"
                [int]$PercentFree = $Disk.FreeSpace / $Disk.Size * 100
        }
        Process{
            while ($PercentFree -lt $FreeSpaceThreshold) {
                Get-ChildItem $Path -Recurse -File |
                    Sort-Object CreationTime |
                    Select-Object -First 1 |
                    Remove-Item
                $PercentFree = $Disk.FreeSpace / $Disk.Size * 100
            }
        }
        end{
                Write-Output "Procesing has completed"
                Stop-Transcript -Verbose
            }
} Automated-FolderCleanup -WhatIf

Simply remove the -WhatIf from the last line to allow processing if the script is run with -WhatIf it will run the script but won't actually make changes it will just give you a verbose output as to what it would do if the -WhatIf switch isn't specified.

Download the script from the Microsoft Script Repository!


Enjoy!

No comments:

Post a Comment