powershellinfobox

PowerShell - extracting nested object


I am fairly new to PowerShell and am trying to use a module called PoshWAPI. This module is used to query Infoblox. One of the commands is Get-IBobject which allows me to query infoblox for a record type.

This is my code so far:

Set-IBConfig -ProfileName 'MyGrid' -WAPIHost dns.example.com -WAPIVersion latest -Credential (Get-Credential) -SkipCertificateCheck
$result = Get-IBObject -ObjectType record:host 'name=host1.network.example.com' -ReturnAll

That works well and returns the following

_ref                 : record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnRtY3MuaW5mb3NlYy5zMGE0OTFlMDAtMjMudDkwNC5zb25lc3N1czAx:host1.network.example.com/default
allow_telnet         : False
comment              : TOSD-37248
configure_for_dns    : True
ddns_protected       : False
disable              : False
disable_discovery    : False
dns_name             : host1.network.example.com
extattrs             : 
ipv4addrs            :{@{_ref=record:host_ipv4addr/ZG5zLmhvc3RfYWRkcmVzcyQuX2RlZmF1bHQudG1jcy5pbmZvc2VjLnMwYTQ5MWUwMC0yMy50OTA0LnNvbmVzc3VzMDEuMTAuNzMuMzAuMTYu:10.73.30.16/host1.network.example.com/default; 
                   configure_for_dhcp=False; host=host1.network.example.com; ipv4addr=10.73.30.16}}
name                 : host1.network.example.com
network_view         : default
rrset_order          : cyclic
use_cli_credentials  : False
use_snmp3_credential : False
use_snmp_credential  : False
use_ttl              : False
view                 : default
zone                 : network.example.com

The info I actually require from there are the name field and within the ipv4addrs field is a field called ipv4addr.

Currently I export it all to CSV using this command

$result | Export-Csv -Path C:\Users\neil.bloyce\Documents\2023\2.Feb\InfoBlox.csv

It isn't elegant but gets some of the info I want, however within the ipv4addrs I don't get any info except for System.Object[].

How do I adjust my code so that I can pull the ipv4addr out of ipv4addrs so I can see it in the CSV, Ideally the only info I actually need is dns_name and ipv4addr.

I am not sure how I attach the CSV file.


Solution

  • Use the Select-Object cmdlet to copy and transform only a subset of the properties from the object:

    $result |Select-Object Name,@{Name='Addresses';Expression={ $_.ipv4addrs.ipv4addr -join ', ' }} |Export-Csv -Path C:\Users\neil.bloyce\Documents\2023\2.Feb\InfoBlox.csv -NoTypeInformation
    

    Here, we extract the Name property from the original object, and then create a new calculated property that extracts the nested ipv4addr values and joins them together in a single string (thus causing Export-Csv to render the value correctly).