Add Virtual Machines (.VMX) to Inventory using PowerShell

There are quite a few clicks to add a virtual machine from a datastore to inventory. If you were to consolidate an environment and had to add multiple virtual machines this would be quite a manual task. Easier solution? Script it! The below script makes scanning a datastore and registering all virtual machines with vCenter a breeze.

You must have VMware PowerCLI installed in order to have the cmdlets required for PowerShell to run the script.

Add VMX (Virtual Machines) to Inventory from a Datastore:

Let’s see it in action!

I want to scan my “Synology LUN” datastore and add any virtual machines it finds into the in the “_Script Testing” folder in the cluster named “Lab Cluster 01”.

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

2 VMX Powershell Script - Code in ICE ran

In the Tasks pane of the vSphere Client you will see failed tasks if the script finds virtual machines that are already in your inventory. The virtual machines that were not in my inventory are now in the “_Script Testing” folder:

3 VMX Powershell Script - - Tass and Results in vCenter

Add VMX (Virtual Machines) to Inventory from a Datastore Cluster:

The $Datastores variable can be changed to scan a Datastore Cluster instead of specific ones:

I updated the highlighted variables:

4 VMX Powershell Script - Code in ICE ran for Storage Cluster

I hope this helps speed up the process if you find yourself in a situation needing to scan and add multiple VMs to inventory!

7 thoughts on “Add Virtual Machines (.VMX) to Inventory using PowerShell”

  1. Hi there, 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

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

    Reply
    • It does not like your $Datastores variable, is the DataStore1 in a Storage Cluster? You can edit the $Datastores variable to select a specific datastore like this:

      $Datastores = “DataStore1”

      Reply
    • I ran into a similar issue and it was because in my entire vcenter, which spans MANY datacenters, there was more than one storage LUN with the same name.

      Reply
    • Line 23 narrows down the data from what Line 20 created. I had the variable incorrect, it was $Datastores and should have been $Datastore (without the s). Remove that s and try again

      Reply
  2. Hi i have an issue in case of multiple vmdk for a vm not on same directory as vmx file, is there a way to resolve path for all vmdk properly ?

    Reply
  3. Hi Daniel,
    thank you for sharing this.
    I have difficulties on understanding why you add this line of code
    %{$_.FolderPath + ($_.File | select Path).Path}

    Why you used a “where” clause and then you did not compare anything?

    Hope you can help

    Regards

    Reply

Leave a Reply