Hi,
I am trying to archive off all files older than 1 year from our primary File server to an archive file server. I want to move any files older than 365 days to the archive server, however I could only seem to get my script to move the files and it would not maintain the folder structure. I found a thread here which showed how to maintain the folder structure but whenever i try to incorporate this into my script it seems to error:
$Path is a local server and $archpath is a mapped drive to a destination server.
$path = "D:\Group\IT"
$archpath = "A:\IT"
$days = "365"
$logpath = "D:\DB\Reports\Archive_Logs\IT"
$date = Get-Date -format yyyyMMdd
$newpath = $archpath + $_.DirectoryName.Replace($path,"")
write-progress -activity "Archiving Data" -status "Progress:"
If ( -not (Test-Path $archpath)) {ni $archpath -type directory}
Get-Childitem -Path $path -recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} |
ForEach { $filename = $_.fullname
New-Item $newpath -type directory -ErrorAction SilentlyContinue
Move-Item $_.FullName -destination $newpath -force
"Successfully moved $filename to $newpath" | add-content $logpath\log-$date.txt
}
I get this error below:
You cannot call a method on a null-valued expression.At D:\DB\Powershell\DFS\Archive.ps1:10 char:1
+ $newpath = $archpath + $_.DirectoryName.Replace($path,"")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
New-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:13
+ New-Item $newpath -type directory -ErrorAction SilentlyContinue
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
Move-Item : Cannot process argument because the value of argument "destination" is null. Change the value of argument "destination" to a non-null value.
At D:\DB\Powershell\DFS\Archive.ps1:19 char:11
+ Move-Item $_.FullName -destination $newpath -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Move-Item], PSArgumentNullException
+ FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.MoveItemCommand
Get-Childitem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At D:\DB\Powershell\DFS\Archive.ps1:16 char:1
+ Get-Childitem -Path $path -recurse | Where-Object {$_.LastWriteTime -lt (get-dat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (D:\Group\IT\A-I...N FEE - MUAM\BT:String) [Get-ChildItem], PathTooLongException
+ FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand
New-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:13
+ New-Item $newpath -type directory -ErrorAction SilentlyContinue
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand
New-Item : Cannot bind argument to parameter 'Path' because it is null.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:13
+ New-Item $newpath -type directory -ErrorAction SilentlyContinue
+ ~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.NewItemCommand