Register a LUN full of VMs with PowerCLI

One of the great things about VMware vSphere is virtual machine encapsulation — a VM is simply a directory of files that can be easily copied around by administrators for a variety of purposes.  Most other platforms obfuscate virtual machines beyond recognition.

Recently, I was working on a project that required a large number of VMs that I had used previously but were no longer registered on a VMware ESXi host — they were just waiting patiently on a SAN LUN for their day to come.

Using the vSphere Client to connect the VMFS datastore to a host was a snap.  However, I was not enthusiastic about the several dozen clicks that would have been needed to bring them all back into the inventory for use, so I came up with a simple PowerCLI command to do all the work.

I was so pleased with the power of PowerCLI that I had to share…

Register all virtual machines on a datastore

Connect to vCenter Server with a PowerCLI session and adjust the obvious placeholders to suit your environment:

$esxhost= Get-VMHost
dir 'vmstores:\myvCenterServer@443\DC1\CX4-02-LUN12\*\*.vmx' |
 % {New-VM -Host $esxhost -VMFilePath $_.DatastoreFullPath}

A few moments later, all the VMs are correctly registered and ready for action.

Build a local hosts file with PowerCLI

Another issue I encountered was the lack of DNS for guest operating systems at an offsite lab.  There was no way to connect to the various VMs by name, only IP address — not my favorite approach.  Since vSphere has information about guest networking, I quickly generated a list of IP addresses and VM names in a format suitable for pasting into my local hosts file:

Get-VM | sort-object -Property Name |
 Get-VMGuest | % {$_.ipaddress[0] + "`t" + $_.VMName}

PowerCLI to the rescue again.  What are your favorite PowerCLI one-liners?

(Visited 8,979 times, 1 visits today)
This entry was posted in Virtualizationism and tagged , , . Bookmark the permalink.

13 Responses to Register a LUN full of VMs with PowerCLI

  1. Alan Renouf says:

    Love the local host file trick, thats cool !

  2. Vladan says:

    I like those one liners… Bookmark now. -:) Excellent.

  3. Erick Moore says:

    Just to note, on PowerCLI 4.1, “New-VM -Host” will need to be “New-VM -VMHost”. Very slick!

  4. Pingback: A couple of new things

  5. LucD says:

    Two great and very useful PowerCLI scripts.
    You can find another method to register guests and templates in my post.

  6. Tony says:

    Nice article Eric! I recently did a DR test with VM’s sitting on NetApp NFS replicating to another NetApp at our DR site…Of course VMware shined and the test went flawlessly. I’d like to start scripting the mounting of NFS exports and the registering of VM’s. This script is definitely going into my bag o’ tricks. I love the new SRM, but I’m trying to do things on the cheap.

    Thanks again!

  7. Susan Roter says:

    Worked like a charm. You have a new follower Eric. Thank you.

  8. Sami says:

    What is this ‘vmstores:\’ ??? this is new to me, what should I be googling for ??

  9. Sami says:

    Never mind, I got it now 🙂

    Using ‘Get-VMGuest’ assumes that all vms have VMware-Tool installed, no??

    Great article!!

Comments are closed.