Home > Net >  How can I access my custom side indicators in Compare-Object in powershell?
How can I access my custom side indicators in Compare-Object in powershell?

Time:10-27

I am comparing two xml files that contain a record of directories and files that we want to update to be the same. I have custom messages that replace the "=>" and "<=" side indicators but in the file I export the object to I cannot see the messages or indicator column. When the names are very long is the side indicator put somewhere else? I just want to be able to see which file the difference comes from. This is what I see in the txt file I write it to.

  <Snapshots timestamp="10/19/2021 08:01:20">
  <Snapshots timestamp="10/26/2021 09:10:15">
      <Directory path="v195" />
      <File path="V169\seamless-mob-satinfo-v169" checksum="3857365E7BE8DB0933C57E17B69A250A7DD3A67C45A3F4E095E23797367E1BD6A7AF9F76FFF25B6016B9040C68561A8FF3076F4A195B89BAC74FEE7834106047" />
      <File path="v195\seamless-global-satinfo-v195" checksum="E68E8284A28C52EF43C447B446C31306D00FA1487CF9CDD630D39B507A403F18180210BA4B3F28218A501FFC2631BFD3D89A7F5C1A3CD0ABE09EB35DB806DD41" />
      <Directory path="A319\Version 7.10.1.58" />
      <File path="A319\Version 7.10.1.58\AAL_A319_4Wap_Production_7.10.1.58.tar.xz.enc" checksum="E44A154612CA668A720AB6AA57295832EB6D9B4A296A4410C73F6EF32165E0C3963CFA4922CF4CB4B50DC6F625BDEA0350BA2BFA30B42A274419C3908308C36E" />
      <Directory path="A321\Version 7.10.1.58" />
      <File path="A321\Version 7.10.1.58\AAL_A321_5Wap_Production_7.10.1.58.tar.xz.enc" checksum="39807D865C2429919A6F8635E82D12AB295313AF86F367EF43B6EC5A39620AE55F3764E5DCDDAF9448087BF8634776DCBBD9DACE7027458820DE432018DC09DC" />
      <Directory path="A321NX\Version 7.10.1.58" />
      <File path="A321NX\Version 7.10.1.58\AAL_A21N_5Wap_Production_7.10.1.58.tar.xz.enc" checksum="7059A222B8F14ECBE365157CBC24EC5573238A136B86446F3766B53EEF699DC86787C28EB246648866BAA20B28A3D6EFA59B5275CB31145E6E8BF35EAE2630D7" />
      <Directory path="B737MAX8\Version 7.10.1.58" />
      <File path="B737MAX8\Version 6.5.1.3\AAL_B38M_5Wap_Production_6.5.1.3.tar.xz.enc" checksum="06EB98A7D5993449B09707F730EBEBBA3F8E53731A9C8040D7298924F2427D8A72C14E61D8E43AB7477ECF7CEC4C5D67A1385DF95130D5C9141E6AF048AF42E3" />
      <File path="B737MAX8\Version 7.10.1.58\AAL_B38M_5Wap_Production_7.10.1.58.tar.xz.enc" checksum="7E83CF06AC84A16343149492ABDDF1893220AC109B079153A3E6905E4C8512DB22DFFDD08912ECE5B8C2CCF22CFF49814B5F8C97BB0164BF28826E0B918A19B2" />
      <Directory path="B737NG\Version 7.10.1.58" />
      <File path="B737NG\Version 7.10.1.58\AAL_B738_5Wap_Production_7.10.1.58.tar.xz.enc" checksum="DC4804FC8C030963BFF75B74431B5E680E403EEE92613FE620779692249C49AD58F056A4FCC557DF4B8EFBE6F1EBF165CF130D956428EBE8C20C5CD6EB504835" />
      <Directory path="B787\Version 7.5.1.9" />
      <File path="B787\Version 7.5.1.9\AAL_B788_6Wap_Production_7.5.1.9.tar.xz.enc" checksum="E68BE59CEA9C557B8AE42A1FA705DA5C5C5ACCE7124F60952DCD689689699A4A61CC162DC52D5AD24A9D867DF439D8204A8B1F1945A87681BB2EC06D17F9D012" />
      <File path="Version 7.10.1.79\system_software_load_kit_prod_7.10.1.79.tar" checksum="9E62D7C8EDDC736B9B4E03A1E70599DA3DE536E2A890091E038209B3C28FFAC3F49809FCF57BDCF29A1B09FB8F4332B136C3ADF3B6CFDDC1BEC7982219F43374" />

My code I am attempting this with is this.

$NAS = Get-Content C:\temp\Gold_Copy.xml
$Snapshot = Get-Content C:\temp\QJ07312433_10_19_2021_snapshot.xml

(Compare-Object -ReferenceObject $NAS -DifferenceObject $Snapshot -PassThru |
    ForEach-Object {
        if ($_.SideIndicator -eq '=>') {
            $_.SideIndicator = 'Not in NAS'
        } elseif ($_.SideIndicator -eq '<=')  {
            $_.SideIndicator = 'Not in Snapshot'
        }
        $_
    }) > C:\temp\diff.txt

CodePudding user response:

You're using Compare-Object's -PassThru switch, which passes the (differing) input objects through as their original type, while decorating them with an instance .SideIndicator ETS NoteProperty property.

With strings as input objects, these decorations are ignored in many contexts, notably when you save to a file, such as with >.

You'll have to output the two pieces of information - the string value and the .SideIndicator value - separately.

You can repackage the values in a [pscustomobject] instance, which also allows you to put the side indicator into the first property, as shown in the following simplified example (note that there's then no strict reason to use -PassThru anymore):

Compare-Object ('foo', 'bar') ('foo', 'baz') -PassThru | 
  ForEach-Object {
    [pscustomobject] @{
      Source = @{ '=>' = 'Not in NAS'; '<=' = 'Not in snapshot'}[$_.SideIndicator]
      Line = $_
    }
  }

The above yields the following:

Source          Line
------          ----
Not in NAS      baz
Not in snapshot bar

Note that if you save this as-is to a file with >, you'll get exactly this for-display representation in the file.

Therefore, if you need to programmatically process the file later, it is better to use a structured text format such as CSV, for instance, by piping to Export-Csv.

  • Related