在 txt 中搜索字符串的 PowerShell 脚本,收集它们并在所有文件之间进行比较

PowerShell script that searches for a string in txt , collect them and compare between all files

我有很多包含 ID 的文件,我需要获取所有 ID 并与其余文件进行比较,并显示是否存在相同的 ID。 我正在寻找的是 Package ID="" ,在同一个文件中可能不止一个,每个文件也包含一个类似的字符串但没有 space (我不需要收集的 PackageID='' 我开始于:

$seedMatches = Select-String -Path "C:\Temp\vip.manifest" -Pattern "Package ID"

您可以尝试这样的操作,不幸的是,如果 Package ID 的出现不止一次,您将需要阅读文件的全部内容。您可以使用 switch with the -Regex parameter to search for the Guid-File 参数来读取文件:

$initialPath = 'path/to/filemanifests'
$guidmap = foreach($file in Get-ChildItem $initialPath -Filter *.manifest) {
    switch -Regex -File($file) {
        '(?<=<Package ID=")(?<guid>[\d\w-]+)"' {
            [pscustomobject]@{
                Guid = $Matches['guid']
                Path = $file.FullName
            }
            # if you only need the first appearance of the Guid
            # you can add a `break` here to stop searching
        }
    }
}

$guidmap | Group-Object Guid | Where-Object Count -GT 1 | ForEach-Object Group