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

by Grzegorz Kulikowski

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.

You may also like

2 comments

Caleb January 18, 2017 - 10:47 pm

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

Reply
psvmware January 23, 2017 - 11:09 am

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

Reply

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish
Streaming live on Twitch right now.
CURRENTLY OFFLINE