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

}


VMware and the Zimbra Virtual Appliance Install

Zimbra_logoTM_VMware_LU_RGB Introduction

VMware recently announced the general availability of a Zimbra virtual appliance that VMware customers can simply import into their existing infrastructure and get “e-mail in box”.  This is a great concept for administrators because the operating system is pre-configured and purposefully built for the application that is packaged with it.  The virtual appliance will import into the virtual center management console and will have the standard "OVF” (Open Virtualization Format) file extension for those that are new to virtual appliances.

I am no e-mail administrator, so I wanted to see how easy setting up the Zimbra virtual appliance would be and provide some instructions for those out there that are looking to test out Zimbra.

 

Get the Bits!

First things first, go out and grab the download of the Zimbra virtual appliance by clicking on the icon below.  Yes you will need to register to download the bits…

image

Import the Virtual Appliance

There are two methods of importing a virtual appliance, you can enter the url, which is supplied by the Zimbra website once you register, or you can download the appliance locally and import it locally.  I grabbed the full download in case I hosed something up I would have a copy of the ovf locally so I could start over from scratch.  I guess a snapshot would work as well, so it’s up to you how you would like to proceed here.  Below is a screenshot of the import:

image

Configure the Zimbra Virtual Appliance

The Zimbra virtual appliance is pre-configured to ask you the basic configuration parameters you will need to get the appliance up and running.  You can see below are the questions that you will to answer, pretty common stuff if you a IT administrator.  Make sure you use the FQDN for the hostname.

 

image

Power it up

Now that you have configured your basic system information you can now power up your new virtual appliance.  You can see below that it will automatically configure the appliance based off your information you have previously populated.  Very nice for a hands off approach and a streamlined installation process.

 

image

Finish it off

Now that you are powered up and on the network, you can login to the administration console to finish your configuration.  Point your web browser to https://<hostname>:5480  The administration console will be the place where you can create user accounts, configure licensing information, pull diagnostic data for troubleshooting and update the virtual appliance itself.

DNS is a big component of e-mail.  If you are doing split DNS or Dynamic DNS, I suggest  to reference this link to assist your efforts.  I am using a dynamic DNS service at home along with split DNS, so I had to go and update my host entry with a MX record so the world new where to route my e-mail traffic.  Once that was done correctly I was up and running and able to send/receive e-mails with no problem.

 

image

image

Licensing

The last thing you will want to to is license your installation, the nice folks over at Zimbra will give you a 10 user license free of charge.  Click the link below to go license your configuration or view some sample pricing on what a fully licensed configuration would look like.  Enjoy!

 

image

-Scott