Here is a script that will scan a datastore for VM templates (.VMTX) and add them to inventory. This is a modified version of the Add Virtual Machines (.VMX) to Inventory script.
You must have VMware PowerCLI installed in order to have the cmdlets required for PowerShell to run the script.
Add VMTX (Virtual Machine Templates) to Inventory from a Datastore:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
##################################################################### # Load VMware Plugins and vCenter Connect # ##################################################################### Add-PSSnapin vmware.vimautomation.core connect-viserver -server ENTER VCENTER FQDN HERE ##################################################################### # Add .VMTX (VM Templates) to Inventory from Datastore # ##################################################################### # Variables: Update these to the match the environment $Cluster = "ENTER CLUSTER NAME HERE" $Datastore = "ENTER DATASTORE NAME HERE" $VMFolder = "ENTER FOLDER NAME HERE" $ESXHost = Get-Cluster $Cluster | Get-VMHost | select -First 1 foreach($Datastore in get-datastore $Datastore) { # Collect .vmtx paths of registered VMs on the datastore $registered = @{} Get-Template -Datastore $Datastore | ForEach-Object -Process { $registered.Add($_.Name+'.vmtx',$true) } # Set up Search for .VMTX Files in Datastore(s) $null = New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' $unregistered = @(Get-ChildItem -Path TgtDS: -Recurse | ` Where-Object -FilterScript { $_.FolderPath -notmatch '.snapshot' -and $_.Name -like '*.vmtx' -and !$registered.ContainsKey($_.Name) } ) Remove-PSDrive -Name TgtDS #Register all .vmtx Files as VMs on the datastore foreach($vmtxFile in $unregistered) { New-Template -Name $vmtxFile.Name -TemplateFilePath $vmtxFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync } } |
Let’s see it in action!