1

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

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

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

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

$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
4

2 回答 2

2

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

首先,您需要找到它们:

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

然后排除它们:

Copy-Item C:\SomeDir D:\Backup -Recurse -Force -Exclude $DirLinks
于 2012-12-27T20:29:22.820 回答
0

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

于 2012-12-27T20:25:49.547 回答