【问题标题】:How to exclude files and folders from Get-ChildItem in PowerShell?如何从 PowerShell 中的 Get-ChildItem 中排除文件和文件夹?
【发布时间】:2020-09-08 01:55:13
【问题描述】:

我制作了一个 PowerShell 脚本,它通过 md5 检查运行 robocopy。

它工作正常,但如果我尝试排除一些目录或文件,robocopy 会处理排除,而比较哈希的脚本的 MD5 部分不起作用,会返回一些错误,因为源文件/哈希比目的地...

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

以下是我到目前为止所做的。在这种模式下,md5-copy 工作(不排除):

$Source = "F:\"

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

机械复制:

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-children 列表中排除我不想复制的数据?在这种特定情况下,如果可能,在 Get-ChildItem 必须在整个文件系统中排除显式内容列表(变量字符串和/或数组)的“所有情况”中。

【问题讨论】:

    标签: powershell get-childitem get-filehash


    【解决方案1】:

    从 PowerShell 7.1 开始,Get-ChildItem 等 cmdlet 的 -Exclude-Include 提供程序参数仅对 names 项(文件/目录名称,如果是文件-系统提供程序),而不是完整路径或目录子树

    鉴于您要排除的所有路径都是目标目录的直接子目录,我建议采用两步方法:

    # Get all files and directories in $Source, except those to be excluded.
    # Note the use of \* instead of \*.*, so as to also include the
    # directories (whose names don't have an extension).
    $items = Get-Item $Source\* -Force | Where-Object FullName -NotIn $Ignored
    
    # Recursively process all resulting files and directories and
    # calculate their hashes.
    # Note the use of -File to limit output to files.
    $SourceHash = $items | Get-ChildItem -Recurse -Force -File | Get-FileHash
    

    当然,如果您仅根据文件/目录名称定义$Ignored数组,您可以使用-Exclude

    # Convert the ignore list to file/directory names only.
    $Ignored = $Ignored | Split-Path -Leaf
    
    $SourceHash = Get-ChildItem -File $Source -Recurse -Force -Exclude $Ignored |
                    Get-FileHash
    

    如果要排除的路径可能出现在子目录层次结构的任何级别,则需要做更多工作:

    $ignoredRegex = '(?<=^|\{0})({1})(?=\{0}|$)' -f
                      [IO.Path]::DirectorySeparatorChar,
                      ($Ignored.ForEach({ [regex]::Escape($_) }) -join '|')
    
    
    $SourceHash = Get-ChildItem $Source -Recurse -File -Force |
                    Where-Object FullName -notmatch $ignoredRegex
                      Get-FileHash
    

    上面使用regular expression-match 运算符的(取反形式)来排除子目录树中任何位置的所有指定项及其子项,递归

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2013-02-24
      • 2013-11-19
      相关资源
      最近更新 更多