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

Time:03-27

I have many files that contains an ID and I need to get all the IDs and compare to the rest of the files and show if there are same IDs. What I am looking for is Package ID="" ,Could be more than one in the same file, each file contains also a similar string but without space (PackageID='' that I don't need to collect I started with:

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

enter image description here

CodePudding user response:

You don't have text files. You have XML files.

Don't use Select-String. Use Select-Xml and XPath.

$packages = Get-ChildItem *.manifest | Select-Xml -XPath "/Vip/Package/@ID"
$packageGroups = $packages | Group-Object { $_.Node."#text" } 

$packageGroups | where Count -gt 1 | foreach {
    Write-Host "Package ID:" $_.Name
    foreach ($g in $_.Group) {
        Write-Host " ->" $g.Path
    }
}

CodePudding user response:

You could try something like this, unfortunately, if there can be more than one appearance of Package ID you would need to read the full content of the file. You can use a switch with the -Regex parameter to search for the Guid and -File parameter to read the 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
  • Related