VMware Cloud Community
kevinjj
Contributor
Contributor
Jump to solution

gather template info

get-template cmdlet only provides template name. How to get where the templates are located, time created/updated?

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

That you only see the Name and Id of the templates if you execute a Get-Template cmdlet, depends on the default output formatting of the cmdlet. The cmdlet actualy returns a lot more information. The next script returns the name, vmhost, folder, datastores and changeversion of a template. Because you didn't say which location you wanted, I thought I would show them all. The time that a template was created or modified is not in the template's object. You might get it from the eventlog. There are some post that show you how to do that. The changeversion is the last time the template's configuration has changed. I hope this will be sufficient.

# This script returns information about templates
Get-Template | ForEach-Object {
$Template = $_
  $Report = "" | Select-Object -Property Name,VMHost,Folder,Datastores,ChangeVersion
  $Report.Name = $Template.Name
  $Report.VMHost = (Get-View -Id $Template.ExtensionData.Runtime.Host).Name
  $Report.Folder = (Get-Folder -Id $Template.FolderId).Name
  # Initialize $Datastore as an array
  $Datastores = @()
  $Template.DatastoreIdList | ForEach-Object {
    $Datastores += (Get-View -Id $_).Name
  }
  $Report.Datastores = $Datastores
  # The Changeversion is last date/time the template's configuration has changed
  $Report.ChangeVersion = $Template.ExtensionData.Config.ChangeVersion
  $Report
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
5 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

That you only see the Name and Id of the templates if you execute a Get-Template cmdlet, depends on the default output formatting of the cmdlet. The cmdlet actualy returns a lot more information. The next script returns the name, vmhost, folder, datastores and changeversion of a template. Because you didn't say which location you wanted, I thought I would show them all. The time that a template was created or modified is not in the template's object. You might get it from the eventlog. There are some post that show you how to do that. The changeversion is the last time the template's configuration has changed. I hope this will be sufficient.

# This script returns information about templates
Get-Template | ForEach-Object {
$Template = $_
  $Report = "" | Select-Object -Property Name,VMHost,Folder,Datastores,ChangeVersion
  $Report.Name = $Template.Name
  $Report.VMHost = (Get-View -Id $Template.ExtensionData.Runtime.Host).Name
  $Report.Folder = (Get-Folder -Id $Template.FolderId).Name
  # Initialize $Datastore as an array
  $Datastores = @()
  $Template.DatastoreIdList | ForEach-Object {
    $Datastores += (Get-View -Id $_).Name
  }
  $Report.Datastores = $Datastores
  # The Changeversion is last date/time the template's configuration has changed
  $Report.ChangeVersion = $Template.ExtensionData.Config.ChangeVersion
  $Report
}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
nava_thulasi39
Jump to solution

Hi Robert,

I was looking for the same information.

Still didn't check this script.

But how to look this much indepth parameter - example

Report.VMHost = (Get-View -Id $Template.ExtensionData.Runtime.Host).Name

This might be newbie question, still i would like to learn

Thanks in advance.

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Hi,

this is a good question. When I started to learn PowerCLI I also wondered how this works.

If you look at the output of the Get-Template cmdlet you will see only the Name and Id columns. This is because the default output formatting for this cmdlet only shows you these two properties. However the cmdlet returns more properties, that you can see when you pipe the output of the Get-Template cmdlet to 'Format-List *':

Get-Template | Format-List *

The value in the Id property, is something like 'VirtualMachine-vm-1223'. This is a MoRef or Managed Object Reference. For every MoRef you can get the content of the Managed Object it references using the Get-View cmdlet:

Get-View -Id VirtualMachine-vm-1223

You will see that a template object has a property ExtensionData, as every PowerCLI object does. This contains the Managed Object of the template object. The same object that the above Get-View cmdlet would give you.

A fast way to examine all the properties of a PowerCLI object to a certain depth is using the PowerShell Format-Custom cmdlet. If you do:

Get-Template | Format-Custom * -Depth 5


you will see all the properties of all the templates upto level 5 in depth.

You can also things like:

$Template = Get-Template "MyTemplate"

$Template | Format-List *

$Template.ExtensionData

$Template.ExtensionData.Runtime


to find properties of the object. In $Template.Extensiondata.Runtime.Host you will find something like: 'HostSystem-host-1141'. Because this is a MoRef you can use:

Get-View -Id HostSystem-host-1141


to get more information about the host, like it's name.

I hope this answered your question. If it didn't or if you have more questions, don't hesitate to ask.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
nava_thulasi39
Jump to solution

Hi RvdNieuwendijk,

Great explanation.

Hope, I learned something new today Smiley Happy

If you find this or any other answer useful please consider awarding points by marking the answer correct or helpful
Reply
0 Kudos
kevinjj
Contributor
Contributor
Jump to solution

Hi Bob,

Thanks for your great explanation. The only problem was with datastore. The output was as follows:

Datastores    : {$null}

Reply
0 Kudos