Add Virtual Machine Templates (.VMTX) to Inventory using PowerShell

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:

#####################################################################
#          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″.

VMTX Powershell Script - Environment Variables

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:

VMTX Powershell Script - Ran from ICE

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:

VMTX - vCenter after script ran

 

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:

#####################################################################
#          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:

VMTX Powershell Script - Ran from ICE for Storage Cluster

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!

2 thoughts on “Add Virtual Machine Templates (.VMTX) to Inventory using PowerShell

  1. 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.

Leave a Reply