Table trouble

Welcome Forums General PowerShell Q&A Table trouble

This topic contains 1 reply, has 2 voices, and was last updated by

3 years, 5 months ago.

  • Author
  • #34337

    Topics: 50
    Replies: 86
    Points: 1
    Rank: Member

    Below is some powercli code, I think this is a powershell issue (if not I can find powercli forums).

    It creates several different tables, but when ran as a script the tables do not display correctly, they get progressively worse until it's just display the first column.

    The main issues is displaying $alltemplates, $freespaceDS, $allfolders and $allclusters to output to the console when ran from a .ps1 file.

    function Get-FolderPath{
      [parameter(valuefrompipeline = $true,
      position = 0,
      HelpMessage = "Enter a folder")]
      [switch]$ShowHidden = $false
        $excludedNames = "Datacenters","vm","host"
        $Folder | %{
          $fld = $_.Extensiondata
          $fldType = "yellow"
          if($fld.ChildType -contains "VirtualMachine"){
            $fldType = "blue"
          $path = $fld.Name
            $fld = Get-View $fld.Parent
            if((!$ShowHidden -and $excludedNames -notcontains $fld.Name) -or $ShowHidden){
              $path = $fld.Name + "\" + $path
          $row = "" | Select Name,Path,Type
          $row.Name = $_.Name
          $row.Path = $path
          $row.Type = $fldType
    $start = get-date
    $displayname = Read-host "Enter the VM name as it will appear in vCenter"
    $computername = read-host "Enter the server name"
    [int]$vcpu = Read-host "Enter the number of vCPU sockets"
    [int]$cores = read-host "Enter the number of vCPU cores"
    [int]$numcpus = $vcpu * $cores
    [int]$memMB = Read-host "Enter the RAM in GB"
    [int]$memMB = $memmb * 1024
    #select your template
    $global:counter = 0
    $allTemplates = get-template | select-object @{n='Number';exp={$global:counter; $global:counter++} }, name
    #displays all templates
    $x = read-host "Enter the name of the template you want to use"
    $sourcetemplate = get-template ($allTemplates[$x]).name
    #assigning a cluster
    $global:counter = 0
    $allclusters = get-cluster | select @{n='Number'; e={$global:counter; $global:counter++}}, @{n='Name'; e={$}}
    #all clusters
    $x = read-host "Select a cluster for this VM"
    $cluster = get-cluster ($allclusters[$x]).name
    #Select Datastore Section
    $global:counter = 0
    #creates a table from highest freespace to lowest
    $freespaceDS = get-datastore | ?{$ -notlike "*local*"}|sort-object freespacegb -descending |
    select-object @{n='Number';exp={$global:counter; $global:counter++} },
    name, @{n='FreeSpaceGB'; exp={[math]::round($_.freespacegb,2)}}, CapacityGB
    #actually lists the DS
    $x = read-host "Please select the datastore you would like to use"
    $datastore = ($freespaceDS[$x]).name
    $global:counter = 0
    $allfolders = Get-Folderpath (get-folder | ?{$ -notmatch "datacenters|VM|host"}) | select @{n='Number'; exp={$global:counter; $global:counter++}},@{n='path'; exp={$_.path}}
    $x = read-host "Select a folder to place the VM in"
    $fullpath = $allfolders[$x] | % {$_.path} | % {$_.split("\")}
    $DCname = get-datacenter $fullpath[0]
    foreach($folder in ($fullpath)[1..(($fullpath).length -1)])
    	$dcname = $dcname | get-folder $folder

    when it hits the first table it looks correct:

                                     Number Name
                                     ------ ----
                                          0 Windows 2012 R2
                                          1 Windows_2012_R2
    Enter the name of the template you want to use:

    When it hits the cluster section it loses it's header

                                          0 aaaDCLST02
                                          1 aaaDMZCLST01
                                          2 aaaDCLST03
                                          3 aaaDCLST01
    Select a cluster for this VM:

    And when it hits it's datastore section it loses the freespacegb/capacitygb columns and on the $allfolders it just lists the number column (no header, no other columns).

  • #34340

    Topics: 13
    Replies: 4872
    Points: 1,842
    Helping HandTeam Member
    Rank: Community Hero


    So, here's the thing. When you feed objects into the pipeline, PowerShell has to display them. It has some logic for doing so, which in your case is resulting in tables. That's good. But the display logic can't really cope with _multiple different kinds of objects in the pipeline at the same time_. It barfs, and that's what you're seeing.

    For example, make a simple script, and just put...


    and run it. The first one looks like you'd expect, and the second one falls back to a simpler display mode. PowerShell's really not designed with on-screen "reports" in mind. The formatting system just isn't that sophisticated. There are some supported ways to do what you're after, but they get _really_ complex, and they're not terribly well documented, so you'd be up for a lot of experimenting.

The topic ‘Table trouble’ is closed to new replies.