Comparing two files

Welcome Forums General PowerShell Q&A Comparing two files

Viewing 4 reply threads
  • Author
    Posts
    • #210864
      Participant
      Topics: 12
      Replies: 11
      Points: 156
      Rank: Participant

      Hello All,

      I am in the process of writing some code to compare two files.

      1. Place the data into variables
      2. Use get-content to obtain the content of the files
      3. Compare and echo the differences

      $a = get-content “C:\temp\one.txt”
      $b = get-content “c:\temp\two.txt”
      Compare-Object $a $b -IncludeEqual

      One.txt has a bunch of letters in it two.txt has nothing in it. This is my prototype as the end goal is to compare hotfixes in each file and then use compare-object to echo what is different.

       

      However when I run the code I get the following error cant figure out what the  issue is

      Compare-Object : Cannot bind argument to parameter ‘DifferenceObject’ because it is null.
      At line:1 char:19
      + Compare-Object $a $b
      + ~~
      + CategoryInfo : InvalidData: (:) [Compare-Object], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.C
      ompareObjectCommand

      Regards

       

      Fish and Chips

       

    • #210873
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      To compare file content you use hashing as in:

      if ((Get-FileHash .\file1.txt).Hash -eq (Get-FileHash .\file2.txt).Hash) {
          'The 2 files are identical'
      } else {
          'the 2 files are different'
      }
      

      This may be slow for large files across slow wan links.
      A less accurate but faster method is to compare file sizes in bytes, and their LastWriteTime

    • #210876
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      The error is clear, $b is null or nothing as you specified you are providing nothing in the file. Basically, the error is you need to provide a value to compare against. How are you populating the hotfixes into the files? Have you looked at Get-Hotfix?

      PS C:\Users\rasim> $list1 = Get-Hotfix
      
      PS C:\Users\rasim> $list1
      
      Source        Description      HotFixID      InstalledBy          InstalledOn              
      ------        -----------      --------      -----------          -----------              
      DESKTOP-MG... Update           KB4537572     NT AUTHORITY\SYSTEM  3/4/2020 12:00:00 AM     
      DESKTOP-MG... Security Update  KB4516115     NT AUTHORITY\SYSTEM  11/12/2019 12:00:00 AM   
      DESKTOP-MG... Update           KB4517245     NT AUTHORITY\SYSTEM  12/26/2019 12:00:00 AM   
      DESKTOP-MG... Security Update  KB4521863     NT AUTHORITY\SYSTEM  11/12/2019 12:00:00 AM   
      DESKTOP-MG... Security Update  KB4524244     NT AUTHORITY\SYSTEM  2/13/2020 12:00:00 AM    
      DESKTOP-MG... Security Update  KB4524569     NT AUTHORITY\SYSTEM  11/14/2019 12:00:00 AM   
      DESKTOP-MG... Security Update  KB4525419     NT AUTHORITY\SYSTEM  11/12/2019 12:00:00 AM   
      DESKTOP-MG... Security Update  KB4528759     NT AUTHORITY\SYSTEM  1/14/2020 12:00:00 AM    
      DESKTOP-MG... Security Update  KB4537759     NT AUTHORITY\SYSTEM  2/13/2020 12:00:00 AM    
      DESKTOP-MG... Security Update  KB4538674     NT AUTHORITY\SYSTEM  2/13/2020 12:00:00 AM    
      DESKTOP-MG... Security Update  KB4541338     NT AUTHORITY\SYSTEM  3/10/2020 12:00:00 AM    
      DESKTOP-MG... Update           KB4551762     NT AUTHORITY\SYSTEM  3/13/2020 12:00:00 AM  
      

      Then you can do:

      $list1 = Get-Hotfix -ComputerName Computer123
      $list2 = Get-Hotfix -ComputerName Computer321
      
      Compare-Object -ReferenceObject $list1 -DifferenceObject $list2 -Property HotFixId
      

      Get-Content is returning an array of strings. If you want to use the list you can do this:

      #Emulate Get-Content
      $list1 = @"
      hotfix1
      hotfix2
      hotfix3
      hotfix4
      "@ -split [environment]::NewLine
      
      #Emulate Get-Content
      $list2 = @"
      hotfix1
      hotfix4
      "@ -split [environment]::NewLine
      
      Compare-Object -ReferenceObject $list1 -DifferenceObject $list2
      

      A little more structured would be:

      $list1 = @"
      hotfix1
      hotfix2
      hotfix3
      hotfix4
      "@ | ConvertFrom-CSV -Header HotfixId
      
      $list2 = @"
      hotfix1
      hotfix4
      "@ | ConvertFrom-CSV -Header HotfixId
      
      Compare-Object -ReferenceObject $list1 -DifferenceObject $list2 -Property HotfixId
      
    • #210879
      Participant
      Topics: 12
      Replies: 11
      Points: 156
      Rank: Participant

      Hi Sam\Rob

      Thanks for your input. I know of this get-hotfix command and have experimented with it. However I am writing small prototype programs to see if they work first before moving onto building get-hotfix into my script.

      The end goal i envisage is for me to run the get-hotfix on two servers then generate a file with a list of hotfixes in there. Then compared the two files to see where the difference is in the hotfixes.

       

      Hope this makes sense

       

      Thanks

       

      Fish and Chips

       

       

       

    • #210888
      Participant
      Topics: 4
      Replies: 13
      Points: 49
      Rank: Member

      Have you considered capturing the results of the Get-HotFix execution as files via export-csv or export-clixml?  Then when you want to compare, recreate the objects by importing the files (import-csv or import-clixml) and then compare the *objects*.

      Get-HotFix | Export-CSV -Path C:\Temp\MyHotFixes.csv
      Get-HotFix -ComputerName | Export-CSV -Path c:\temp\YourHotFixes.csv
      
      # ...Fifteen Minutes Later...
      
      $X = IMport-Csv -Path C:\Temp\MyHostFixes.csv
      $Y = Import-CSV -Path C:\Temp\YourHotFixes.csv
      
      Compare-Object $X $Y -Property HotFixID
      • This reply was modified 2 months, 1 week ago by JB Lewis.
Viewing 4 reply threads
  • You must be logged in to reply to this topic.