Powershell Scripting using the vmware PowerCLI

Scripting is a bit of an art.  Recently I’ve done some scripting and used the vmware PowerCLI.  I am not a Powershell scripter.  I’ve done lots of bash, Perl, and vbs scripting, but not much Powershell.  Fortunately, after messing around with Syntax, it’s not too hard to do some basic scripting.  I’m nowhere near  a powershell or PowerCLI expert, and fortunately in my role, all I need to do is give my customers “ideas” so that they can continue on with those “ideas”.  With any sort of scripting or development, it is always good to review others techniques which are readily available on the vmware community site for the PowerCLI.  General Powershell examples are also very readily available just by googling (is that a word?) for the specific example that you want to review.  Obviously, any hit that you get may or may not be good examples of technique, but with a bit of discernment and looking at multiple examples you can begin to guess which are the better ways to attack a problem and write very readable and editable scripting code.

To write a basic PowerCLI script isn’t necessarily that difficult, as I mentioned.  I find that the difficult part, is to write a resilient script that can handle any situation or state that might be thrown at it.  I like to offer examples to my customers that can do good error checking and handling or be able to react to different scenerios.  The second example script that I offer below, makes an attempt to do just that.

The first script, however, is a script that references the vSphere API, directly.  Even though the PowerCLI has many cmdlets to handle many vSphere functions, it doesn’t expose the entire vSphere API as cmdlets.  Thus, sometimes it is required to have Powershell work on the API objects, themselves.  This first script, is thus a bit more cryptic than the next two, because there wasn’t a cmdlet available for the function I was trying to perform.  Since it is not as common to operate on the vSphere API, there aren’t nearly as many examples and references.  Lots of trial and error is required if you need to script at this level.   The cmdlet “get-view” allows the Powershell programmer the ability to operated directly on the vSphere API object.  Hopefully, its clear from this script how the “get-view” cmdlet can be very powerful to the Powershell programmer.

So what does it actually do?  The first example script will churn through the vms in inventory, and modify the virtual machine’s settings (checkbox under the options tab of the vmware settings) so that the vms will check for their vmware tools upgrade during a power cycle.  This checkbox is not enabled by default, and is very time consuming and potentially error prone to try and make the change one vm at a time for an environment with lots and lots of vms.  This same script could be modified for any mass vm setting changes that a customer would require.  This script actually doesn’t do any error checking, so I am breaking my own rules of a resilient script, but again, all I needed to provide was an “idea”.

The second and third scripts are related.  They shutown and startup vms based on a specific folder.  This could be modified to work off of a resource pool, or a naming convention depending on your setup.  This particular customer wanted to call a script after and HA event to shutdown or suspend non critical vms so that they would not be capacity constrained after an HA event.  The second script, in particular, tries to react to different vm powered on states or whether vmware tools are even installed.  This is an example of trying to write a resilient script.  Enjoy!


One has to have the PowerClI toolkit installed to run these scripts.

PowerCLI main site (including the download link for the PowerCLI toolkit)
http://www.vmware.com/support/developer/PowerCLI/index.html

The link to the vSphere SDK is below (for those functions that are not available via cmdlets):

http://www.vmware.com/support/developer/vc-sdk/visdk400pubs/ReferenceGuide/index.html

PowerCli Command Reference
http://www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/index.html

Script #1

Connect-VIServer <Ip or HostName> -user <user name> -password <pw>

$vms = get-vm

ForEach($vm in $vms)

{
$vm_view = $vm | get-view
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = “UpgradeAtPowerCycle”
$vm_view.ReconfigVM_Task($vmConfigSpec)
}


Script #2 (my version of shutdown within a specific folder)

Connect-VIServer <Server IP or NAME> -user <userid> -password <password>

$vms = get-vm -Location Test-Folder

ForEach($vm in $vms)

# try a graceful shutdown if vmware tools are installed..could use suspend-VMGuest as an alternative,
# otherwise just stop the vm

{
$vm_view = $vm | get-view
$vmtoolsstatus = $vm_view.summary.guest.toolsRunningStatus

Write-Host “VM $vm says tools status is $Vmtoolsstatus”

if ($vmtoolsstatus -eq “guestToolsRunning”)
{
Shutdown-VMGuest -VM $vm -Confirm:$false
}
else
{
stop-vm -RunAsync -VM $vm -Confirm:$false
}
}


Script #3 (my version of a startup script based on a specific folder)

Connect-VIServer <Server IP or NAME> -user <userid> -password <password>

$vms = get-vm -Location Test-Folder

# Start each vm in the folder

ForEach($vm in $vms)

{
start-vm -RunAsync -VM $vm -Confirm:$false

}


Apple Mac OSX in the cloud?

A colleague of mine, Mark Medovich, turned me on to an interesting solution for Mac users, like me. I am a recently converted Windows user to the MAC. (now for a little over a year) I have to admit, I love my MAC. I still use many Windows applications. I like outlook better than entourage, and the powerpoint presentation still works better with the version in which it was created. Hurray for Fusion… But I digress…. This solution is about running a MAC OS on top of vsphere. What? Really? I thought that wasn’t allowed? Actually, the great community over at DiscCloud have created a way to do so, without actually “running” the MAC OS on vsphere. http://disccloud.ning.com/

