Compare-Object cmdlet wont work with “<!--” in a txt file

风流意气都作罢 提交于 2021-01-28 08:40:57

问题


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

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.

Thank you!!!


回答1:


It looks like compare-object sorts the lines before it compares them. It looks like you have two extra blank lines in the first file. Otherwise, if the lines are sorted they are the same. Minimal reproducible example:

file1.xml:



<top>

  <inner1>
  </inner1>

  <!--
  <inner2>
  </inner2>
  -->
</top>

file2.xml:

<top>
  <!--
  <inner1>
  </inner1>
  -->

  <inner2>
  </inner2>

</top>
compare-object (cat file1.xml) (cat file2.xml) 


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

cat file1.xml | sort




  -->
  <!--
  </inner1>
  </inner2>
  <inner1>
  <inner2>
</top>
<top>


cat file2.xml | sort


  -->
  <!--
  </inner1>
  </inner2>
  <inner1>
  <inner2>
</top>
<top>

Syncwindow example:

file1

line1
line2
line3
line4

file2

line3
line4

Looks like it gives up looking for equal ones in that sync window:

compare-object (cat file1) (cat file2) -SyncWindow 1

InputObject SideIndicator
----------- -------------
line3       =>
line1       <=
line2       <=
line3       <=

compare-object (cat file1) (cat file2)

InputObject SideIndicator
----------- -------------
line1       <=
line2       <=

Here's an old blog post where the default syncwindow used to be 5: http://fatbeards.blogspot.com/2009/01/compare-object-syncwindow-parameter.html



来源:https://stackoverflow.com/questions/61461930/compare-object-cmdlet-wont-work-with-in-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!