If you want to generate a quick inventory of all VMware Virtual Machine Templates created in your vCenter environment you can use PowerCLI command-lets and pull that data quickly.
Today I am sharing a script which helped me a lot as I have several vCenters to support globally and each one has several Templates for different OS Platforms/OS versions etc. With this script I can run it against all vCenters and generate a csv file with required data. Usually, I review those to check for any old or obsolete Template, that I need to remove and create a new one or in case if someone has created a new Template, which I should be aware of.
I will be using vCenter Name and Output file path as input in this script and assign values for those. As I always say, feel free to update this script and pass those as parameters.
Here is the complete script. Let me know if this is helpful.
$oFile = "<Some Path>\allTemplateDetails.csv" #Output file to store script run result
$vCenterName = "<myvcenter>.<somedomain>.<com>" # vCenter FQDN
# Check if Output file exists.If exists, delete it and create a new file. You can set the file name unique with date-time stamp if you want generate unique file every time
if(Test-Path $oFile){
Remove-Item -Path $oFile -Force
}
"vCenter Name,Template Name,Template Path" | Out-file $oFile -Append -Encoding ASCII
connect-viserver $vCenterName #Connect to the vCenter Server.If you have multiple vCenters, enter a for-loop to loop through them
# Filter out view result only for Templates
Get-View -ViewType virtualmachine -Filter @{'Config.Template'='TRUE'} | ForEach-Object{
$templateName = $_.Name
$currentPath = Get-View $_.Parent
$templatePath = $_.Name
#traverse through the path and find out the logical path of the template in vCenter
do{
$parentPath = $currentPath
if($parentPath.Name -ne "vm"){
$templatePath = $parentPath.Name +"\"+ $templatePath
}
$currentPath = Get-View $currentPath.Parent
}while($currentPath.Parent -ne $null)
"$($vCenterName),$($templateName),$($templatePath)" | Out-File $oFile -Append -Encoding ASCII
}
disconnect-viserver $vCenterName # disconnect from vCenter