Get-Template : 7/12/2012 4:48:28 PM Get-Template Could not find VIContainer with name ‘ xxx ‘.


I wanted to see all templates that reside in a particular cluster. I was very surprised that get-template can not handle the cluster as a -Location parameter.
I have double,triple check help for get-template:

SYNTAX
    Get-Template [-Location ]

-Location 
    Specifies the vSphere container objects (such as folders, datacenters, and clusters) you want to search for templates.

From get-help get-template it is clear that it should be able to handle clusters as well.
Well, yeah, it can’t 😉
Whenever you are doing get-template -Location “cluster-name” you are thinking that you are checking templates in a cluster but in reality you are checking for templates in datacenter AND folder that has the same name as the cluster-name you have given.
What to do in this case ?
To print table with all clusters in VC and columns : VMs, Templates

$clusters=(get-view -viewtype virtualmachine -Filter @{"Config.Template"="True"})| %{$_.Summary.Runtime.Host|Get-VIObjectByVIView|get-cluster|sort -property name} | Group-Object -Property name
get-view -viewtype clustercomputeresource | select name, @{N="VMs";E={(get-view -viewtype virtualmachine -SearchRoot $_.moref -Filter @{"Config.Template"="False"}).Count} }, @{ N="Templates";E={$clname=$_.name; ($clusters | ? {$_.Name -eq $clname}).Count } }

Result:

Name      VMs Templates
----       --- ---------
cluster=1  3
Cluster-2  4      2
cluster-3  1      6

You can do some checks if you want:

get-cluster | %{ $clname=$_.name; $_.name;get-template -Location $_.name -ea silentlycontinue| select folderID, @{N="folderName";E={(get-view -id $_folderid).name} }, @{N="CL name";E={$clname} }, @{N="template name";E={$_.name} } }

You will see that the folder name where the template is registered is a child or directly the main folder, which has the same name as the cluster. If you see different folder name than the cluster name, it means that this folder is a child of the folder with name as the ‘cluster’ you have given in -Location parameter.
You can browse though templates ignoring child items using the -NoRecursion switch. Without this parameter all templates will be listed.

Advertisements

2 thoughts on “Get-Template : 7/12/2012 4:48:28 PM Get-Template Could not find VIContainer with name ‘ xxx ‘.

  1. Here is a one liner.

    Get-View -ViewType VirtualMachine -Filter @{“config.template”=”true”} | Select Name, @{N=”cluster”; e={%{$_.Summary.Runtime.Host|Get-VIObjectByVIView|get-cluster}}} | Format-Table -AutoSize

    • Hi Caleb, thanks for the reply. The post was to explain how the get-template might be confusing sometimes. As it goes for your 1 liner, it’s ok. Have a look at the below comparison of your 1 liners to 2 examples of my 1liners. Your 1liner execution time , 1:34min , my approach without hashing 0:44, with hashing 0:02 😉

      PowerCLI C:\> measure-command {$out2= Get-View -ViewType VirtualMachine -Filter @{“config.template”=”true”} | Select Name, @{N=”cluster”; e={%{$_.Summary.Runtime.Host|Get-VIObjectByVIView|get-cluster}}}}

      Minutes : 1
      Seconds : 34
      Milliseconds : 648

      PowerCLI C:\> measure-command {$out = Get-View -ViewType VirtualMachine -Filter @{“config.template”=”true”} -property ‘config.template’,’name’,’runtime.host’| select name, @{n=’host’;e={(get-view -id (get-view -id ($_.runtime.host) -property ‘parent’).parent -property ‘name’).name}}}

      Minutes : 0
      Seconds : 44
      Milliseconds : 322

      PowerCLI C:\> Measure-command { $cl2h=@{};$clh=@{};$clusters = get-view -ViewType clustercomputeresource -property ‘name’,’host’;foreach ($cluster in $clusters ) { ($cluster.host |%{$cl2h+= @{$_.ToString()=$cluster.moref} }) }; $clusters| % {$clh+= @{$_.moref = $_.name}};Get-View -ViewType VirtualMachine -Filter @{“config.template”=”true”} -property ‘config.template’,’name’,’runtime.host’| select name, @{n=’cluster’;e={ $clh[$cl2h[$_.runtime.host.toString()]] }} }

      Minutes : 0
      Seconds : 2
      Milliseconds : 874

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s