How to check template settings without converting it to vm using get-teamplate and get-view?

So if we will look at a template vm using vSphere client we can’t see more than basic settings. We can check the guest OS, vm version, cpu, memory, vmware tools status, state, on which host was it registered, and on which datastore it resides. That’s it.
Ok, so in order to get some more information from template we can use get-view.

get-template vm2
-----
Name                Id
----                --
vm2            VirtualMachine-vm-738

Is this all ? Let’s see what else can we see using this view id

get-view -id (get-template vm2).id
-------
Capability           : VMware.Vim.VirtualMachineCapability
Config               : VMware.Vim.VirtualMachineConfigInfo
Layout               : VMware.Vim.VirtualMachineFileLayout
LayoutEx             : VMware.Vim.VirtualMachineFileLayoutEx
Storage              : VMware.Vim.VirtualMachineStorageInfo
EnvironmentBrowser   :
ResourcePool         :
ParentVApp           :
ResourceConfig       : VMware.Vim.ResourceConfigSpec
Runtime              : VMware.Vim.VirtualMachineRuntimeInfo
Guest                : VMware.Vim.GuestInfo
Summary              : VMware.Vim.VirtualMachineSummary
Datastore            : ...
Network              : {}
Snapshot             :
RootSnapshot         : {}
GuestHeartbeatStatus : gray
LinkedView           :
Parent               : Folder-group-v..0
CustomValue          : {..}
OverallStatus        : green
ConfigStatus         : green
ConfigIssue          : {}
EffectiveRole        : {-1}
Permission           : {}
Name                 : vm2
DisabledMethod       : {MakePrimaryVM_Task, TerminateFaultTol
RecentTask           : {}
DeclaredAlarmState   : {}
TriggeredAlarmState  : {}
AlarmActionsEnabled  : True
Tag                  : {}
Value                : {507}
AvailableField       : {}
MoRef                : VirtualMachine-vm-738
Client               : VMware.Vim.VimClient

So as you can see we can see similar output as if you would apply get-view to normal virtual machine. From here you can query your template just as it would be virtual machine. What is worth mentioning here is that it will be impossible to make any tasks on the template, but still we can get information.
So let’s check for example what network interfaces our templates has:

$templateview=get-view -id (get-template vm1).id
($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualVmxnet]})|%{$_.DeviceInfo}
--------
Label                      Summary
-----                      -------
Network adapter 1          VM Network

So we can see that we have 1 network adapter which is connected to ‘VM Network’ portgroup.
Now let’s see how were the disks configured

($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualDisk]})|%{$_.DeviceInfo}
---------
Label                         Summary
-----                         -------
Hard disk 1                   1,048,576 KB
Hard disk 2                   1,048,576 KB

Now again, we create some templates and we try to remind “have i put this floppy device or not ?” So normally you would have to mark template as a vm, then edit settings and so on..
Let’s check if i have put floppy to template:

($templateview.config.hardware.device | ? {$_ -is [vmware.vim.VirtualFloppy]})|%{$_.DeviceInfo}
--------
Label                                        Summary
-----                                        -------
Floppy drive 1                               Remote
Floppy drive 2                               Remote

Wow! Not one but two! 😉
Ok few times i have seen situation when people were surprised that some of vms have memory reservations/limit on them and they were wondering how ?! “i did not set it up”
Let’s see if we have some memory limits or reservations

$templateview.ResourceConfig.MemoryAllocation
----------
Reservation           : 100
ExpandableReservation : False
Limit                 : -1
Shares                : VMware.Vim.SharesInfo

Well it seems that my template has 100 mb memory reserved, and no limit set. You can query cpu allocation as well:

$templateview.ResourceConfig.CpuAllocation
----------
Reservation           : 0
ExpandableReservation : False
Limit                 : -1
Shares                : VMware.Vim.SharesInfo

What tools upgrade policy will my vms have after deployment ? We can also check this :

$templateview.config.tools.ToolsUpgradePolicy
----------
upgradeAtPowerCycle

Nice… And all this information AND MORE! we can grab without converting template to vm. So if you want really quickly take a look on some specific property of your template , get-view will help you out 😉

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.