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!
I want to scan my “Synology LUN” datastore and add any virtual machine templates it finds into the in the “_Templates” folder in the cluster named “Lab Cluster 01″.
I prefer to run my scripts out of PowerShell ISE as I feel it gives me more control. I updated the variables (highlighted in yellow) and ran it:
In the Tasks pane of the vSphere Client if you see failed tasks the script found templates that were already in your inventory. The templates that were not in my inventory are now in the “_Templates” folder:
Add VMX (Virtual Machines) to Inventory from a Datastore Cluster:
The $Datastore variable can be changed to scan a Datastore Cluster instead of specific ones:
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 (Templates) to Inventory from Storage Cluster # ############################################################### # Variables: Update these to the match the environment $Cluster = "ENTER CLUSTER NAME HERE" $Datastore = get-datastorecluster "ENTER DATASTORE CLUSTER NAME HERE" | get-datastore $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 } } |
I updated the highlighted variables:
I hope this helps speed up the process if you find yourself in a situation needing to scan and add multiple VM templates to inventory!
Great scripts.
Did a little modification in the last section of Templates script, for my Templates to appear without .vmtx name in inventory:
foreach ($vmtxFile in $unregistered)
{
$TemplateName = $vmtxFile.Name -replace “.vmtx”
New-Template -Name $TemplateName -TemplateFilePath $vmtxFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
}
Thanks.
Very nice!!!