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 myhost.example.com
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?

Related posts:

  1. Easy recovery from a full VMware ESX datastore
  2. Using SCVMM to attach ISO images to VMware ESX VMs
  3. PowerShell Prevents Datastore Emergencies
  4. VM Encapsulation
  5. vSphere Thin-Provisioned Disk Performance

Tags: , ,

  1. Alan Renouf’s avatar

    Love the local host file trick, thats cool !

    Reply

    1. Eric Gray’s avatar

      Thanks, Alan. Nice to hear from you.

      Reply

  2. Vladan’s avatar

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

    Reply

    1. Eric Gray’s avatar

      Thanks, Vladan. Got any PowerCLI tricks to share?

      Reply

  3. Erick Moore’s avatar

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

    Reply

  4. LucD’s avatar

    Two great and very useful PowerCLI scripts.
    You can find another method to register guests and templates in my http://www.lucd.info/2009/12/02/raiders-of-the-lost-vmx/ post.

    Reply

    1. Eric Gray’s avatar

      Thanks, Luc. That is quite an awesome script, very flexible.

      Reply

  5. Tony’s avatar

    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!
    Tony

    Reply

    1. Eric Gray’s avatar

      Excellent, thanks for sharing your success.

      Reply

  6. Susan Roter’s avatar

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

    Reply

  7. Sami’s avatar

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

    Reply

  8. Sami’s avatar

    Never mind, I got it now :)

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

    Great article!!

    Reply

Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">