I wrote this code to clean up some of the space on our file server. We've got 15 years of legacy data that nobody accesses or changes or cares about that we have to keep regardless. I'd rather have it not sitting on our main file server so that I don't have to add an additional TB to it annually. This script goes through the file structure, finds files that haven't been touched in 4 years and copies them to slower storage, then replaces the source file with a symlink.
I'm new to PowerShell, so any pointers regarding style or a better way to do things would be nice. I know that my way of bracing things is wrong and that closing curly braces should be indented one less level, but I don't care and find this easier to deal with.
param(
[string]$Dir = "",
[string]$ArchiveDrive = ""
)
if ($ArchiveDrive -eq ""){
$hostname = hostname
$ArchiveDrive = "\\Archives\"+$hostname+"\"+$Dir[0]+"\"
}
import-module PSCX
import-module new-symlink
$FileList = @()
$SourceDrive = $dir[0] + ":\"
$date = Get-Date -Format yyyy-MM-dd
$ErrLog = "C:\ErrorLog $date.txt"
$DelLog = "C:\DelLog $date.txt"
$PathWarning = "C:\_PROBLEMS DETECTED.txt"
function BuildLists($dir){
$FileList = @()
$DirList = (dir $dir -recurse)
foreach ($item in $DirList){
if ( ((get-date).Subtract($item.LastWriteTime).Days-gt 1460) -eq $True) {
$FileList += $item
}
#else {write-host "$item is modified recently"}
}
return $FileList
}
function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName}
}
function ArchiveFile($SourceFile){
$DestFile = ($SourceFile.fullname.replace($SourceDrive, $ArchiveDrive))
$DestDir = ($SourceFile.DirectoryName.replace($SourceDrive, $ArchiveDrive))
mkdir -Path $DestDir 2>$ErrLog
copy $SourceFile.FullName $DestFile
}
function HashCheckFile($SourceFile){
$DestFile = $SourceFile.FullName.replace($SourceDrive, $ArchiveDrive)
$SourceHash = get-hash($SourceFile.fullname)
$DestHash = get-hash("$DestFile")
return $SourceHash.HashString -eq $DestHash.HashString
}
function DeleteFIle($File){
del $file.fullname
}
function LinkFile($Sourcefile){
$SourceFilePath = $Sourcefile.fullname
$DestFile = ($sourcefilepath.replace($SourceDrive, $ArchiveDrive))
New-Symlink -path $DestFile $SourceFile.fullname -file 1>$errlog
}
function CheckPathLength($file){
if ($File.FullName.Length -ge 220){
copy $PathWarning $File.DirectoryName}
}
function ReplicateFile($file){
if ($file.Attributes -eq "Directory"){continue}
ArchiveFile($File)
if (HashCheckFile($File)){
DeleteFile($File)
LinkFile($File)
}
}
function Archive($FileList){
foreach ($File in $FileList){
CheckPathLength($file)
ReplicateFile($File)
}
}
function RunArchiving($dir){
$FileList = BuildLists($dir)
Archive($FileList)
}
function UnArchive(){}
RunArchiving($dir)
fsutil behavior set SymlinkEvaluation r2r:1
and verified by doingfsutil behavior query SymlinkEvaluation
at an admin prompt, or via GPO (Computer Config>Policies>Admin Templates>System>FileSystem>Selectively allow the evaluation of a symbolic link). \$\endgroup\$