Site icon Grzegorz Kulikowski

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:
[sourcecode language=”powershell”]
SYNTAX
Get-Template [-Location ]

-Location
Specifies the vSphere container objects (such as folders, datacenters, and clusters) you want to search for templates.
[/sourcecode]
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
[sourcecode language=”powershell”]
$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 } }
[/sourcecode]
Result:
[sourcecode language=”powershell”]
Name VMs Templates
—- — ———
cluster=1 3
Cluster-2 4 2
cluster-3 1 6
[/sourcecode]
You can do some checks if you want:
[sourcecode language=”powershell”]
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} } }
[/sourcecode]
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.