对拒绝访问的User \ Documents上的Powershell Copy-Item失败

时间:2012-12-27 20:10:14

标签: powershell windows-8

我有一个简单的备份脚本,可以将Documents文件夹复制到外部硬盘:

Copy-Item C:\Users\Username\Documents G:\Backup -Recurse -Force

但是,当我运行该命令时,Windows 8会为文件夹'...\Documents\My Music''...\Documents\My Pictures''...\Documents\My Videos'引发UnauthorizedAccessException。但是,这些文件夹只是符号链接,我假设是导致错误的原因。如何避免复制符号链接?

修改:即使使用硬编码的排除列表也不起作用。完整的脚本如下:

$backupFolder = (get-date -uformat %Y%m%d)
$lines = Get-Content backup.cfg

$DirLinks = "C:\Users\Me\Documents\My Videos", "C:\Users\Me\Documents\My Music","C:\Users\Me\Documents\My Pictures"
$basePath = $lines[0] + $backupFolder + "\"
for ($i = 1; $i -lt $lines.Length; $i++)
{
    $lines[$i]
    Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -Force -Exclude $DirLinks
}

"Done!"

我仍然遇到以下错误:

    Copy-Item : Access to the path 'C:\Users\Me\Documents\My Music' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Music:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\Me\Documents\My Pictures' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Pictures:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\Me\Documents\My Videos' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Videos:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

2 个答案:

答案 0 :(得分:2)

不幸的是,没有简单的方法可以避免使用符号链接。

首先你需要找到它们:

$DirLinks = Get-ChildItem C:\SomeDir -Recurse | ? { $_.Attributes -like "*ReparsePoint*" } | % { $_.FullName } 

然后排除它们:

Copy-Item C:\SomeDir D:\Backup -Recurse -Force -Exclude $DirLinks

答案 1 :(得分:0)

您是否以管理员身份运行脚本?另外我假设运行脚本的用户也是适合用户名区域的用户?