简体   繁体   English

比较对象 cmdlet 不适用于“

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

I am building another web server "web2", which must have the same configuration as web server "web1"我正在构建另一个 Web 服务器“web2”,它必须与 Web 服务器“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所以在安装一些软件后,我需要编辑“web2”服务器上的“web.config”以匹配“web1”服务器中的那个

I am doing this from my laptop, so I copied both "web.config" files to my laptop running these commands:我是在笔记本电脑上执行此操作的,因此我将两个“web.config”文件都复制到了运行以下命令的笔记本电脑中:

    $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好的,现在我有来自两台服务器的“web.config”文件,命名为:web1.txt 和 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.在这里您可以看到文件差异的图片(我使用记事本++得到了这个),因为您可以看到只有 4 个差异。 please take in note I trim the files to only have 42 lines each请注意我将文件修剪为每个文件只有 42 行

所有差异都在红色框中

Then I tried to compare both files using the "Compare-Object" cmdlet but I am not getting an accurate info:然后我尝试使用“比较对象”cmdlet 比较两个文件,但我没有得到准确的信息:

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:奇怪的是,如果我只是将“< !--”替换为“< !--a”,输出将更改为:

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:然后如果我将“ - - >”更改为“ - - > b”,这是输出:

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.我尝试检查文件的编码,以 UTF-8 创建它们,我还删除了很多 xml 文件块,删除了很多括号(在两个文件上),但它从未显示出差异。

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.如下图所示,我做了一个选择字符串,在 web1.txt 中显示了第 11 行,但 web2.txt 没有显示第 11 行,所以这些文件不一样。

比较对象和选择字符串

Thank you!!!谢谢!!!

It looks like compare-object sorts the lines before it compares them.看起来 compare-object 在比较它们之前对它们进行排序。 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:文件1.xml:



<top>

  <inner1>
  </inner1>

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

file2.xml:文件2.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文件 1

line1
line2
line3
line4

file2文件 2

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这是一篇旧的博客文章,其中默认的同步窗口曾经是 5: http ://fatbeards.blogspot.com/2009/01/compare-object-syncwindow-parameter.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM