Files
management-scripts/Cleanup-RevitTempFilesOnFileServerCalcOnly.ps1

79 lines
3.1 KiB
PowerShell

# $CleanupPath must be set by RMM
# $DaysWithNoModification must be set by RMM
# Exit if the source path does not exist
If ( !(Test-Path $CleanupPath) ) { Write-Output "The specified path does not exist: ""${CleanupPath}""" ; Return }
# Running total of the size of all directories and files
$TotalSize = 0
# Running total of the number of all identified directories
$TotalDirectories = 0
# Running total of the number of all identified files
$TotalFiles = 0
$StartDate = Get-Date
$StartTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Output '-------------------------------------------------------------------------------'
Write-Output "Started at : ${StartTime}"
Write-Output "Run on : ${Env:COMPUTERNAME}"
Write-Output "Source : ${CleanupPath}"
If ( $DaysWithNoModification -gt 0 ) { Write-Output "Modified : ${DaysWithNoModification} day(s) ago" }
Write-Output '-------------------------------------------------------------------------------'
# Identify all files and folders that match the criteria for what we're looking for
$TEMPORARY_REVIT_OBJECTS = Get-ChildItem -Path $CleanupPath -Recurse | Where-Object {
(
# Revit model backup folder
$_.Name -match '_backup$' -or
# Revit backup models
$_.Name -match '\.\d\d\d\d\.rvt$'
# Files and folders not recently modified
) -and ($_.LastWriteTime -lt $StartDate.AddDays(-$DaysWithNoModification))
}
# Loop thru each path to get its size and
Foreach ( $obj in $TEMPORARY_REVIT_OBJECTS ) {
# Skip the object if it no longer exists or is otherwise inaccessible
If ( !(Test-Path $obj.FullName) ) { Continue } Else { $item = $obj.FullName }
Switch ( $obj.PSIsContainer ) {
# Get the size of the directory and update counter
$true {
$size = (Get-ChildItem -Path $item -Recurse | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
$TotalDirectories++
}
# Get the size of the file and update counter
$false {
$size = (Get-Item $item | Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
$TotalFiles++
}
}
Write-Output $item
$TotalSize += $size
}
# Record stats
$EndTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$Duration = "{0:g}" -f (New-TimeSpan -Start $StartTime -End $EndTime)
Switch ( $TotalSize ) {
{ $_ -ge 1TB } { $GrandTotal = "{0}TB" -f ([Math]::Round(($TotalSize / 1TB),2)) ; break }
{ $_ -ge 1GB } { $GrandTotal = "{0}GB" -f ([Math]::Round(($TotalSize / 1GB),2)) ; break }
{ $_ -ge 1MB } { $GrandTotal = "{0}MB" -f ([Math]::Round(($TotalSize / 1MB),2)) ; break }
{ $_ -ge 1KB } { $GrandTotal = "{0}KB" -f ([Math]::Round(($TotalSize / 1KB),2)) ; break }
{ $_ -lt 1KB } { $GrandTotal = "${TotalSize} bytes" ; break }
}
Write-Output '-------------------------------------------------------------------------------'
Write-Output "Finished at: ${EndTime}"
Write-Output "Duration : ${Duration}"
Write-Output "Directories: ${TotalDirectories}"
Write-Output "Files : ${TotalFiles}"
Write-Output "Total size : ${GrandTotal}"
Write-Output '-------------------------------------------------------------------------------'