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

  • Scripting
  • Published Aug 1, 2013 Updated Jan 4, 2020

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
}

Comments

Related Posts

Creating Realistic Test User Accounts in Active Directory

Creating Realistic Test User Accounts in Active Directory
When you need to simulate a real Active Directory with thousands of users you quickly find that creating realistic test accounts is not trivial. Sure enough, you can whip up a quick PowerShell one-liner that creates any number of accounts, but what if you need real first and last names? Real (existing) addresses? Postal codes matching phone area codes? I could go on. The point is that you need two things: input files with names, addresses etc. And script logic that creates user accounts from that data. This blog post provides both.
Scripting

Configuring Citrix ShareFile Sync from PowerShell

Configuring Citrix ShareFile Sync from PowerShell
When you have a cloud-based file sharing service it makes a lot of sense to synchronize part or all of the data with your desktop computer. Citrix ShareFile offers the Sync for Windows tool for that purpose. However, once you open its configuration screen you notice that has a severe restriction: it can only synchronize to a single local folder. In many cases it would make much more sense to synchronize different cloud folders to different locations on your hard disk. When I complained to the product manager Peter Schulz about this I learned about a hidden gem: the single folder restriction is only present in the UI; the underlying sync engine is much more flexible. And the best thing is: the sync engine can be configured from PowerShell. Here is how.
Citrix/Terminal Services/Remote Desktop Services

Latest Posts

Fast & Silent 5 Watt PC: Minimizing Idle Power Usage

Fast & Silent 5 Watt PC: Minimizing Idle Power Usage
This micro-series explains how to turn the Lenovo ThinkCentre M90t Gen 6 into a smart workstation that consumes only 5 Watts when idle but reaches top Cinebench scores while staying almost imperceptibly silent. In the first post, I showed how to silence the machine by replacing and adding to Lenovo’s CPU cooler. In this second post, I’m listing the exact configuration that achieves the lofty goal of combining minimal idle power consumption with top Cinebench scores.
Hardware

Fast & Silent 5 Watt PC: Lenovo ThinkCentre M90t Modding

Fast & Silent 5 Watt PC: Lenovo ThinkCentre M90t Modding
This micro-series explains how to turn the Lenovo ThinkCentre M90t Gen 6 into a smart workstation that consumes only 5 Watts when idle but reaches top Cinebench scores while staying almost imperceptibly silent. In this first post, I’m showing how to silence the machine by replacing and adding to Lenovo’s CPU cooler. In a second post, I’m listing the exact configuration that achieves the lofty goal of combining minimal idle power consumption with top Cinebench scores.
Hardware