When Value contains multiple values convertto-html will return system.object[] and not value
what i'm trying build function pull networking values esxi hosts. function works correctly, when try convertto-html values contain more 1 entry returns result system.object[]
function get-vmnetworking { param ( [parameter(valuefrompipelinebypropertyname=$true)]$name ) $i = 0 $output = @() $itemcount=$input.count $input | foreach { write-progress -activity "getting networking information" -status $_ -percentcomplete (($i++ / $itemcount) * 100) $temp = (get-vmhost $_ | get-vmhostnetworkadapter ) $object = new-object psobject $object | add-member noteproperty "host" $_ $object | add-member noteproperty "mgmt ip" ($temp | ? {$_.managementtrafficenabled -eq $true} | select-object -expandproperty ip) $object | add-member noteproperty "mgmt mtu" ($temp | ? {$_.managementtrafficenabled -eq $true} | select-object -expandproperty mtu) $object | add-member noteproperty "vmotion ip" ($temp | ? {$_.vmotionenabled -eq $true} | select-object -expandproperty ip) $object | add-member noteproperty "vmotion mtu" ($temp | ? {$_.vmotionenabled -eq $true} | select-object -expandproperty mtu) $object | add-member noteproperty "storageip" ($temp | ? {$_.managementtrafficenabled -eq $false -and $_.vmotionenabled -eq $false -and $_.faulttoleranceloggingenabled -eq $false} | select-object -expandproperty ip) $object | add-member noteproperty "storagemtu" ($temp | ? {$_.managementtrafficenabled -eq $false -and $_.vmotionenabled -eq $false -and $_.faulttoleranceloggingenabled -eq $false} | select-object -expandproperty mtu) $output += $object } # end each loop $output } # end of set-vmnetworking
running command
get-vmhost | sort-object name | get-vmnetworking
will return result
mgmt ip : 192.168.100.24
mgmt mtu : 1500
vmotion ip : 192.168.102.24
vmotion mtu : 1500
storageip : {192.168.101.24, 192.168.101.27}
storagemtu : {1500, 1500}
as can see results storage ip , storage mtu contained within brackets because have multiple values.
if convertto-html
$frag0 = get-vmhost | get-vmnetworking | convertto-html -as table -fragment -precontent '<h2>networking information</h2>' | out-string
convertto-html $style -postcontent $frag0 | out-file $output\file.html
the outputted html file replace multiple values system.object[]
any appreciated.
walter
convertto-html lot export-csv in produces flat table, , doesn't along data structures contain arrays. either add code flatten out objects piped convertto-html (turning, example, single storageip property has multiple ips several properties named storageip_1, storageip_2, etc, or converts single storageip property string instead of array), or roll own code produce html output other simple table (instead of using convertto-html).
here's example of modifying code produce comma-separated strings rather arrays, regardless of how many objects in results:
$object | add-member noteproperty "host" $_ $object | add-member noteproperty "mgmt ip" (($temp | ? {$_.managementtrafficenabled -eq $true} | select-object -expandproperty ip) -join ', ') $object | add-member noteproperty "mgmt mtu" (($temp | ? {$_.managementtrafficenabled -eq $true} | select-object -expandproperty mtu) -join ', ') $object | add-member noteproperty "vmotion ip" (($temp | ? {$_.vmotionenabled -eq $true} | select-object -expandproperty ip) -join ', ') $object | add-member noteproperty "vmotion mtu" (($temp | ? {$_.vmotionenabled -eq $true} | select-object -expandproperty mtu) -join ', ') $object | add-member noteproperty "storageip" (($temp | ? {$_.managementtrafficenabled -eq $false -and $_.vmotionenabled -eq $false -and $_.faulttoleranceloggingenabled -eq $false} | select-object -expandproperty ip) -join ', ') $object | add-member noteproperty "storagemtu" (($temp | ? {$_.managementtrafficenabled -eq $false -and $_.vmotionenabled -eq $false -and $_.faulttoleranceloggingenabled -eq $false} | select-object -expandproperty mtu) -join ', ')
Windows Server > Windows PowerShell
Comments
Post a Comment