I found the below powershell script on "Hey, Scripting Guy! Blog" it is a great arching script. I modified it to meet my needs of a log file being created and sending an email. However i discovered that the move-file command is to slow and wanted to use robocopy instead. I used robocoopy but i keep receiving "ERROR 123 (0x0000007B) Accessing Source Directory C:\shared\test\New Microsoft Visio Drawing.vsd\" I am not sure what can be causing this and was hoping that someone can help.
[string] $server = 'Test'
[string] $source = "c:\shared\test"
[string] $target = "C:\shared\test1"
[int] $days = 60
$logdate = Get-Date -Format MM-dd-yy
[string] $log = ('c:\shared\logs\testlog-'+ $logdate + '.log')
# Object created for shortcut creation
$wsh = new-object -comobject wscript.shell
# Get all the files from the source path, that are not shortcuts and older than the days set
Get-ChildItem $source -Recurse |
Where-Object {!$_.psiscontainer -and ((get-date) - $_.lastwritetime).totaldays -gt $days -and $_.extension -ne ".lnk"} |
ForEach-Object {
# For each file build the destination path
$dest = $_.fullname -replace ([regex]::escape($source)), $target
# Check if the destination file path has the parent directory, if not create it
$parent = split-path $dest
if(!(test-path $parent)){
[void] (new-item -Path (split-path $parent) -Name (split-path $parent -leaf) -ItemType directory)
}
# Save the modification date and the ACL of the file for later use
$date = $_.lastwritetime
$acl = $_ | Get-Acl
# Try to move the file into the destination
Robocopy.exe $_.fullname "$dest" /mov /e /zb /r:1 /copyall /nfl /np /LOG+:"$log"
#Move-Item -Path $_.fullname -Destination $dest -Verbose -ErrorAction silentlycontinue *>&1 | Out-File -FilePath $log -Append
# If successful create shortcut
if($?){
$shortCut = $wsh.CreateShortCut("$($_.fullname).lnk")
$shortCut.TargetPath = $dest
$shortCut.Save()
# Set the "date modified" property of the shortcut same as date modified property of the original file
(Get-Item "$($_.fullname).lnk").lastwritetime = $date
# Replace the access control entries on the shortcut, so that users have read only access to it
$acl.SetAccessRuleProtection($true,$true)
$acl | Set-Acl -Path "$($_.fullname).lnk"
$acl = Get-Item "$($_.fullname).lnk" | Get-Acl
$acl.Access | where-object {"BUILTIN\Administrators" -ne $_.identityreference -and "NT AUTHORITY\SYSTEM" -ne $_.identityreference} |
ForEach-Object {
$identity = $_.identityreference
[void] $acl.RemoveAccessRule($_)
$restrictedACE = New-Object system.security.AccessControl.FileSystemAccessRule($identity,"ReadAndExecute",,,"Allow")
$acl.AddAccessRule($restrictedACE)
}
$acl | Set-Acl
}
# Else write error message
else { write-host "Error moving $_" -ForegroundColor red}
}
#Mail Server Variables
$SMTPserver = "test.abc.com"
$from = "archivetest@abc.com"
$to = "test@abc.com"
$subject = $server + " Offline Data Archive Report " + $logdate
# Send email
Send-MailMessage -From $from -To $to -SmtpServer $SMTPserver -Subject $subject -Body ('Attached is the log file for files Archived on ' + $server + ' on ' + $logdate ) -Attachments ( $log)
There are a few things i can suggest.
$_.fullname
is not.User contributions licensed under CC BY-SA 3.0