PowerShell精确列比较

时间:2019-07-17 16:03:20

标签: powershell comparison md5 checksum

我有两个文本文件,在此txt文件中,有一些备份文件的MD5校验和。我正在尝试比较这些文本文件,如果MD5是相同的,我尝试在“ -2”附近添加,如果不同则添加“ -1”,但问题是目录不同,这就是为什么当我比较结果始终为-1时的原因。如何准确选择文件中的MD5位置?我认为,如果我可以在代码中更改[System.IO.File]::ReadAllText('C:\Users\trerulug\Documents\Inventory1\Inventory.txt'),就可以使用。

输出:

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
    }
}

预期输出:

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

0 个答案:

没有答案