William Kendly
William Kendly

Reputation: 35

PowerShell Compare Outputs

I've got a text file where I'am storing some backup file names with their hash tables. I've got another text file where I'am storing only hash tables. I add the second text file under the first documents. I want to compare these hash tables if they are the same, I want to add near the hash table -2, else I want to add near the hash table -1.

  • "SHA256","89EC548C14582B2BDC7739BC0FA007","C:\WINDOWS\[email protected]"

  • "SHA256","0B99B2576F1FA0689FF6E03462076C","C:\WINDOWS\system32@AudioTot"

  • "SHA256","F3B178AD338023AA3CBCB071CC0063","C:\WINDOWS\[email protected]"

  • "SHA256","383D8CBFCD078B3D661655A816676F","C:\WINDOWS\[email protected]"

  • "89EC548C14582B2BDC7739BC0FA007" -2

  • "0B99B2576F1FA0689FF6E03462076C" -2

  • "0A57D7F60CDD9DB6F6F461BBC464FD" -1

  • "F3B178AD338023AA3CBCB071CC0063" -2

  • "7BCA060610F1A753A881593F967G47" -1

  • "383D8CBFCD078B3D661655A816676F" -2

I am using add the hashes under the txt file from another file but could not compare :

$From = Get-Content -Path .\CopyFromFile.txt

Add-Content -Path .\CopyToFile.txt -Value $From

Get-Content -Path .\CopyToFile.txt

Upvotes: 0

Views: 164

Answers (2)

Andrei Odegov
Andrei Odegov

Reputation: 3429

Check if the following code helps solve your problem.
The code was wrapped in a script block to create a new scope and preserve the values of the $ErrorActionPreference and $OFS preference variables.
The ReadAllText method was used to read the contents of the CopyToFile.txt file.

& {
  $ErrorActionPreference = 'Stop'
  $OFS = ','
  try {
    $To = [System.IO.File]::ReadAllText('.\CopyToFile.txt')
    $From = & {
      # Add a line if the file .\CopyToFile.txt did not end with a newline.
      if ( -not $To.EndsWith([System.Environment]::NewLine) ) { '' }
      Get-Content -Path .\CopyFromFile.txt |
        ForEach-Object { "$($_, @(-1, -2)[[int]$To.Contains($_)])" }
    }
    Add-Content -Path .\CopyToFile.txt -Value $From
    Get-Content -Path .\CopyToFile.txt
  } catch {
    $_ | Out-String | Write-Warning
  }
}

Output:

"SHA256","89EC548C14582B2BDC7739BC0FA007","C:\WINDOWS\[email protected]"
"SHA256","0B99B2576F1FA0689FF6E03462076C","C:\WINDOWS\system32@AudioTot"
"SHA256","F3B178AD338023AA3CBCB071CC0063","C:\WINDOWS\[email protected]"
"SHA256","383D8CBFCD078B3D661655A816676F","C:\WINDOWS\[email protected]"
"89EC548C14582B2BDC7739BC0FA007",-2
"0B99B2576F1FA0689FF6E03462076C",-2
"0A57D7F60CDD9DB6F6F461BBC464FD",-1
"F3B178AD338023AA3CBCB071CC0063",-2
"7BCA060610F1A753A881593F967G47",-1
"383D8CBFCD078B3D661655A816676F",-2

Upvotes: 1

user6811411
user6811411

Reputation:

Pardon me, but I don't think it is a clever approach to create a text file with different formats inside.

Why not using a more PowerShell like way with Import-Csv and Compare-Object creating a new object which can also be saved as a csv file.

Presuming the above data is stored in files hashes.csv and the pure hashes in hashes.txt

This small script (line)

Compare-Object -ref (Import-Csv .\hashes.csv -Header Algorythm,Hash,File) `
               -dif (Import-csv .\hashes.txt -Header Hash) -Property Hash -PassThru -IncludeEqual

yields this output:

Algorythm Hash                           File                          SideIndicator
--------- ----                           ----                          -------------
SHA256    89EC548C14582B2BDC7739BC0FA007 C:\WINDOWS\[email protected]    ==
SHA256    0B99B2576F1FA0689FF6E03462076C C:\WINDOWS\system32@AudioTot  ==
SHA256    F3B178AD338023AA3CBCB071CC0063 C:\WINDOWS\[email protected] ==
SHA256    383D8CBFCD078B3D661655A816676F C:\WINDOWS\[email protected] ==
          0A57D7F60CDD9DB6F6F461BBC464FD                               =>
          7BCA060610F1A753A881593F967G47                               =>

which could be tweaked to replace the SideIndicator with your values -2,-1

Upvotes: 1

Related Questions