You do still have to run the Mac OS on your Mac and you can’t run the MAC OS on a non MAC Hardware. Pretty cool stuff. The way this works, is that it mounts the MAC OS instance as another instance on your mac client. Everything you work on, is stored and secured on the vsphere server. Backups can happne in the cloud.

All of the FAQs are here, including how it doesn’t affect the Apple EULA. http://disccloud.ning.com/page/disccloud-faq

This is a real world example of how the cloud can benefit the average MAC user, like me. This example brings home the how the cloud will eventually be a natural way of how we all will do our computing, in the future. Apple, as we know, has had a resurgence in popularity in the last several years with the iPhone being one of the major drivers. But us with MacBook Pros, have also helped. This is all conusmer/end user driven growth. Not Enterprise driven growth. Actually the MAC causes traditional enterprise IT shops difficutly. This is not unlike of how vmware got its momentum through individual server administrators that were tired of doing work at 2am on Sunday morning. And now, DiscCloud, a group of passionate MAC users and developers have developed a very inovative solution using vSphere and a cloud computing mindset. Here’s to the community, individual and innovation. Now its up to companies like vmware to take those inovations and enhance their ability to be managed within the enterprise.

What will your role be in the cloud?

Chris Everette is a colleague of mine, a Sr. Systems Engineer out of Detroit.  He is a very sharp, seasoned, virtualization industry pro.  In working with him over the past two years, I’ve noticed that he writes very well.  So of course, I’ve been trying to get him to start a blog, or contribute to this one, for quite a while. 

My hard work has paid off :)  It looks like for now he’ll be a guest blogger here on Virtual Insanity.  Whether or not he starts his own blog remains to be seen.  Please welcome Chris Everette to Virtual Insanity!

During VMworld last week, he sent me his first post, which I’m just now getting a chance to post …

What will your role be in the cloud?

Well being at my 3rd vmworld gets me thinking.  I am wondering about this “cloud thing” just like everyone else who has been in IT for a long time.  I sometimes follow other blogger’s articles, and I like Chris Wolf’s writing.  He got me thinking, as well.  His article, titled the “Cloud and the Wal-martification of IT”  struck a chord.  If this Cloud thing really takes off, which by all indications it has and will continue to gain momentum, companies may scratch their heads and wonder if it makes sense to have their own IT assets and resources and IT professionals.  Especially companies where their core business is not IT.  So, what does that mean for IT professionals and particularly my customers?  Are we really going to all get our computing from several large cloud providers and many smaller ones?  Does anyone remember mainframe time sharing? 

Do I think that companies will outsource all of their IT to the cloud in the next year?  Probably not.  Chris Wolf uses the timeframe 5-10 years.  However, will portions be moved to the cloud?  I was speaking with one of my customers and he reminded me that he is already “outsourcing” web filtering and spam filtering to two different providers.  Many companies have their web  presences already “in the cloud”.  Software as a Service has had some bumpy starts and stops, but is now a reality for many types of applications.  It will be an evolution.  Security is still of concern.  There will be companies that will try something, not like it, pull back, only to move again to a model that provides them more flexibility and reduces costs. 

What does this mean for the IT professional?  I believe that if you are working for a company that is not an IT company, that you will want to manage your companies migration to the cloud.  Get out in front of it.  Volunteer to do the research.  Use it to further your own knowledge and career.  Can you convert your IT department from a cost center to a profit center and be a cloud provider for other types of similar businesses?   If all else fails, take your expertise to a cloud company.  The exciting thing is in the future you can work for a company that will service many different types of customers and provide you many interesting job challenges.  I believe cloud providers (since their business is IT) will be on the forefront of exciting and new technology as well as need the best and brightest to operate them.  Do an inventory of your skills.  Do you understand databases?  Great, cloud providers need to manage many databases (if not for their customers) for their own internal systems for billing, monitoring, reporting, etc.  Do you understand networking and security?  Cloud providers are going to need to guarantee that data cannot bleed from one customer to another.  Same for storage.  And obviously, also for virtualization. 

We call this concept of being able to support multiple customers on the same infrastructure as multi-tenancy.  Since a cloud provider’s model is multiple customers on the same infrastructure (“cloud”), it better be secure.    Is your expertise Enterprise Applications, messaging, development?  Guess what, cloud providers need application expertise as well so they can meet the business needs of their customers.  If you are a developer you need to be able to write applications that are “cloud aware”.  Applications that can ask for more resources if they need them.  Applications that may service more than one customer.  Do you understand service management?  Do you work with infrastructure services such as backup and recovery, business continuity or data center design?  Do you work for an IT provider, already?  Your customers of the future may change to include a mix of large “cloud providers”.    They exist, today and are called many things.  They may provide Hosting (infrastructure as a service), application services (Software as a service) or multiple platforms including items such as  storage (Platforms as a service) and even voice services.   I am sure there are some cloud “thinkers” that may challenge my simple definitions of these terms, but you get the idea.

I believe the security concern is the single largest inhibitor to companies running full tilt into cloud computing.  However, just like we use VPNs, SSL web services, and other forms of digital encryption of data on the public internet, these security problems will get solved and enforced.

IT is an interesting business.  Sometimes things build up until there is a tidal wave and things change rapidly.  Sometimes things move more slowly and there is an evolution.  However, if you hear the term “cloud” and think it is only a marketing term, you may want to think again.  It is already affecting and will affect all of us in IT sooner than we may think.

What do you think?