Archive old Files/Folders to new server

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

August 20th, 2015 3:19am

Your usage von $newpath is not correct because "$_" is not available at that moment.
http://stackoverflow.com/questions/3494115/what-does-mean-in-powershell

You have to use $newpath like this:

Get-Childitem -Path $path -recurse| Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | ForEach { $filename = $_.fullname $newpath=$archpath + $_.DirectoryName.Replace($path,"") New-Item $newpath -type directory -ErrorAction SilentlyContinue Move-Item $_.FullName -destination $newpath -force "Successfully moved $filename to $newpath" | add-content $logpath\log-$date.txt }


  • Edited by KranzerM 23 hours 23 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 3:47am

Hey,

Thanks for the reply however it still gives an error message about the destination being a null value.

August 20th, 2015 5:44am

Are you sure you saved the file? Because the errormessage and your code does not fit together.
  • Edited by KranzerM 19 hours 51 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 7:16am

Your usage von $newpath is not correct because "$_" is not available at that moment.
http://stackoverflow.com/questions/3494115/what-does-mean-in-powershell

You have to use $newpath like this:

Get-Childitem -Path $path -recurse| Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$days)} | ForEach { $filename = $_.fullname $newpath=$archpath + $_.DirectoryName.Replace($path,"") New-Item $newpath -type directory -ErrorAction SilentlyContinue Move-Item $_.FullName -destination $newpath -force "Successfully moved $filename to $newpath" | add-content $logpath\log-$date.txt }


  • Edited by KranzerM Thursday, August 20, 2015 7:47 AM
August 20th, 2015 7:45am

hi,

You are correct, however i still get the below message:



Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 8:29am

Please post the actual error message instead of pictures.

August 20th, 2015 8:30am

You cannot call a method on a null-valued expression.
At D:\DB\Powershell\DFS\Archive.ps1:16 char:5
+     $newpath=$archpath + $_.DirectoryName.Replace($path,"")
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
Move-Item : Could not find a part of the path.
At D:\DB\Powershell\DFS\Archive.ps1:18 char:11
+           Move-Item $_.FullName -destination $newpath -force
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (BT INTERNET CAN... ID 1394836.msg:FileInfo) [Move-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : MoveFileInfoItemIOError,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:14 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
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 8:42am

Skip the script and just use robocopy:

http://ss64.com/nt/robocopy.html

Hitting the character limit is a pain to work around.

August 20th, 2015 8:43am

Have you tried to debug the script (https://technet.microsoft.com/en-us/library/dd819480.aspx?f=255&MSPPError=-2147217396)

Setting breakpoint to line 16 (F9)
Start Script (F5)
Put the "$_" out.

....


Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 8:54am

Something like this?

robocopy \\Source \\Destination /E /COPY:DATS /R:5 /W:10 /zb /MINAGE:365 /LOG:\\LOG.TXT

August 20th, 2015 8:55am

Are you sure you saved the file? Because the errormessage and your code does not fit together.
  • Edited by KranzerM Thursday, August 20, 2015 11:20 AM
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 11:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics