VMware Cloud Community

Powershell PSObjects converting to HTML output

I have the below functions that work with VAMI via REST API. Everything working correctly with the exception of the output to HTML format.

function Get-VAMISettings


    $Sessions = @()

    $VAMIDNS = @()

    $Output = @()

    #Get Session ID for each VAMI.

for($i=0; $i -le ($VAMIIPs.Count - 1); $i++)


    #Collect all variables needed to get VAMI Session.

    $IP = $VAMIIPs[$i]

    $User = $VAMIUsers[$i]

    $Password = $VAMIPasswords[$i]

    #Call function to get session for VAMI. Store  all VAMI Session IDs in $Sessions Variable

    $Sessions += Get-VAMISession


## Call functions


$Output += $VAMIDNS | ConvertTo-HTML -Fragment -AS Table  -PreContent "<H2>VAMI DNS Servers</h2>" | out-string

Return $Output


function Get-VAMIDNSServer


    $Type = "application/json"

    for($i=0; $i -le ($VAMIIPs.Count -1); $i++)


        $IP = $VAMIIPs[$i]

        $BaseURL = "https://" + $ip + "/rest/"

        $URI = $BaseURL+"appliance/networking/dns/servers"

        $Session = @{

            ‘vmware-api-session-id’ = $Sessions[$i]





             $DNStmp = Invoke-RestMethod -Method Get -Uri $URI -TimeoutSec 100 -Headers $Session -ContentType $Type -SkipCertificateCheck

             $DNSservers = $DNStmp.value.servers


             #Object array for all DNS Servers.

             $DNSObj = New-Object PSObject -Property @{


                    VAMIIP     = $IP

                    DNSServers  = $DNSservers


            }# End Try Statement




            $error[0] | Format-List -Force

            } #End catch statement.


            $VAMIDNS += $DNSObj

    } #End For Loop

    #$VAMIDNS += $DNSObj

    return $VAMIDNS, $DNSObj


When Get-VAMIDNSServers returns $VAMIDNS it output the below as expected in Get-VAMISettings function.

DNSServers                                                     VAMIIP

----------                                                     ------

{**.**.**.225}                                               **.**.**.54

{**.**.**.218, ***.**.**.156, **.**.***.225, **.***.**.227} **.**.**.100

{**.***.**.218}                                                **.**.**.101

{**.**.**.218}                                                **.**.**.102

{**.**.**.218}                                                **.**.**.102


Unfortunately this doesn't translate to html when using "ConvertTo-HTML"

[quote] $Output += $VAMIDNS | ConvertTo-HTML -Fragment -AS Table  -PreContent "<H2>VAMI DNS Servers</h2>" | out-string [/quote]

Above Command gives me "System.Object" instead of DNS servers in table. Below is output for example.

<H2>VAMI DNS Servers</h2>









Anyone have any suggestion on how to get correct output to HTML format?

0 Kudos
1 Reply

That happens because an element in the table you pass to ConvertTo-Html is itself an array.

The cmdlet doesn't know how to convert that to HTML, hence the indication System.Object[] in the HTML.

Btw, I'm not sure where you got that Get-VAMDnsServer function, but the least I can say, it requires some work.

Why do you return 2 objects from that function?

You already stored $DNSObj in $VAMIDNS, so why return it separately?

Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos