PowerCLI to list Template Info

We are starting to decommission the legacy VMware hosts in my work environment. Normally not a big deal or script for a task like this, but give me enough time I found a need. Templates. So you can easily find templates either using the H5 client or PowerCLI.

VM templates
Get-Template

This cmdlet displays a list of the template VMs that are in the environment, but it doesn’t say where they are located. I had a issue where a template was left on a cluster, all of the hosts were put into maintenance mode, then tried to convert the template to virtual machine. It didn’t like that, it didn’t like that at all. After some troubleshooting, and log reading, I found the template and fixed the problem. But if the cmdlet would have displayed the location information, I would have known from the beginning.

Get-Template | Select *

Using this command, it give you more information about each template. The problem is all of the information is in the form of IDs. So unless you can remember that Host-6293 is the first host in your Non-Production cluster you’ll need to find a way to convert the ID into the host name. So time to mash the keyboard to get what we need.

Get-Template | Select Name, @{N="Host";E={(Get-VMhost -id $_.HostID).Name}}

So now this displays the Name and VMhost that the template calls home. If you want to got a step further, I can add the datastore name.

Get-Template | Select Name, @{N="Host";E={(Get-VMhost -id $_.HostID).Name}}, @{N="Datastore"; E={(Get-Datastore -id $_.DatastoreIDlist).Name -join ","}} | FT -AutoSize

This will return a table with the information to see which VMhost the template is on, along with the datastore name.

-Stuart

3 thoughts on “PowerCLI to list Template Info”

    1. Rahul,
      Sorry for the long delay in my response.
      I have been able to find the creation date by running the following script.

      $templateview = Get-template "TemplateNameHere" | Get-view
      $Templateview.config.ChangeVersion
      

      This should return a date and time like the following:

      2018-05-30T20:44:55.23782Z
      

      I hope this helps.

      -Stuart

  1. Thanks, Stuart. Good find. Of course, technically a template doesn’t have a host, per se, but you have to put it somewhere and that place is a host. So I don’t understand why not just store the host name instead of an ID, but at least there’s a way to get to it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.