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?
Love the local host file trick, thats cool !
Thanks, Alan. Nice to hear from you.
I like those one liners… Bookmark now. -:) Excellent.
Thanks, Vladan. Got any PowerCLI tricks to share?
Just to note, on PowerCLI 4.1, “New-VM -Host” will need to be “New-VM -VMHost”. Very slick!
Pingback: A couple of new things
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.
Thanks, Luc. That is quite an awesome script, very flexible.
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
Excellent, thanks for sharing your success.
Worked like a charm. You have a new follower Eric. Thank you.
What is this ‘vmstores:\’ ??? this is new to me, what should I be googling for ??
Never mind, I got it now 🙂
Using ‘Get-VMGuest’ assumes that all vms have VMware-Tool installed, no??
Great article!!