I’m not sure if Stephen Campbell is a fan of the 2001 cinematic tour de force Not Another Teen Movie, but he has been keeping busy lately writing a bunch of useful PowerShell scripts, including:
Keep ’em coming, Stephen!
I’m really excited about the VI Toolkit Extensions as a way for us to make the common problems we see on our forums really easy to do. Just today, a user wanted to find all files on his datastores that contain the word "delta" (which can indicate a snapshot that hasn’t been cleaned up properly). As it turns out, with the extensions this is just a one-liner. The art of PowerShell really seems to be the art of the one-liner and with the extensions there are already a lot of great one-liners you can use.
We’ve got more than 10 cmdlets in the extensions now, and it’s starting to get really useful. Here’s a brief tour of what we’ve got so far:
One of the really great new things in the extensions is the ability to copy files to and from datastores. None of this would have been possible without the help of Stephen Campbell of Marchview Consultants Ltd. Stephen wrote an amazing example of actually compiling C# code within a PowerShell script. With this code, Stephen was able to create a .NET-based web client that would accept self-signed certificates, which is critical for enabling file uploads to ESX hosts. With the new Copy-TkeDatastoreFile cmdlet you can do some really amazing stuff.
Here’s an example you can use to move VMs from one ESX host to another without having to rely on VirtualCenter. This example assumes that each ESX server has a datastore called datastore1 and that there’s a VM called MyVM that is to be moved.
<p>$server1 = connect-viserver server1<br />$server2 = connect-viserver server2<br />copy-tkedatastorefile `<br /> -source "[datastore1] MyVM/MyVM.vmdk" `</p>
<p> -sourceServer $server1 `<br /> -sourceDatacenter (get-datacenter -server $server1) `<br /> -destination "[datastore1] MyVM/MyVM.vmdk" `</p>
<p> -destinationServer $server2 `<br /> -destinationDatacenter (get-datacenter -server $server2)</p>
The cmdlet can also accept arbitrary URLs as either sources or destinations. Here’s an example of copying an Openfiler virtual appliance directly to your ESX host:
<p>copy-tkedatastorefile `<br /> -source <a href="http://s3.amazonaws.com/rbuilder/openfiler/%2F16315%2Fopenfiler-2.3-x86.esx.tar.gz">http://s3.amazonaws.com/rbuilder/openfiler/%2F16315%2Fopenfiler-2.3-x86.esx.tar.gz</a> `<br /> -destination "[YourDatastore] OpenFiler/Openfiler.tar.gz" `<br /> -destinationDatacenter (get-datacenter)</p>
If you combine that with a little more scripting it’s fairly easy to have a one-liner that can create iSCSI storage on your ESX host, something we’ll hopefully have in the extensions before too long. Neither of these examples even require a password, since they rely on connections already being established through the Connect-VIServer cmdlet.
Anyway, there’s lots of great stuff we have planned for the extensions, so be sure to stay tuned. If you’ve got a feature you want to see, ask us. To get started with the extensions you need PowerShell 2 CTP2 as well as the VI Toolkit. Have fun!
In his blog, Mike DiPetrillo shows us another example of both the power of the VMware API and the ease of use we get with the VI Toolkit for Windows. His script will move a VM from one ESX host to another, without making use of VMware VMotion. The drawback is that the VM becomes unresponsive for a period of time (usually a few seconds). This is roughly functionally equivalent to Microsoft’s Quick Migration feature, except that this was done in 130 lines of script.
I’ve traded a few emails with Mike on the subject so I can add one thing that Mike doesn’t mention in his blog. Mike has quite a lot of industry experience and has done a lot of custom development like this before. However, until recently Mike had never seen the PowerShell or the VI Toolkit at all, yet he still was able to put this script together in the span of roughly a weekend. So I’m not trying to imply that Mike’s not a smart guy but there’s no question in my mind that the simplicity of the VI Toolkit made it possible for this really interesting script to get developed in record time.
Mike’s script asks you to log into VirtualCenter, then prompts for a source and destination host, as well as a VM to move. After a few sanity checks the VM is suspended, moved and un-suspended. That’s all there is to it. You can do this process manually through the VI Client or automatically with Mike’s script. With a couple of tweaks to Mike’s script, you wouldn’t even need VirtualCenter to make all of this happen.
Great work, Mike!