0

有人可以帮帮我吗。对 powershell 来说还是新手,我正在比较我的文档和桌面镜像,以使用几种不同的代码检查我的备份解决方案。下面的一个旨在检查文档/桌面及其镜像文件夹,并准确告诉我源和目标之间哪些文件“不同”,并将其输出到同一个 output.txt 文件中(不确定它是否会覆盖它)。当我单独为我的文档执行此操作时,当我想为我的桌面尝试代码时它会工作,它根本不会输出任何东西。有什么建议吗?

function Get-Directories ($path)
{
    $PathLength = $path.length
    Get-ChildItem $path -exclude *.pst,*.ost,*.iso,*.lnk | % {
        Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.substring($PathLength+1)
        $_
    }
}

Compare-Object (Get-Directories $Folder3) (Get-Directories $Folder4) -Property RelativePath | Sort RelativePath, Name -desc | Out-File  C:\Users\desktop\output.txt
4

2 回答 2

1

由于您-Recurse在列出文件时没有使用,您可以只使用文件名而不是添加相对路径属性:

$folder1 = gci 'C:\Users\username\desktop' -exclude *.pst,*.ost,*.iso,*.lnk
$folder2 = gci 'D:\desktop' -exclude *.pst,*.ost,*.iso,*.lnk
Compare-Object $folder1 $folder2 -Property Name,Length

Name     Length SideIndicator
----     ------ -------------
test.txt    174 =>           
test.csv    174 <=           

# Alternatively, use -PassThru to keep the whole object:
Compare-Object $folder1 $folder2 -Property Name,Length -PassThru | select SideIndicator,FullName,Length,LastWriteTime

SideIndicator FullName                  Length LastWriteTime       
------------- --------                  ------ -------------       
=>            D:\desktop                   174 7/14/2021 2:47:09 PM
<=            C:\Users\username\desktop    174 7/14/2021 2:47:09 PM

用于Out-File -Append将输出附加到文件。


至于对当前脚本进行故障排除,请尝试手动检查 RelativePath 属性是否看起来为您正确设置:

(Get-Directories $Folder3).RelativePath
(Get-Directories $Folder4).RelativePath

最后,我建议在 powershell 上使用 robocopy 来进行此类备份,因为它可以使用备份权限(对于锁定的文件)并且可以一次复制多个文件,但这是个人喜好:

robocopy source destination /b /mir /mt /r:0 /w:0

/b - Runs robocopy in backup mode. Will copy everything as long as you are an Administrator
/mir - Mirrors everything from the source to the destination
/mt - Copies up to 8 files at a time
/r:0 - Sets it to not retry a file, default is like a million retries
/w:0 - Sets the time to 0 seconds between retries - default is like 30 seconds

来源:https ://community.spiceworks.com/topic/286190-transfer-user-profiles-using-robocopy

于 2021-07-15T16:15:49.210 回答
1

从您问题的早期版本来看,您的问题不在于您的代码根本没有输出任何内容,而是输出具有空Name属性值

因此,您的代码中唯一缺少的Compare-Object-PassThruswitch

Compare-Object -PassThru (Get-Directories $Folder3) (Get-Directories $Folder4) -Property RelativePath | 
  Sort RelativePath, Name -desc | 
    Out-File C:\Users\desktop\output.txt

如果没有 -PassThru,则Compare-Object输出[pscustomobject]具有.SideIndicator属性的实例(以指示差异对象专属于哪一侧)并且仅将比较属性传递给-Property

  • 也就是说,在您最初的尝试中,Compare-Object输出对象只有 .SideIndicator.RelativePath属性,而原始[System.IO.FileInfo]实例的其他属性都没有Get-ChildItem,例如.Name, .LastWriteTime, ...

使用 -PassThru原始对象被传递,用 ETS(扩展类型系统).SideIndicator属性装饰(装饰方式与您添加.RelativePath属性的方式相同),稍后访问该.Name属性按预期工作。

笔记:

  • 从那时Out-File起接收完整的(和修饰的)[System.IO.FileInfo]实例,您可能希望Select-Object预先限制通过调用写入的属性。
    此外,您可以选择结构化输出格式,Export-Csv例如,考虑到适用的格式Out-File仅适用于人类观察者,而不适用于程序处理。

  • $_.FullName.substring($PathLength+1)在你Get-Directories应该是$_.FullName.substring($PathLength),否则你会切断第一个字符。

于 2021-07-16T03:02:31.683 回答