Powershell 归档脚本

Powershell 归档脚本

我在“嘿,脚本小子!博客”上找到了下面的 powershell 脚本,这是一个很棒的 arching 脚本。我修改了它以满足我创建日志文件和发送电子邮件的需求。但是我发现 move-file 命令太慢了,我想改用 robocopy。我使用了 robocoopy,但我一直收到“ERROR 123 (0x0000007B) 访问源目录 C:\shared\test\New Microsoft Visio Drawing.vsd\”我不确定是什么原因造成的,希望有人能帮忙。

 [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 = "[email protected]"
$to = "[email protected]"
$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)

答案1

我可以提出一些建议。

  • 确保源参数和目标参数都用双引号引起来,但在本例中似乎$_.fullname没有。
  • 检查源路径和目标路径不超过 Windows 文件路径和名称长度规范(总共 260 个字符)
  • 检查源路径和目标路径不包含任何保留文件/路径字符 (< > ; " / \ | ? *)
  • 如果这不起作用,尝试使用以下方法捕获复制过程进程监控并发布捕获文件。

相关内容