Last active
March 6, 2020 16:50
-
-
Save Agazoth/e0bf8330936a6aa38d4a80475b2a0494 to your computer and use it in GitHub Desktop.
Generates a usage report on a given folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script generates a report containing the nessesary information about Size, LastChangeDate, LastReadDate on any given share | |
# The script needs to run locally on the server to optimize speed | |
param ([System.IO.DirectoryInfo]$Path, [switch]$SkipUnicode) | |
if (!$Path.Exists){ | |
Write-Warning "$($path.FullName) not found!" | |
break | |
} | |
#Add Unicode switch | |
if (!$SkipUnicode){ | |
$DirPath = '\\?\' + $Path.FullName | |
} else { | |
$DirPath = $Path.FullName | |
} | |
# We need to measure, when tha data was last accessed | |
# Last access time will be split into pools with an interval of days | |
$Date = Get-Date | |
$Paths = Get-ChildItem -Path $DirPath -Recurse -Force | where {!$_.Psiscontainer} | |
foreach ($f in $Paths ){ | |
$LastAccessTime = Get-Date $f.LastAccessTime | |
$LastAccessdDate = ($Date - $LastAccessTime).Days | |
$AccessGroup = switch ($LastAccessdDate) { | |
{$PSItem -lt 14} {'1 - 14 days'} | |
{$PSItem -ge 14 -and $PSItem -lt 30} {'2 - 30 days'} | |
{$PSItem -ge 30 -and $PSItem -lt 45} {'3 - 45 days'} | |
{$PSItem -ge 45 -and $PSItem -lt 60} {'4 - 60 days'} | |
{$PSItem -ge 60 -and $PSItem -lt 90} {'5 - 90 days'} | |
{$PSItem -ge 90 -and $PSItem -lt 180} {'6 - 180 days'} | |
{$PSItem -ge 180 -and $PSItem -lt 365} {'7 - 365 days'} | |
{$PSItem -ge 365 -and $PSItem -lt 730} {'8 - 1-2 years'} | |
{$PSItem -ge 730} {'9 - More then 2 years'} | |
} | |
Add-Member -InputObject $f -NotePropertyName AccessGroup -NotePropertyValue $AccessGroup | |
} | |
$MeasureTotalSize = $Paths | Measure-Object Length -sum | |
$GBc = 0 | |
$Attributes = foreach ($g in ($Paths | Group-Object AccessGroup | Sort-Object Name)){ | |
$Size = $g.Group | Measure-Object Length -sum | |
$GBc += $Size.Sum | |
$Percent = [math]::Round(($GBc/$MeasureTotalSize.Sum)*100,2) | |
[pscustomObject]@{ | |
LastUsedGroup = $g.name | |
FileCount = $g.Count | |
SizeInGB = [math]::Round($Size.Sum/1GB, 4) | |
Percentage = $Percent | |
} | |
} | |
$HeaderData = [PSCustomObject]@{ | |
Server = $env:COMPUTERNAME | |
RootPath = $path.FullName | |
FileCount = $MeasureTotalSize.Count | |
TotalSizeInGB = [math]::Round($MeasureTotalSize.Sum/1GB, 2) | |
} | |
$Folder = $path.name | |
if ($Folder -match ':') {$Folder = ($folder -split ':')[0}]} | |
$UsageReport = '{0}\{1}-Usagereport.txt' -f $(Get-Location).Path, $Folder | |
$FileReport = '{0}\{1}-Filereport.clixml' -f $(Get-Location).Path, $Folder | |
'Start: {0}' -f $date | Out-File $UsageReport | |
$HeaderData | Out-File $UsageReport -Append | |
$Attributes | Out-File $UsageReport -Append | |
$Paths | Select-Object Name, Directory, LastWriteTime, LastAccessTime, Length, Extension, AccessGroup | export-clixml $FileReport |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment