Home > PowerCLI, PowerShell, vSphere > Dutch VMUG event 2009 – handy scripts

Dutch VMUG event 2009 – handy scripts

December 11th, 2009 Leave a comment Go to comments

Today the Dutch VMUG event 2009 took place. It was my first visit but this truly is an amazing event. Lots of knowledgeable and interested visitors.

During my session I showed some small, handy scripts that I use on a regular base. In fact they are stored in my profile.

As promised, here are the scripts.


Since I can never remember for which vSphere Managers the Get-View cmdlet accepts a shortcut, I wrote the following.

function Get-GVShortcuts{
	Write-Host "Shortcuts supported by Get-View" -ForegroundColor yellow
	$si = Get-View ServiceInstance
	$si.content | gm -MemberType Property | % {
		if($_.Name -match "Manager"){
			$t = Get-View $_.Name -ErrorAction SilentlyContinue
			if($t -ne $null){
				Write-Host $_.Name
			}
		}
	}
}

Annotation

Line 6: use the -ErrorAction SilentlyContinue to suppress all error messages

It produces the following output

Get-View-shortcuts

Another script that comes in handy from time to time is this little script.

In only 10 lines of PowerShell code it recursively prints out the hierarchy of your vSphere environment.

filter Get-AllViObjects{
	$children = @()
	Write-Host $global:offset $_.GetType().Name " - " $_.Name
	$global:offset += "-"
	if($_.ChildEntity){$children += $_.ChildEntity}
	if($_.datastoreFolder){$children += $_.datastoreFolder}
	if($_.hostFolder){$children += $_.hostFolder}
	if($_.networkFolder){$children += $_.networkFolder}
	if($_.vmFolder){$children += $_.vmFolder}
	$children | % {Get-View $_ | Get-AllViObjects}
	$global:offset = $global:offset.Substring(1)
}

It produces the following output when you feed it the rootFolder.

vSPhere-hierarchy

Note that the hierarchy also shows all the “hidden” vSphere folders (Datacenters, datastore, host, network, vm).

  1. joeschmoe
    June 30th, 2010 at 22:29 | #1

    I have been trying to reverse-engineer your script without much luck (don’t really know PS that well). I am trying to get something that returns only the datastore folders, not the volumes (child objects). Is this possible?

    • July 1st, 2010 at 23:54 | #2

      Not 100% sure what you want, but if it is anything like this

      Folder – Datacenters
      - Datacenter – DC1
      – Folder – datastore
      — Datastore – DS2
      — Datastore – MyDS
      — Datastore – ds1
      — Datastore – ds2
      — Datastore – esx41-local
      — Datastore – esx42-local
      — Datastore – localvmfs

      then you just have to comment out a couple of lines in the script

      filter Get-AllViObjects{
      $children = @()
      Write-Host $global:offset $_.GetType().Name " - " $_.Name
      $global:offset += "-"
      if($_.ChildEntity){$children += $_.ChildEntity}
      if($_.datastoreFolder){$children += $_.datastoreFolder}
      # if($_.hostFolder){$children += $_.hostFolder}
      # if($_.networkFolder){$children += $_.networkFolder}
      # if($_.vmFolder){$children += $_.vmFolder}
      $children | % {Get-View $_ | Get-AllViObjects}
      $global:offset = $global:offset.Substring(1)
      }
      $si = Get-View ServiceInstance
      Get-View $si.content.Rootfolder | Get-AllViObjects

  2. December 12th, 2009 at 15:09 | #3

    Two great scripts indeed. Thanks for mentioning my website during your session.

  3. December 12th, 2009 at 00:00 | #4

    2 very cool and handy scripts

  1. December 11th, 2009 at 23:57 | #1