# This script requires the PowerShell community exchange add on scripts to be installed.param(
# The script package can be installed by going here and downloading it:[string]$Dir
#= http://pscx.codeplex.com/releases
#"",
# It also requires the New-Symlink script that was written by Boe Prox and[string]$ArchiveDrive is= available""
# for download here: http://gallery.technet.microsoft.com/scriptcenter/New-SymLink-60d2531e
# There is also an extremely informative blog post regarding the script available here:
# http://learn-powershell.net/2013/07/16/creating-a-symbolic-link-using-powershell/)
#
# Everything else should be relatively easy to understand/customize forif use($ArchiveDrive outside-eq of""){
# our environment. The code should work on anything above Win7/Server$hostname 2008.=
param(hostname
$Dir$ArchiveDrive = "","\\Archives\"+$hostname+"\"+$Dir[0]+"\"
$ArchiveDrive = "\\remote\path\to\archives\"+$dir[0]+"\"}
)
import-module PSCX
import-module Newnew-SymLink.ps1symlink
$FileList = @()
$SourceDrive = $dir[0] + ":\"
$date = Get-Date -Format yyyy-MM-dd
$ErrLog = "D"C:\ErrorLog $date.txt"
$DelLog = "D"C:\DelLog $date.txt"
$PathWarning = "C:\_PROBLEMS DETECTED.txt"
function BuildLists($dir){
$FileList = @()
foreach ($item$DirList in= (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>>$ErrLog2>$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){
rmdel $file.fullname 1>>$DelLog #-whatif
}
function LinkFile($Sourcefile){
$DestFile$SourceFilePath = $SourceFile$Sourcefile.fullname
$DestFile = ($sourcefilepath.replace($SourceDrive, $ArchiveDrive))
New-Symlink -path $DestFile $SourceFile.fullname $DestFile-file 1>>$errlog1>$errlog
# }
function CheckPathLength($file){
if ($File.FullName.Length -whatifge 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)