PowerShell,从Get-ChildItem中排除文件和跟踪信息

问题描述 投票:0回答:1

我制作了一个Powerschell脚本,该脚本运行带有md5检查的robocopy。

效果很好,但是如果我尝试排除一些目录或文件,则robocopy会处理该排除,而比较哈希的脚本的MD5部分则无法正常工作,会返回一些错误,因为源文件/哈希比目标文件多...

我可能已经尝试过在这里和互联网上找到的所有方法!我无法从路径中排除目录和/或文件!

这是我所定义的以及如何:(在此模式下,md5-复制有效,但不排除)

$Source = "F:\"

$IgnoreDir = @(
    $Source + '$RECYCLE.BIN'
    $Source + "System Volume Information"
    $Source + "VMs"
)   
$IgnoreFile = @(
    $Source + "SHDrive.vmdk"
    $Source + "SHDrive-flat.vmdk"
)
$Ignored = $IgnoreDir + $IgnoreFile

Robocopy:

Robocopy.exe /R:1 /W:0 $Source $Dest /E /V /TEE /XD $IgnoreDir /XF $IgnoreFile /LOG:$LogDir\RBCY_MD5_F.txt

MD5:

$SourceHash = Get-ChildItem "$Source\*.*" -Recurse -Force -Exclude $Ignored | Where-Object {!$_.psiscontainer } | Get-FileHash
$SourceHash | Select-Object "Hash", "path" | ft -HideTableHeaders -AutoSize | Out-File -Width "300" $LogDir\SRC_MD5_REF.txt
$SourceHash.Hash | Out-File $LogDir\SRC_MD5.txt 

比较:

$Diff = Compare-Object -ReferenceObject $(get-content "$LogDir\SRC_MD5.txt") -DifferenceObject $(get-content "$LogDir\DST_MD5.txt")

F:\驱动器的内容:

PS C:\Users\Robbi> Get-ChildItem F:\ -force


    Directory: F:\


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--hs-       19/03/2019     06:40                $RECYCLE.BIN
d-----       16/05/2020     04:41                DATA
d-----       19/01/2020     06:34                Drivers
d-----       16/05/2020     04:55                Gumball
d-----       16/05/2020     04:58                SW
d--hs-       19/03/2019     06:36                System Volume Information
d-----       13/03/2020     16:08                Tools
d-----       12/12/2019     00:02                VMs
d-----       16/05/2020     04:55                _Pre-Cestino
-a----       08/02/2020     03:02    21474836480 SHDrive-flat.vmdk
-a----       08/02/2020     03:02            466 SHDrive.vmdk

如何从“获取子项”列表中排除不想复制的数据?在这种特定情况下,如果可能的话,可以在“所有情况”中使用其中Get-childitem必须在整个文件系统中排除显式contents-list(可变字符串和/或数组)的方式。

powershell get-childitem exclude get-filehash
1个回答
0
投票

-Exclude(和-Include)仅对组件名称(文件/目录名称)起作用,而不对完整路径起作用。

要排除完整路径,必须使用单独的Where-Object命令:

Where-Object
© www.soinside.com 2019 - 2024. All rights reserved.