首页 > 解决方案 > PowerShell exact column comparison

问题描述

I have two text file and in this txt files there is an MD5 checksums of some backup files. I am trying to compare these text files and if MD5 is same I am try to adding near "-2" if it is different adding "-1" but the problem is directories are different that is why when I compare results being always -1. How can I choose exactly MD5 place in the file? I think if I can change the [System.IO.File]::ReadAllText('C:\Users\trerulug\Documents\Inventory1\Inventory.txt') in the code it could work.

Output :

MD5       C1FA0C20EEFD90076AEFAAB199D854B9 C:\Users\william\Documents\Inventory\12348318433batch.xml
MD5       C1FA0C20EEFD90076AEFAAB199D854B9 C:\Users\william\Documents\Inventory2\12348318433batch.xml,-1
MD5       86CA06D361850A9385CA475E133E4182 C:\Users\william\Documents\Inventory\12348318433batch.xml.xml
MD5       86CA06D361850A9385CA475E133E4182 C:\Users\william\Documents\Inventory2\12348318433batch.xml.xml,-1
MD5       A75515C272B4716FB160DDA644BC9955 C:\Users\william\Documents\Inventory\12348318433batch.xml
MD5       A75515C272B4716FB160DDA644BC9955 C:\Users\william\Documents\Inventory2\12348318433batch.xml,-1
& {
    $ErrorActionPreference = 'Stop'
    $OFS = ','

    try {
        $To = [System.IO.File]::ReadAllText('C:\Users\Williams\Documents\Inventory1\Inventory.txt')
        $From = & {
            if (-not $To.EndsWith([System.Environment]::NewLine)) { '' }
            Get-Content -Path C:\Users\william\Documents\Inventory2\Inventory.txt |
                ForEach-Object { "$($_, @(-1, -2)[[int]$To.Contains($_)])" }
        }

        Add-Content -Path C:\Users\william\Documents\Inventory1\Inventory.txt -Value $From
        Get-Content -Path C:\Users\trerulug\Documents\Inventory1\Inventory.txt
    } catch {
        $_ | Out-String | Write-Warning
    }
}

Expected output:

MD5       C1FA0C20EEFD90076AEFAAB199D854B9 C:\Users\william\Documents\Inventory\12348318433batch.xml
MD5       86CA06D361850A9385CA475E133E4182 C:\Users\william\Documents\Inventory2\12348318433batch.xml.xml
MD5       A75515C272B4716FB160DDA644BC9955 C:\Users\william\Documents\Inventory2\12348318433batch.xml
MD5       C1FA0C20EEFD90076AEFAAB199D854B9 C:\Users\william\Documents\Inventory2\12348318433batch.xml,-2
MD5       86CA06D361850A9385CA475E133E4182 C:\Users\william\Documents\Inventory2\12348318433batch.xml.xml,-2
MD5       A75515C272B4716FB160DDA644BC9955 C:\Users\william\Documents\Inventory2\12348318433batch.xml,-2

标签: powershellcomparisonmd5checksum

解决方案


推荐阅读