Automation

 View Only
  • 1.  A faster way to show VM, VM-Folder, and PowerState

    Posted Aug 08, 2018 11:38 PM

    I came across this by LucD and found it extremely usefull

    http://www.lucd.info/2016/06/03/get-inventoryplus-inventory-all-vsphere-objects/

    I have a following 1 liner, it works OK, but its really slow due to secondary "Get-VM" lookup for powerstate

    PS /Documents/vmware/powershell> Get-InventoryPlus | where {$_.Type -eq 'VirtualMachine'} | Select Name,BluePath,@{N="PowerState";E={Get-VM -Name $_.Name | Select PowerState}}

    Name             BluePath                                   PowerState

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

    goblin-pktbot-16 /morgany-sqa/moria/Block3/goblin-pktbot-16 @{PowerState=PoweredOff}

    goblin-pktbot-14 /morgany-sqa/moria/Block3/goblin-pktbot-14 @{PowerState=PoweredOff}

    goblin-pktbot-13 /morgany-sqa/moria/Block3/goblin-pktbot-13 @{PowerState=PoweredOff}

    goblin-pktbot-12 /morgany-sqa/moria/Block2/goblin-pktbot-12 @{PowerState=PoweredOff}

    goblin-pktbot-15 /morgany-sqa/moria/Block3/goblin-pktbot-15 @{PowerState=PoweredOff}

    goblin-pktbot-11 /morgany-sqa/moria/Block2/goblin-pktbot-11 @{PowerState=PoweredOff}

    goblin-pktbot-10 /morgany-sqa/moria/Block2/goblin-pktbot-10 @{PowerState=PoweredOff}

    goblin-pktbot-8  /morgany-sqa/moria/Block1/goblin-pktbot-8  @{PowerState=PoweredOff}

    goblin-pktbot-7  /morgany-sqa/moria/Block1/goblin-pktbot-7  @{PowerState=PoweredOff}

    goblin-pktbot-9  /morgany-sqa/moria/Block2/goblin-pktbot-9  @{PowerState=PoweredOff}

    goblin-pktbot-6  /morgany-sqa/moria/Block1/goblin-pktbot-6  @{PowerState=PoweredOff}

    goblin-pktbot-5  /morgany-sqa/moria/Block1/goblin-pktbot-5  @{PowerState=PoweredOff}

    goblin-pktbot-4  /morgany-sqa/moria/Block0/goblin-pktbot-4  @{PowerState=PoweredOff}

    goblin-pktbot-3  /morgany-sqa/moria/Block0/goblin-pktbot-3  @{PowerState=PoweredOff}

    goblin-pktbot-2  /morgany-sqa/moria/Block0/goblin-pktbot-2  @{PowerState=PoweredOff}

    goblin-pktbot-1  /morgany-sqa/moria/Block0/goblin-pktbot-1  @{PowerState=PoweredOff}

    Also, I'm not sure why the data for "PowerState" is returned as "@{PowerState=PoweredOff}" vs just "PoweredOff"



  • 2.  RE: A faster way to show VM, VM-Folder, and PowerState
    Best Answer

    Posted Aug 09, 2018 04:41 AM

    The Select-Object cmdlet returns a specific type of object, that is why you see the funny output.

    If you only want to see the value of the property, use the ExpandProperty parameter.

    To make you script faster, you can switch to the Get-View cmdlet with the Property parameter.

    And only use the Get-VIBlueFolderPath function.

    Something like this

    function Get-ViBlueFolderPath {

      [cmdletbinding()]

      param(

        [VMware.Vim.ManagedEntity]$Item

      )

      $hidden = 'Datacenters', 'vm'

      if ($Item -is [VMware.Vim.VirtualMachine]) {

        $Item.UpdateViewData('Parent')

        $parent = $Item.Parent 

      }

      elseif ($Item -is [VMware.Vim.VirtualApp]) {

        $Item.UpdateViewData('ParentFolder')

        $parent = $Item.ParentFolder

      }

      if ($parent) {

        $path = @($Item.Name)

        while ($parent) {

          $object = Get-View -Id $parent -Property Name, Parent

          if ($hidden -notcontains $object.Name) {

            $path += $object.Name

          }

          if ($object -is [VMware.Vim.VirtualApp]) {

            $object.UpdateViewData('ParentFolder')

            if ($object.ParentFolder) {

              $parent = $object.ParentFolder

            }

            else {

              $object.UpdateViewData('ParentVapp')

              if ($object.ParentVapp) {

                $parent = $object.ParentVapp

              }

            }

          }

          else {

            $parent = $object.Parent

          }

        }

        [array]::Reverse($path)

        return "/$($path -join '/')"

      }

      else {

        return $NoValue

      }

    }

     

    Get-View -ViewType VirtualMachine -Property Name, Parent, Runtime.PowerState |

      Select Name,

        @{N = 'BlueFolderPath'; E = {Get-ViBlueFolderPath -Item $_}},

        @{N = "PowerState"; E = {$_.Runtime.PowerState}}



  • 3.  RE: A faster way to show VM, VM-Folder, and PowerState

    Posted Aug 16, 2018 09:41 PM

    Thank you @LucD. This is indeed MUCH faster. My last question is, is there a better way to format the output so the titles auto-scale? I tried to pipe to "ft - AutoSize", but it looks the same

    PS /home/morgany/Documents/vmware/powershell> Get-View -ViewType VirtualMachine -Property Name, Parent, Runtime.PowerState |                                              >> Select Name, @{N = 'BlueFolderPath'; E = {Get-ViBlueFolderPath -Item $_}},@{N = "PowerState"; E = {$_.Runtime.PowerState}} | ft -AutoSize

    Name            BlueFolderPath PowerState

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

    sentry-pktbot-1 /morgany-sqa/wilsonville/Block0/sentry-pktbot-1 poweredOff

    sentry-pktbot-2 /morgany-sqa/wilsonville/Block0/sentry-pktbot-2 poweredOff

    sentry-pktbot-3 /morgany-sqa/wilsonville/Block1/sentry-pktbot-3 poweredOff

    sentry-pktbot-4 /morgany-sqa/wilsonville/Block1/sentry-pktbot-4 poweredOff

    sentry-pktbot-5 /morgany-sqa/wilsonville/Block2/sentry-pktbot-5 poweredOff

    sentry-pktbot-6 /morgany-sqa/wilsonville/Block2/sentry-pktbot-6 poweredOff

    sentry-pktbot-7 /morgany-sqa/wilsonville/Block3/sentry-pktbot-7 poweredOff

    sentry-pktbot-8 /morgany-sqa/wilsonville/Block3/sentry-pktbot-8 poweredOff



  • 4.  RE: A faster way to show VM, VM-Folder, and PowerState

    Posted Aug 17, 2018 04:59 AM

    For the result on screen, the Format-Table -AuotSize is the best option.

    Alternatives are to show the result in a grid (Out-GridView, where you can change the column width) or to a file (ConvertTo-Csv).
    Depends a bit on who the consumer of the output will be.