Need to help to convert array of strings to html format

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Suresh krishnan Suresh krishnan 1 year, 2 months ago.

  • Author
  • #28972
    Profile photo of Suresh krishnan
    Suresh krishnan

    Hi All
    I have a script when executed produces results like below.

    foreach ($Adminserver in $Adminservers){
    $hypervischeck = Get-BrokerHypervisorConnection -AdminAddress $Adminserver
    $HypervisorStatus  += New-Object -TypeName PSObject -Property @{Sitename = $Adminserver 
                                                  HypervisorName = $hypervischeck.Name
                                                  Status = $hypervischeck.State

    The out put is like below

    Sitename Status HupervisorName {ON,ON,ON} {VC1,VC2,VC3} {ON,ON,ON} {VC4,VC4,VC5}

    When I covert these output to html format I am getting below results , Status and HypervisorName values are showing as System.object.

    Could some one please help me wit this script. Thanks .

    @{; Status=System.Object[]; HypervisorName=System.Object[]; Value=}
    @{; Status=System.Object[]; HypervisorName=System.Object[]; Value=}

  • #28992
    Profile photo of Peter Jurgens
    Peter Jurgens

    Since your Get-BrokerHypervisorConnection is returning multiple objects you should also loop through each of those objects within the for each $adminservers loop.

  • #28993
    Profile photo of Rob Simmers
    Rob Simmers

    The $hypervischeck variable contains an object with each hypervisor. By doing, Powershell v3 and above will do an implicit foreach loop, basically assuming that you want to do:

    $hypervischeck | foreach{$_.Name}

    This creates an array which is the behavior you are seeing. You need to add an explicit for loop to enumerate through each hypervisor:

    $HypervisorStatus = foreach ($Adminserver in $Adminservers){
        $hypervischeck = Get-BrokerHypervisorConnection -AdminAddress $Adminserver
        foreach ($hypervisor in $hypervischeck) {
            New-Object -TypeName PSObject -Property @{Sitename = $Adminserver 
                                                      HypervisorName = $hypervisor.Name
                                                      Status = $hypervisor.State}
        } #foreach $hypervisor
    } #foreach $Adminserver

    Another note, rather than doing $var=@() and then doing $var += New-Object, assign the variable to the for loop and it will be automatically appended into the varaible.

  • #29039
    Profile photo of Suresh krishnan
    Suresh krishnan

    Hi Rob/Peter

    Thanks for taking your valuable time to help 🙂 . I am able to produce the output how I wanted and thanks for the hint 🙂 . Its really helpful and I learned something new.

    I was breaking my head for quite some time to get this working 🙂
    Thanks once again .

You must be logged in to reply to this topic.