Automation

 View Only
  • 1.  Powershell PSObjects converting to HTML output

    Posted May 21, 2020 06:53 PM

    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


    $VAMIDNS = Get-VAMIDNSServer

    $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]

                }

       

                Try

                {

                 $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

                Catch

                {

                $_.Exception.ToString()

                $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

    [/quote]

    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>

    <table>

    <colgroup><col/><col/></colgroup>

    <tr><th>VAMIIP</th><th>DNSServers</th></tr>

    <tr><td>********.54</td><td>System.Object[]</td></tr>

    <tr><td>**********.100</td><td>System.Object[]</td></tr>

    <tr><td>*************101</td><td>System.Object[]</td></tr>

    <tr><td>***********102</td><td>System.Object[]</td></tr>

    </table>

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



  • 2.  RE: Powershell PSObjects converting to HTML output

    Posted May 21, 2020 07:05 PM

    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?