locked
Struggling -ExpandProperty to Export-csv RRS feed

  • Question

  • All,

    I'm learning PS and struggle on formatting when using -expandproperty.  I need assistance or guidance please. I am trying to Export-Csv below information. If you noticed the Hostname1 and Hostname2 have different "Key" and "Value" .  I want to export it to csv but it comes out different than what I expected.


    $hostname.Results | select Name, Serial, Model -ExpandProperty  Tags | Export-Csv -Path "D:\temp\Exported.csv" -NoClobber -NoTypeInformation

    above syntax  comes out like this. 

    Name          Serial      Model        Key                    Value     
    ----          ------      -----        ---                    -----     
    Hostname1 ABC1902KDXU BMWM3 EOL                    12/31/2021
    Hostname1 ABC1902KDXU BMWM3 InService              8/1/2019  
    Hostname1 ABC1902KDXU BMWM3 Depreciated            1/1/2025  
    Hostname1 ABC1902KDXU BMWM3 EOS                    3/28/2021 
    Hostname2 EFG2023J6HF BMWM3 EOS                    3/28/2021 
    Hostname2 EFG2023J6HF BMWM3 EOL                    12/31/2021


    How do I get it to export this format below?

    I have been trying to look around the web and no luck for me. I've tried below but again no luck

    Add-Member -Type Noteproperty

    Format-Table



    • Edited by Mr2urbo Monday, September 30, 2019 5:38 AM format
    Monday, September 30, 2019 5:33 AM

Answers

  • I took at shot at it and made my own example, assuming the tags were a hashtable.  It's too bad you can't add objects like hashtables.

    $results = [pscustomobject]@{
      Name = 'Hostname1'
      Serial = 'ABC1902KDXU'
      Model = 'BMWM3'
      Tags = @{
        EOL = '12/31/2021'
        InService = '8/1/2019'
        Depreciated = '1/1/2025'
        EOS = '3/28/2021'
      }
    }

    $results | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags.EOL
        InSerivce = $_.tags.InService
        Depreciated = $_.tags.Depreciated
        EOS = $_.tags.EOS
      }
    } | ft


    Name      Serial      Model EOL        InSerivce Depreciated EOS
    ----      ------      ----- ---        --------- ----------- ---
    Hostname1 ABC1902KDXU BMWM3 12/31/2021 8/1/2019  1/1/2025    3/28/2021

    Tuesday, October 1, 2019 3:02 PM

  • Please only post code using the code posting tool. The answer is what I pointed out above. It is an array of tagged hashes.

    $results.Tags.Where{$_.Key -eq'EOL'}.Value

    $results.Tags | 
        ForEach-Object{
            Write-Host $_.Key ==>> $_.Value
        }
    


    \_(ツ)_/

    • Marked as answer by Mr2urbo Friday, October 4, 2019 3:58 AM
    Friday, October 4, 2019 12:10 AM
  • You can also do this:

    PS C:\scripts> $results.Tags[0].Key
    EOL
    PS C:\scripts> $results.Tags[0].Value
    12/31/2021
    PS C:\scripts>
    


    \_(ツ)_/

    • Marked as answer by Mr2urbo Friday, October 4, 2019 3:57 AM
    Friday, October 4, 2019 12:12 AM
  • Wow, this is a strange structure.  I'm not sure how it's handled normally.

    $results = import-clixml results.xml

    # tags is an arraylist of psobjects, with 2 properties, key and value

    #   key                    value
    # 0 EOL                    12/31/2021
    # 1 InService              8/1/2019
    # 2 Depreciated            1/1/2025
    # 3 EOS                    3/28/2021
    # 4 Intersight.LicenseTier Essential

    $results | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags[0].value
        InSerivce = $_.tags[1].value
        Depreciated = $_.tags[2].value
        EOS = $_.tags[3].value
        IntersightLicenseTier = $_.tags[4].value
      }
    }

    • Edited by JS2010 Friday, October 4, 2019 4:34 AM
    • Marked as answer by Mr2urbo Friday, October 4, 2019 4:53 AM
    Friday, October 4, 2019 4:34 AM

All replies

  • You will have to use a calculated property to do this. Search for articles on h0ow to use calculated properties.


    \_(ツ)_/

    Monday, September 30, 2019 5:38 AM
  • What command creates that output?

    Monday, September 30, 2019 5:43 PM
  • "Select-Object".

    Try a search engine and look for "powershell calculated property". You'll find quite a few descriptions of how to use calculated properties.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, September 30, 2019 6:25 PM
  • I mean, where did "$hostname.Results" come from?
    Monday, September 30, 2019 6:29 PM
  • I'd guess he only posted a code snippet that focused on the problem, not how the variable was populated. From what he posted, it's clear that the variable contains the information.

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, September 30, 2019 7:03 PM
  • The $hostname.Results already contains data.  One of the data on that variable are formatted like so

    Tags                 : {class MoTag {
                             Key: EOS
                             Value: 3/28/2021
                           }
                           , class MoTag {
                             Key: EOL
                             Value: 12/31/2021



    • Edited by Mr2urbo Monday, September 30, 2019 11:13 PM typo
    Monday, September 30, 2019 11:10 PM
  • I'll give it a try and dig further information. Thanks Rich.
    Monday, September 30, 2019 11:10 PM
  • thanks jrv and give it a try and play around. 
    Monday, September 30, 2019 11:11 PM
  • A reproducible example would be easier to answer.
    Monday, September 30, 2019 11:14 PM
  • I took at shot at it and made my own example, assuming the tags were a hashtable.  It's too bad you can't add objects like hashtables.

    $results = [pscustomobject]@{
      Name = 'Hostname1'
      Serial = 'ABC1902KDXU'
      Model = 'BMWM3'
      Tags = @{
        EOL = '12/31/2021'
        InService = '8/1/2019'
        Depreciated = '1/1/2025'
        EOS = '3/28/2021'
      }
    }

    $results | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags.EOL
        InSerivce = $_.tags.InService
        Depreciated = $_.tags.Depreciated
        EOS = $_.tags.EOS
      }
    } | ft


    Name      Serial      Model EOL        InSerivce Depreciated EOS
    ----      ------      ----- ---        --------- ----------- ---
    Hostname1 ABC1902KDXU BMWM3 12/31/2021 8/1/2019  1/1/2025    3/28/2021

    Tuesday, October 1, 2019 3:02 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Thursday, October 3, 2019 1:46 AM
  • Thanks JS2010. I tried your recommendation , I still get stuck and struggle to populate the tag value. Here's what I have

    $hostname = Invoke-ComputePhysicalSummaryApiComputePhysicalSummariesGet
    $hostname.Results
    $Name = $hostname.Results | select Name
    $Serial = $hostname.Results | select Serial
    $Model = $hostname.Results | select Model
    $EOL = $hostname.Results | select EOL
    $InService = $hostname.Results | select InService 
    $Depreciated = $hostname.Results | select Depreciated
    $EOS = $hostname.Results | select EOS


    $hostname.Results = [pscustomobject]@{
      Name = $Name
      Serial = $Serial
      Model = $Model
      Tags = @{
        EOL = $EOL
        InService = $InService
        Depreciated = $Depreciated
        EOS = $EOS
      }
    }

    $hostname.Results[3] | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags.EOL
        InSerivce = $_.tags.InService
        Depreciated = $_.tags.Depreciated
        EOS = $_.tags.EOS
      }
    } | ft

    comes out to this, the EOL, InService, Depreciated and EOS are blank

                  

    Name          Serial               Model          EOL InService Depreciated EOS
    ----          ------      -----        --- --------- ----------- ---
    hostname1   FCH34649J6TJ  BMWM3               

    Here's data format from $hostname.Results

    PS D:\>> $hostname.Results


    AccountMoid          : 5c3e2d2173766a363424c14c
    Ancestors            : {}
    CreateTime           : 1/15/2019 6:58:20 PM
    ModTime              : 9/28/2019 4:37:24 PM
    Moid                 : 5c3e2d4c7262396339d1f314
    ObjectType           : compute.PhysicalSummary
    Owners               : {5c3e2d2173766a363424c14c, 5c3e2d2238357137782b5b26}
    Parent               : class MoBaseMoRef {
                             Moid: 5c3e2d487262396339d1efa1
                             ObjectType: equipment.Chassis
                           }
                           
    Tags                 : {class MoTag {
                             Key: EOL
                             Value: 3/28/2021
                           }
                           , class MoTag {
                             Key: InService
                             Value: 12/31/2021
    }
                           , class MoTag {
                             Key: Depreciated
                             Value: 12/31/2021
                           }
                           , class MoTag {
                             Key: EOS
                             Value: 12/31/2021
                           }
    ...}
    AdminPowerState      : policy
    AssetTag             : 
    AvailableMemory      : 524288
    ChassisId            : 7
    CpuCapacity          : 44
    DeviceMoId           : 5c3e2d2238357137782b5b26
    Dn                   : sys/chassis-7/blade-3
    FaultSummary         : 0
    Firmware             : 3.1(26k)
    Ipv4Address          : 1.1.1.74
    KvmIpAddresses       : {class ComputeIpAddress {
                             Address: 1.1.1.73
                             Category: Equipment
                             DefaultGateway: 1.1.1.1
                             Dn: sys/chassis-7/blade-3/mgmt/ipv4-pooled-addr
                             HttpPort: 0
                             HttpsPort: 0
                             KvmPort: 0
                             Name: Outband
                             Subnet: 255.255.255.0
                             Type: VnicIpV4PooledAddr
                           }
                           , class ComputeIpAddress {
                             Address: 0.0.0.0
                             Category: ServiceProfile
                             DefaultGateway: 0.0.0.0
                             Dn: sys/chassis-7/blade-3/mgmt/ipv4-prof-addr
                             HttpPort: 0
                             HttpsPort: 0
                             KvmPort: 0
                             Name: Outband
                             Subnet: 255.255.255.0
                             Type: VnicIpV4ProfDerivedAddr
                           }
                           }
    MemorySpeed          : 1866
    MgmtIpAddress        : 0.0.0.0
    Model                : BMWM3
    Name                 : hostname1
    NumAdaptors          : 1
    NumCpuCores          : 20
    NumCpuCoresEnabled   : 20
    NumCpus              : 2
    NumEthHostInterfaces : 8
    NumFcHostInterfaces  : 8
    NumThreads           : 40
    OperPowerState       : on
    OperState            : ok
    Operability          : operable
    PlatformType         : UCSFI
    Presence             : equipped
    RegisteredDevice     : class AssetDeviceRegistrationRef {
                             Moid: 5c3e2d2238357137782b5b26
                             ObjectType: asset.DeviceRegistration
                           }
                           
    Revision             : 0
    Rn                   : 
    Serial               : FCH34649J6TJ
    ServerId             : 0
    ServiceProfile       : org-root/Profile308
    SlotId               : 3
    SourceObjectType     : compute.Blade
    TotalMemory          : 524288
    UserLabel            : 
    Uuid                 : 28882e44-f4e3-11e4-0003-000000000068
    Vendor               : Cisco Systems Inc


    PS D:\>> 



    • Edited by Mr2urbo Thursday, October 3, 2019 3:49 AM clearing typo
    Thursday, October 3, 2019 3:44 AM
  • The $hostname.Results already contains data.  One of the data on that variable are formatted like so

    Tags                 : {class MoTag {
                             Key: EOS
                             Value: 3/28/2021
                           }
                           , class MoTag {
                             Key: EOL
                             Value: 12/31/2021

    The tags look like Json.  You will have to convert it in to a collection of objects and extract the keys and values.

    $hostname.Results |
    	ForEach-Object{
    		$tags = $_.Tags  | ConvertFrom-Json
    		[pscustomobject]@{
    			Name = $_.Name
    			Serial = $_.Serial
    			Model = $_.Model
    			EOL = $tags.EOL
    		}
    	}
    


    \_(ツ)_/

    Thursday, October 3, 2019 4:05 AM
  • What does this do?

    $hostname.results | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags.EOL
        InSerivce = $_.tags.InService
        Depreciated = $_.tags.Depreciated
        EOS = $_.tags.EOS
      }
    }

    • Edited by JS2010 Thursday, October 3, 2019 4:14 AM
    Thursday, October 3, 2019 4:13 AM
  • That won't work if it is Json and it looks more like Json then an array of hashes.


    \_(ツ)_/

    Thursday, October 3, 2019 4:44 AM
  • @JS2010  , it comes out like this

    Name        : Hostname1
    Serial      : FCH34649J6TJ
    Model       : BMWM3
    EOL         : 
    InSerivce   : 
    Depreciated : 
    EOS         : 

                      
    Thursday, October 3, 2019 4:48 AM
  • It is also an extended Json that requires a schema to convert. It is a typed set of objects.

    {
    	class MoTag {
              Key: EOS
              Value: 3/28/2021
         },
    	class MoTag {
             Key: EOL
             Value: 12/31/2021
        }
    }
    
    

    PowerShell cannot convert this so it will have to be converted by a custom script.


    \_(ツ)_/

    Thursday, October 3, 2019 4:50 AM
  • @jrv. i tried the Convertfrom-Jason and comes out with invalid

    ConvertFrom-Json : Invalid JSON primitive: class.
    At line:3 char:22
    +         $tags = $_.Tags  | ConvertFrom-Json
    +                            ~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

    Thursday, October 3, 2019 4:51 AM
  • It can't be converted by PowerShell. You will have to find out what it is. If it is a string then use string parsing to dev=code it.

    \_(ツ)_/

    Thursday, October 3, 2019 4:58 AM
  • If it is an array then it will need to be parsed as an array.

    $hostname.Results.Tags[0]


    \_(ツ)_/

    Thursday, October 3, 2019 5:00 AM
  • Thanks everyone. I will have to continue digging.  I believe I have enough ammo to continue my  journey :) 
    Thursday, October 3, 2019 5:21 AM
  • Let's try to serialize a sample.

    $hostname.results | select -first 1 | export-clixml results.xml
    get-content results.xml


    • Edited by JS2010 Thursday, October 3, 2019 1:18 PM
    Thursday, October 3, 2019 1:18 PM
  • PS D:\>> $hostname.Results | select -first 1 | export-clixml results.xml

    PS D:\>> get-content results.xml
    <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
      <Obj RefId="0">
        <TN RefId="0">
          <T>intersight.Model.ComputePhysicalSummary</T>
          <T>System.Object</T>
        </TN>
        <ToString>class ComputePhysicalSummary {_x000A_  AccountMoid: 5c3e2d2173766a363424c14c_x000A_  Ancestors: System.Collections.Generic.List`1[intersight.Model.MoBaseMoRef]_x000A_  CreateTime: 1/15/2019 6:58:21 PM_x000A_  ModTime: 9/
    28/2019 4:37:24 PM_x000A_  Moid: 5c3e2d4d7262396339d1f370_x000A_  ObjectType: compute.PhysicalSummary_x000A_  Owners: System.Collections.Generic.List`1[System.String]_x000A_  Parent: class MoBaseMoRef {_x000A_  Moid: 5c3e2d48726239633
    9d1ef9f_x000A_  ObjectType: equipment.Chassis_x000A_}_x000A__x000A_  Tags: System.Collections.Generic.List`1[intersight.Model.MoTag]_x000A_  AdminPowerState: policy_x000A_  AssetTag: _x000A_  AvailableMemory: 524288_x000A_  ChassisId:
     4_x000A_  CpuCapacity: 44_x000A_  DeviceMoId: 5c3e2d2238357137782b5b26_x000A_  Dn: sys/chassis-4/blade-4_x000A_  FaultSummary: 0_x000A_  Firmware: 3.1(25e)_x000A_  Ipv4Address: 10.250.140.87_x000A_  KvmIpAddresses: System.Collections
    .Generic.List`1[intersight.Model.ComputeIpAddress]_x000A_  MemorySpeed: 1866_x000A_  MgmtIpAddress: 10.250.140.5_x000A_  Model: UCSB-B200-M3_x000A_  Name: DAL-UCS-3-4-4_x000A_  NumAdaptors: 1_x000A_  NumCpuCores: 20_x000A_  NumCpuCore
    sEnabled: 20_x000A_  NumCpus: 2_x000A_  NumEthHostInterfaces: 8_x000A_  NumFcHostInterfaces: 8_x000A_  NumThreads: 40_x000A_  OperPowerState: on_x000A_  OperState: ok_x000A_  Operability: operable_x000A_  PlatformType: UCSFI_x000A_  P
    resence: equipped_x000A_  RegisteredDevice: class AssetDeviceRegistrationRef {_x000A_  Moid: 5c3e2d2238357137782b5b26_x000A_  ObjectType: asset.DeviceRegistration_x000A_}_x000A__x000A_  Revision: 0_x000A_  Rn: _x000A_  Serial: FCH1902
    KDXU_x000A_  ServerId: 0_x000A_  ServiceProfile: org-root/ls-DAL-UCS-ESXi-348_x000A_  SlotId: 4_x000A_  SourceObjectType: compute.Blade_x000A_  TotalMemory: 524288_x000A_  UserLabel: _x000A_  Uuid: 28882e44-f4e3-11e4-0003-00000000002b
    _x000A_  Vendor: Cisco Systems Inc_x000A_}_x000A_</ToString>
        <Props>
          <S N="AccountMoid">5c3e2d2173766a363424c14c</S>
          <Obj N="Ancestors" RefId="1">
            <TN RefId="1">
              <T>System.Collections.Generic.List`1[[intersight.Model.MoBaseMoRef, intersight, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]</T>
              <T>System.Object</T>
            </TN>
            <LST />
          </Obj>
          <DT N="CreateTime">2019-01-15T18:58:21.177Z</DT>
          <DT N="ModTime">2019-09-28T16:37:24.691Z</DT>
          <S N="Moid">5c3e2d4d7262396339d1f370</S>
          <S N="ObjectType">compute.PhysicalSummary</S>
          <Obj N="Owners" RefId="2">
            <TN RefId="2">
              <T>System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]</T>
              <T>System.Object</T>
            </TN>
            <LST>
              <S>5c3e2d2173766a363424c14c</S>
              <S>5c3e2d2238357137782b5b26</S>
            </LST>
          </Obj>
          <Obj N="Parent" RefId="3">
            <TN RefId="3">
              <T>intersight.Model.MoBaseMoRef</T>
              <T>System.Object</T>
            </TN>
            <ToString>class MoBaseMoRef {_x000A_  Moid: 5c3e2d487262396339d1ef9f_x000A_  ObjectType: equipment.Chassis_x000A_}_x000A_</ToString>
            <Props>
              <S N="Moid">5c3e2d487262396339d1ef9f</S>
              <S N="ObjectType">equipment.Chassis</S>
            </Props>
          </Obj>
          <Obj N="Tags" RefId="4">
            <TN RefId="4">
              <T>System.Collections.Generic.List`1[[intersight.Model.MoTag, intersight, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]</T>
              <T>System.Object</T>
            </TN>
            <LST>
              <Obj RefId="5">
                <TN RefId="5">
                  <T>intersight.Model.MoTag</T>
                  <T>System.Object</T>
                </TN>
                <ToString>class MoTag {_x000A_  Key: EOL_x000A_  Value: 12/31/2021_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Key">EOL</S>
                  <S N="Value">12/31/2021</S>
                </Props>
              </Obj>
              <Obj RefId="6">
                <TNRef RefId="5" />
                <ToString>class MoTag {_x000A_  Key: InService_x000A_  Value: 8/1/2019_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Key">InService</S>
                  <S N="Value">8/1/2019</S>
                </Props>
              </Obj>
              <Obj RefId="7">
                <TNRef RefId="5" />
                <ToString>class MoTag {_x000A_  Key: Depreciated_x000A_  Value: 1/1/2025_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Key">Depreciated</S>
                  <S N="Value">1/1/2025</S>
                </Props>
              </Obj>
              <Obj RefId="8">
                <TNRef RefId="5" />
                <ToString>class MoTag {_x000A_  Key: EOS_x000A_  Value: 3/28/2021_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Key">EOS</S>
                  <S N="Value">3/28/2021</S>
                </Props>
              </Obj>
              <Obj RefId="9">
                <TNRef RefId="5" />
                <ToString>class MoTag {_x000A_  Key: Intersight.LicenseTier_x000A_  Value: Essential_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Key">Intersight.LicenseTier</S>
                  <S N="Value">Essential</S>
                </Props>
              </Obj>
            </LST>
          </Obj>
          <S N="AdminPowerState">policy</S>
          <S N="AssetTag"></S>
          <I64 N="AvailableMemory">524288</I64>
          <S N="ChassisId">4</S>
          <Sg N="CpuCapacity">44</Sg>
          <S N="DeviceMoId">5c3e2d2238357137782b5b26</S>
          <S N="Dn">sys/chassis-4/blade-4</S>
          <I64 N="FaultSummary">0</I64>
          <S N="Firmware">3.1(25e)</S>
          <S N="Ipv4Address">10.250.140.87</S>
          <Obj N="KvmIpAddresses" RefId="10">
            <TN RefId="6">
              <T>System.Collections.Generic.List`1[[intersight.Model.ComputeIpAddress, intersight, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]</T>
              <T>System.Object</T>
            </TN>
            <LST>
              <Obj RefId="11">
                <TN RefId="7">
                  <T>intersight.Model.ComputeIpAddress</T>
                  <T>System.Object</T>
                </TN>
                <ToString>class ComputeIpAddress {_x000A_  Address: 10.250.140.87_x000A_  Category: Equipment_x000A_  DefaultGateway: 10.250.141.254_x000A_  Dn: sys/chassis-4/blade-4/mgmt/ipv4-pooled-addr_x000A_  HttpPort: 0_x000A_  Https
    Port: 0_x000A_  KvmPort: 0_x000A_  Name: Outband_x000A_  Subnet: 255.255.254.0_x000A_  Type: VnicIpV4PooledAddr_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Category">Equipment</S>
                  <S N="Name">Outband</S>
                  <S N="Type">VnicIpV4PooledAddr</S>
                  <S N="Address">10.1.1.87</S>
                  <S N="DefaultGateway">10.1.1.254</S>
                  <S N="Dn">sys/chassis-4/blade-4/mgmt/ipv4-pooled-addr</S>
                  <I64 N="HttpPort">0</I64>
                  <I64 N="HttpsPort">0</I64>
                  <I64 N="KvmPort">0</I64>
                  <S N="Subnet">255.255.254.0</S>
                </Props>
              </Obj>
              <Obj RefId="12">
                <TNRef RefId="7" />
                <ToString>class ComputeIpAddress {_x000A_  Address: 0.0.0.0_x000A_  Category: ServiceProfile_x000A_  DefaultGateway: 0.0.0.0_x000A_  Dn: sys/chassis-4/blade-4/mgmt/ipv4-prof-addr_x000A_  HttpPort: 0_x000A_  HttpsPort: 0_x0
    00A_  KvmPort: 0_x000A_  Name: Outband_x000A_  Subnet: 255.255.255.0_x000A_  Type: VnicIpV4ProfDerivedAddr_x000A_}_x000A_</ToString>
                <Props>
                  <S N="Category">ServiceProfile</S>
                  <S N="Name">Outband</S>
                  <S N="Type">VnicIpV4ProfDerivedAddr</S>
                  <S N="Address">0.0.0.0</S>
                  <S N="DefaultGateway">0.0.0.0</S>
                  <S N="Dn">sys/chassis-4/blade-4/mgmt/ipv4-prof-addr</S>
                  <I64 N="HttpPort">0</I64>
                  <I64 N="HttpsPort">0</I64>
                  <I64 N="KvmPort">0</I64>
                  <S N="Subnet">255.255.255.0</S>
                </Props>
              </Obj>
            </LST>
          </Obj>
          <S N="MemorySpeed">1866</S>
          <S N="MgmtIpAddress">10.250.140.5</S>
          <S N="Model">UCSB-B200-M3</S>
          <S N="Name">DAL-UCS-3-4-4</S>
          <I64 N="NumAdaptors">1</I64>
          <I64 N="NumCpuCores">20</I64>
          <I64 N="NumCpuCoresEnabled">20</I64>
          <I64 N="NumCpus">2</I64>
          <I64 N="NumEthHostInterfaces">8</I64>
          <I64 N="NumFcHostInterfaces">8</I64>
          <I64 N="NumThreads">40</I64>
          <S N="OperPowerState">on</S>
          <S N="OperState">ok</S>
          <S N="Operability">operable</S>
          <S N="PlatformType">UCSFI</S>
          <S N="Presence">equipped</S>
          <Obj N="RegisteredDevice" RefId="13">
            <TN RefId="8">
              <T>intersight.Model.AssetDeviceRegistrationRef</T>
              <T>System.Object</T>
            </TN>
            <ToString>class AssetDeviceRegistrationRef {_x000A_  Moid: 5c3e2d2238357137782b5b26_x000A_  ObjectType: asset.DeviceRegistration_x000A_}_x000A_</ToString>
            <Props>
              <S N="Moid">5c3e2d2238357137782b5b26</S>
              <S N="ObjectType">asset.DeviceRegistration</S>
            </Props>
          </Obj>
          <S N="Revision">0</S>
          <S N="Rn"></S>
          <S N="Serial">FCH2403KDXU</S>
          <I64 N="ServerId">0</I64>
          <S N="ServiceProfile">org-root/ls-ESXi-348</S>
          <I64 N="SlotId">4</I64>
          <S N="SourceObjectType">compute.Blade</S>
          <I64 N="TotalMemory">524288</I64>
          <S N="UserLabel"></S>
          <S N="Uuid">28882e44-f4e3-11e4-0003-00000000002b</S>
          <S N="Vendor">Cisco Systems Inc</S>
        </Props>
      </Obj>
    </Objs>

    PS D:\>> 
    • Edited by Mr2urbo Friday, October 4, 2019 12:01 AM typo
    Thursday, October 3, 2019 11:57 PM

  • Please only post code using the code posting tool. The answer is what I pointed out above. It is an array of tagged hashes.

    $results.Tags.Where{$_.Key -eq'EOL'}.Value

    $results.Tags | 
        ForEach-Object{
            Write-Host $_.Key ==>> $_.Value
        }
    


    \_(ツ)_/

    • Marked as answer by Mr2urbo Friday, October 4, 2019 3:58 AM
    Friday, October 4, 2019 12:10 AM
  • You can also do this:

    PS C:\scripts> $results.Tags[0].Key
    EOL
    PS C:\scripts> $results.Tags[0].Value
    12/31/2021
    PS C:\scripts>
    


    \_(ツ)_/

    • Marked as answer by Mr2urbo Friday, October 4, 2019 3:57 AM
    Friday, October 4, 2019 12:12 AM
  • jrv that hit the spot!  I was able to work on both foreach-object and the $result.tags[0].key and $result.tags[0].value

    the solution was a combination of recommendations from JS2010 about the [pscustomobject] and jrv above.  I'm not done yet and still some work to do my side.  But for sure I have something I can work.  I really.. appreciate all. I have learn a lot here.

    Thanks all. 



    • Edited by Mr2urbo Friday, October 4, 2019 3:58 AM edit notes
    Friday, October 4, 2019 3:56 AM
  • Wow, this is a strange structure.  I'm not sure how it's handled normally.

    $results = import-clixml results.xml

    # tags is an arraylist of psobjects, with 2 properties, key and value

    #   key                    value
    # 0 EOL                    12/31/2021
    # 1 InService              8/1/2019
    # 2 Depreciated            1/1/2025
    # 3 EOS                    3/28/2021
    # 4 Intersight.LicenseTier Essential

    $results | foreach {
      [pscustomobject]@{
        Name = $_.Name
        Serial = $_.Serial
        Model = $_.Model
        EOL = $_.tags[0].value
        InSerivce = $_.tags[1].value
        Depreciated = $_.tags[2].value
        EOS = $_.tags[3].value
        IntersightLicenseTier = $_.tags[4].value
      }
    }

    • Edited by JS2010 Friday, October 4, 2019 4:34 AM
    • Marked as answer by Mr2urbo Friday, October 4, 2019 4:53 AM
    Friday, October 4, 2019 4:34 AM
  • You cannot guarantee the order of the objects in the array.

    The objects are of type [Deserialized.intersight.Model.MoTag] which defines them as tag type objects with a key and a value.  They are proprietary to the "Intersight" system management software.


    \_(ツ)_/

    Friday, October 4, 2019 4:43 AM
  • Maybe that type has a method to retrieve the value based on the key?  But I can't tell.  Methods can't be serialized.

    • Edited by JS2010 Friday, October 4, 2019 2:45 PM
    Friday, October 4, 2019 4:55 AM
  • Maybe that type has a method to retrieve the value based on the key?  But I can't tell.

    I posted this hours ago. This is how to math the tags.

    $results.Tags.Where{$_.Key -eq'EOL'}.Value


    \_(ツ)_/

    Friday, October 4, 2019 4:57 AM
  • I'm surprised there's not some more efficient way with the original objects.  This doesn't work?  I'm thinking of something like the AttributeIndexer type ('.Meta.Attributes') returned by get-package.  It wouldn't survive being serialized.

    $hostname.results.tags['EOL']

    • Edited by JS2010 Friday, October 4, 2019 4:39 PM
    Friday, October 4, 2019 4:35 PM
  • It's not a single object so that won't work. It is an array of objects of type "MoTag". The type does not have backing code because it was desirialized so no code methods will work. The indexer would be part of the code which doesn't exist on the local system and deserialized objects never have an association with the code unless we cast them to the type (which doesn't exist).


    \_(ツ)_/

    Friday, October 4, 2019 5:21 PM