2

I am building another web server "web2", which must have the same configuration as web server "web1"

So after installing some software I need to edit the "web.config" on the "web2" server to match the one in the "web1" server

I am doing this from my laptop, so I copied both "web.config" files to my laptop running these commands:

    $w1="web1.server.local"
    $w2="web2.server.local"
    $myCred=(Get-Credential -credential "myAD\myUser")
    $file="C:\path\to\my\web.config"
    Invoke-Command -ComputerName $w1 -Credential $myCred (Get-Content $args[0]) -ArgumentList $file | Set-content web1.txt
    Invoke-Command -ComputerName $w2 -Credential $myCred (Get-Content $args[0]) -ArgumentList $file | Set-content web2.txt 

Ok so now i have both "web.config" files from both servers named as: web1.txt and web2.txt

Here you can see a picture of the differences in the files (I got this using notepad++) as you can see there are only 4 differences. please take in note I trim the files to only have 42 lines each

all differences are in red boxes

Then I tried to compare both files using the "Compare-Object" cmdlet but I am not getting an accurate info:

PS C:\> Compare-Object (Get-Content .\web1.txt) (Get-Content .\web2.txt)

InputObject SideIndicator
----------- -------------
            <=
            <=

The weird part is that if I just replace "< ! - - " with " < ! - - a" the output changes to:

PS C:\> Compare-Object (Get-Content .\web1.txt) (Get-Content .\web2.txt)

InputObject SideIndicator
----------- -------------
      <!--  =>
      <!--a <=
            <=
            <=

and then if I change the " - - >" to " - - >b" this is the output:

PS C:\> Compare-Object (Get-Content .\web1.txt) (Get-Content .\web2.txt)

InputObject SideIndicator
----------- -------------
      <!--  =>
      -->   =>
      <!--a <=
      -->b  <=
            <=
            <=

I try checking the encoding of the files, creating them in UTF-8, I also remove a lot of chunks of the xml file, remove a lot of brackets (on both files) but it never shows the difference.

Is there something I am missing?

Why Compare-Object cant tell if a plain file has an extra " - - > " or " < ! - - "

As you can see in the following image, I did a select-string and in the web1.txt shown the line 11, but web2.txt didnot shown line 11, so those files are not the same.

Compare-Object AND Select-String

Thank you!!!

  • 4
    Can we see the files or a small example of them that shows the problem? – js2010 Apr 24 '20 at 14:22
  • 1
    Your output suggests that the RHS file has empty / blank lines that aren't present in the LHS file. – mklement0 Apr 24 '20 at 14:53
  • I suspect that you installed something over the `Cat` alias to [`prettify the xml code`](https://stackoverflow.com/q/16090869/1701026) and comparing the standard output instead. Have you actual tried to use the original PowerShell `Get-Content` cmdlet rather than it's `Cat` *alias*? Thus: `Compare-Object (Get-Content web1.config.txt) (Get-Content web2.config.txt)` – iRon Apr 25 '20 at 07:21

1 Answers1

0

Didn't I already answer this? Compare-object sorts the two inputs first, so the two files end up being the same except for two extra blank lines.

Here's the same question: Compare-Object cmdlet wont work with "<!--" in a txt file

js2010
  • 23,033
  • 6
  • 64
  • 66