0

I'm doing a daily check of Hyper-V VM's and i get the output as required in HTML format. However, there is always room for improvement in html formatting and i need assistance with the following problem.

I have the following Array

$outputArray = @()
foreach($VM in $VMS) { 
      $VMsRAM = [math]::round($VM.Memoryassigned/1GB)
      $VMsCPU = $VM.processorCount
      $VMsState = $VM.State
      $VMsStatus = $VM.Status
      $VMsUptime = $VM.Uptime
      $VMsAutomaticstartaction = $VM.Automaticstartaction
      $VMsIntegrationServicesVersion = $VM.IntegrationServicesVersion
      $VMsReplicationState = $VM.ReplicationState
      $VHDsGB = @{ label="File_Size"; Expression={[math]::round($_.FileSize/1GB)}}
      $VHDs = Get-VHD -ComputerName $VM.ComputerName -VMId $VM.Id | Select Path, VHDType, VHDFormat, $VHDsGB

      $output = new-object psobject
      $output | add-member noteproperty "VM Name" $VM.Name
      $output | add-member noteproperty "RAM(GB)" $VMsRAM
      $output | add-member noteproperty "vCPU" $VMsCPU
      $output | add-member noteproperty "State" $VMsState
      $output | add-member noteproperty "Status" $VMsStatus
      $output | add-member noteproperty "Uptime" $VMsUptime
      $output | add-member noteproperty "Start Action" $VMsAutomaticstartaction
      $output | add-member noteproperty "Integration Tools" $VMsIntegrationServicesVersion
      $output | add-member noteproperty "Replication State" $VMsReplicationState
      $output | add-member noteproperty "VHD Path" $VHDs.Path
      $output | add-member noteproperty "Size GB" $VHDs.File_Size
      $output | add-member noteproperty "VHD Type" $VHDs.vhdtype
      $output | add-member noteproperty "VHD Format" $VHDs.vhdformat
      $outputArray += $output
 }

Next, I throw the output using html formatting

#Export contents to htm
if($outputArray -ne $null) {
    $HTML5 = '<style type="text/css">
    #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
    #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
    #Header th {font-size:14px;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
    #Header tr.alt td {color:#000;background-color:#EAF2D3;}
    </Style>'
    $HTML5 += "<font face=verdana size=4 color=#23B108><b><CENTER>VMGuest Tech Spec's</CENTER></b></font>"
    $HTML5 += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 width=100% id=Header>
        <TR>
            <TH><B>VM Name</B></TH>
            <TH><B>RAM(GB)</B></TD>
            <TH><B>vCPU</B></TD>
            <TH><B>State</B></TD>
            <TH><B>Uptime</B></TD>
            <TH><B>Integration Tools</B></TD>
            <TH><B>Replication State</B></TD>
            <TH><B>VHD Path</B></TD>
            <TH><B>Size GB</B></TD>
            <TH><B>VHD Type</B></TD>
            <TH><B>VHD Format</B></TD>
        </TR>"

Foreach($Entry in $outputArray){
    $HTML5 += "<TR>
            <TD>$($Entry.'VM Name')</TD>
            <TD>$($Entry.'RAM(GB)')</TD>
            <TD>$($Entry.vCPU)</TD>
            <TD>$($Entry.State)</TD>
            <TD>$($Entry.Uptime)</TD>
            <TD>$($Entry.'Integration Tools')</TD>
            <TD>$($Entry.'Replication State')</TD>
            <TD>$($Entry.'VHD Path')<BR></TD>
            <TD>$($Entry.'Size GB')</TD>
            <TD>$($Entry.'VHD Type')</TD>
            <TD>$($Entry.'VHD Format')</TD>

        </TR>"

}

The above code works fine. However, "$($Entry.'VHD Path')
" results in multiple path's as there are multiple virtual disk's & this clutter;s my report. Now, My question is how do i format each output into a separate line inside the table.

Thank you for your help in advance.

2
  • Do I understand correctly that $Entry.'VHD Path' is an array? (or possibly just a comma-separated string...?) Commented Jan 8, 2014 at 7:30
  • Yes, Its part of the array...Within the array i'm giving labels for each value that im fetching. The following gathers all the path info with other information. $VHDs = Get-VHD -ComputerName $VM.ComputerName -VMId $VM.Id | Select Path, VHDType, VHDFormat, $VHDsGB Only the path element $VHDs.Path is now been given a Label $output | add-member noteproperty "VHD Path" $VHDs.Path Finally in the HTML formatted output im using the Label name <TD>$($Entry.'VHD Path')<BR></TD> Commented Jan 8, 2014 at 7:44

1 Answer 1

1

You can use $OFS variable (http://technet.microsoft.com/en-us/library/hh847796.aspx). It specifies the character that separates the elements of an array when the array is converted to a string.

For example :

$OFS = "`n"
$array = Get-ChildItem $env:USERPROFILE
Write-Host "$array"

This will output array elements separated by NewLine character instead of whitespace.

In your case you will need to use $OFS = "<br />"

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect..Works like a charm. Thank you Levgen :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.