Script: Add all Virtual Machines in the Data Store to the Inventory (VMware)

There are situations when you have VMs on disk, but vCenter does not know about them – i.e. they are not in the inventory. While it is fairly easy to add a single machine to the inventory, doing so for dozens or hundreds or VMs is too tedious to do manually. This script automates the process.

#
# Traverses all folders of a data store and adds all VMX files to the inventory
#
# Example:
#
# .\Add-VMXToInventory.ps1 -DatastoreName My_Datastore -VMFolder VDI -vCenter vcentername -ESXHost host.domain.com
#
 
param
(
   [Parameter(Mandatory=$true)] [string] $DatastoreName,		# Case sensitive
   [Parameter(Mandatory=$true)] [string] $VMFolder,			
   [Parameter(Mandatory=$true)] [string] $vCenter,
   [Parameter(Mandatory=$true)] [string] $ESXHost
)
 
#
# General options
#
#Requires -Version 2 
 
Set-StrictMode -Version 2
 
#
# Functions
#
# Loads snapins
function LoadSnapins([string[]] $snapins)
{
   $loaded = Get-PSSnapin -Name $snapins -ErrorAction SilentlyContinue | % {$_.Name}
   $registered = Get-pssnapin -Name $snapins -Registered -ErrorAction SilentlyContinue  | % {$_.Name}
   $notLoaded = $registered | ? {$loaded -notcontains $_}
 
   if ($notLoaded -ne $null)
   {
      foreach ($newlyLoaded in $notLoaded)
	  {
         Add-PSSnapin $newlyLoaded
      }
   }
}
 
 
#
# Start of script
#
# Load snapins
LoadSnapins @("VMware.VimAutomation.Core")
 
# Avoid stupid questions
Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
 
# Connect to vCenter
Connect-VIServer $vCenter
 
# Set up search for .VMX files in datastore
$ds = Get-Datastore -Name $DatastoreName | %{Get-View $_.Id}
$SearchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$SearchSpec.matchpattern = "*.vmx"
$dsBrowser = Get-View $ds.browser
$DatastorePath = "[" + $ds.Summary.Name + "]"
 
# Find all .VMX file paths in datastore, filtering out ones with .snapshot (useful for NetApp NFS)
$SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec) | where {$_.FolderPath -notmatch ".snapshot"} | %{$_.FolderPath + ($_.File | select Path).Path}
 
#Register all .vmx files as VMs on the datastore
foreach($VMXFile in $SearchResult)
{
   New-VM -VMFilePath $VMXFile -VMHost $ESXHost -Location $VMFolder -RunAsync
}

,

6 Responses to Script: Add all Virtual Machines in the Data Store to the Inventory (VMware)

  1. Roy February 17, 2014 at 14:37 #

    Helge,
    Thank you for this script! We had 16 servers loose their VMs during an upgrade. Not sure why they were unregistered. This script has saved me HOURS of work!

    Thanks again
    -Roy

  2. Robin June 16, 2015 at 15:49 #

    Hi,

    I think the part “%{$_.FolderPath + ($_.File | select Path).Path}” is missing a forwards slash, and should be “%{$_.FolderPath + “/” + ($_.File | select Path).Path}”. That’s what I had to do, to get this working (at least under vSphere 6).

    Regards,

    Robin

  3. Jeff June 18, 2015 at 20:59 #

    Great script! Thanks a ton! The script as is worked fine for me.

  4. Ranvir October 27, 2015 at 18:00 #

    Hi there,
    thanks for the script … n Though its quite an old post…but i was trying to use this script in my environment and I am getting below error

    =================================================================================================
    Exception calling “SearchDatastoreSubFolders” with “2” argument(s): “Invalid datastore path ‘[DataStore1 DataStore1 DataStore1 DataStore1]’.”
    At C:\Users\Administrator\Desktop\Automation Scripts\Inventory.ps1:27 char:1
    + $SearchResult = $dsBrowser.SearchDatastoreSubFolders($DatastorePath, $SearchSpec …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException

    -==============================================================================================

    could you help me to sort this out please?

  5. Jose Pulido June 14, 2017 at 07:14 #

    Great script! Thanks a lot! It’s fantastic!

Leave a Reply