【问题标题】:compare-object issue using powershell使用 powershell 比较对象问题
【发布时间】:2018-03-15 00:44:52
【问题描述】:

寻找比较两个 system.objects 的最佳方法。尝试了 compare-object 选项,似乎没有给出所需的输出..

> $abc
BGP summary information for VRF default
Router identifier 192.168.0.3, local AS number 7251
Neighbor Status Codes: m - Under maintenance
  Neighbor         V  AS           MsgRcvd   MsgSent  InQ OutQ  Up/Down State  PfxRcd PfxAcc
  10.12.103.119    4  7251               0         0    0    0    5d23h Connect
  10.46.252.121    4  7251               0         0    0    0    5d23h Connect

> $def
BGP summary information for VRF default
Router identifier 192.168.0.3, local AS number 7251
Neighbor Status Codes: m - Under maintenance
  Neighbor         V  AS           MsgRcvd   MsgSent  InQ OutQ  Up/Down State  PfxRcd PfxAcc
  10.12.103.119    4  7251               0         0    0    0    5d23h Active
  10.46.252.121    4  7251               0         0    0    0    5d23h Estab 

> $abc.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

> $def.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

> Compare-Object -ReferenceObject $abc -DifferenceObject $def

InputObject
-----------
BGP summary information for VRF default...
BGP summary information for VRF default...

【问题讨论】:

    标签: powershell compare


    【解决方案1】:

    您处理的是字符串 ([System.String]),而不是[System.Objects] - 后者只是基本类型,即[System.String] 源自

    比较两个多行字符串的最佳方法是逐行比较它们,为此您必须将它们拆分为一个行数组:

    PS> Compare-Object ($abc -split '\r?\n') ($def -split '\r?\n') 
    
    InputObject                                                                     SideIndicator
    -----------                                                                     -------------
      10.12.103.119    4  7251               0         0    0    0    5d23h Active  =>           
      10.46.252.121    4  7251               0         0    0    0    5d23h Estab   =>           
      10.12.103.119    4  7251               0         0    0    0    5d23h Connect <=           
      10.46.252.121    4  7251               0         0    0    0    5d23h Connect <=           
    

    =&gt; 行(表示差异的输出对象的.SideIndicator 属性值)表示 RHS 独有的行 ($def) 而 &lt;= 表示 LHS 独有的行 ($abc)

    【讨论】:

    • 好像是手动的,多行比较一下,有没有其他选择?
    • 上面带有&lt;==&gt; 的每个输出行都是一个对象,您可以以编程方式处理它,所以问题是:什么表示您在寻找哪些差异?请直接更新您的问题。
    • 是的,需要以编程方式浏览。应该很简单,谢谢你的建议,不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    相关资源
    最近更新 更多