VM Deployment with PowerCLI Part Two

In my last post I presented you with the main deployment script I have been using. What it did not include are all the little tweaks and prerequisites that allow it to work.

The first thing we need before running the script is a Template. As previously mentioned, I built a vm from the corporate standard ISO. I then applied updates and added some post deployment scripts to do things we couldn’t do with NEW-VM or the customization specification.

Here are the files I used. Some consolidation could be made and your mileage may vary:

This simple .bat file is called by the customization specification. It is calling two other powershell scripts, installing sccm client and restarting the vm to finish up the installs.

The first file called is:

This file sets the SNMP properties, updated DNS servers, disables IPv6, adds domain suffixes and renames the local guest account

The next file:

This file adds our AD admin group to the local administrators group on the vm. It also renames the local Administrator account. You may notice the calling of the legal.reg file. This was needed because we have a legal message displayed after login, and it was causing us to have to interact with each vm to get it through the customization process. We disabled the legal notice in the template, and reinstate it using the registry key.

Here it is, masked for business sensitive information, of course:

Now that we have talked about all the scripts and their functions, lets move on to the last, yet very important piece: The customization specification.

I created one specifically for this build process as I needed to customize it a bit more than we were doing normally. I needed credentials with domain joining ability, the proper domain specified in the CS and adding pre.bat to the run once field.

As you can see, not terribly complicated. Definitely room for improvement. And, with the addition of some actual automation tools, could become much more powerful. This was simply my way to reduce busy work within the confines of our environment. Please feel free to use this, pick it apart or send me beer if you really liked it :)

VM Deployment With PowerCLI part one

Tedious, repetitive processes are the number one factor in deciding whether to write a new script. In my current position, we are asked to deploy new VMs on a pretty regular basis and in some cases it may be upwards of 20 in one batch! The standard way we accomplish this is through the following steps:

1. Create a new VM using the vSphere client, setting the general parameters
2. Attach the vendor created ISO that will do all the customization to the selected OS. (this step takes 30-60 minutes)
3. Set SNMP properties
4. Set IP and DNS settings
5. Disable IPV6
6. Add DNS search suffixes
7. Rename local Admin and Guest accounts
8. Install SCCM 2007 Client
9. Add our AD security group to the Local Administrators group
10. Install Cisco AMP

As you can see, there are a lot of steps in the original build process. For one off VMs, this might be acceptable. However, when you need 24 VMs for a big project, it could easily take several work days of your time. This is when my distaste for repetitive, manual processes comes into play.

The first thing that comes to mind, is how much of this can be streamlined through PowerCLI. As it turns out, most of it! Now, having no official automation tools or even support from within to accomplish this task, I had to improvise. This means there are a few factors other than PowerCLI in play. I have a .CSV file containing all the fields necessary to complete the build. Batch files and small Powershell scripts to do some customization. Custom VM Templates and Customization Specifications. As you can see, quite a few tools being used to achieve our modest goals.
The .csv file contains the following fields:

Name – Name of the VM
Template – Name of the Template we are using
Cluster – Which Cluster are we putting this VM in
Datastore – What Datastore should we use
Customization – This is the Customization Specification
vCPU – Number of vCPUs
Memory – Amount of memory
Network – Network the VM should be on
Diskformat – Thick or Thin
Location – Which folder the VM should be in
ipaddress – The IP address
mask – Subnet mask
gateway – Default Gateway
dns1 – DNS server
dns2 – DNS server
disk1 – Disk one size
disk2 – Disk two size (if no disk two, 0 or leave blank)

The .CSV file has the above names as the first set of values. So the next step in the new process is: create/edit the .CSV, in my case, it is named NEWVMS.CVS

Now we are ready to start the actual script.

First we need to get the data from the .CSV

Something I decided to add was a time/date stamp and who built the VM, so we need to get the username of the person running the script (another thing you may consider adding is a project name or sponsor.

Now we get to the fun stuff, importing the data and assigning it to variables

One more step before we build the VM, Set the IP, subnet mask, default gateway and DNS servers in the Customization Specification. If we don’t, we cannot have the VM join the domain using the guest customization routines.

Now we can finally call the New-VM cmdlet and build the VM

Now we will set the VM name, Amount of Memory, Number of CPUs and the Network the Primary NIC will be on

the last few steps are add the second disk (if it exists), set vm built date and builder in the notes field and start the VM!

At this point the VM build process is complete. But wait! What about the other steps? I will cover those in detail in my next post. for now, here is the complete script:

VMware tools and PowerCLI part 1

Recently I noticed a few servers did not have VMware tools installed. Needing to see how big of a problem this was, I turned to a few old standbys: RVtools and PowerCLI.

Of course, RVtools was able to provide the info relatively quickly, it was cluttered with extraneous information. I could have chosen to pair down the data by sorting and removing lines, but that seemed like work.

PowerCLI to the rescue!

Having not done this particular task in PowerCLI, the first thing to do was determine how to get the info. A couple of searches later and the VMware tools status was found in the extension data of get-vm: ExtensionData.Guest.ToolsVersionStatus

Great, now to use that data:

So, a relatively simple one liner to find the VMs with no tools installed. Not being one to stop there and putting google to use once again, I found an article that suggested using New_VIProperty to make it easier to understand and use in the future.

Here is the same thing except we are using New-VIProperty to add some clarity and ease to the process:

The New-VIProperty cmdlet is very useful! While certainly not needed in the above examples, it gives us building blocks for more complex scripts. Stay tuned for more on VMware tools and PowerCLI.

Cleaning up files with powershell GUI confirmation

After automating my report last week, I decided I did not need a copy left lying around, so I decided to write a cleanup script.

My first version was very simple, and consisted of just the required code to recurse the directory and delete the files.

It occurred to me that a Yes/No MsgBox to confirm the deletion would be a nice addition. So without further adieu, here is the code.

And here is the output

MsgBox output from script

See how nice that looks? You can use all the Windows GUI Framework in your scripts. I will be designing a script next week that uses several parts of the windows framework to make the script more end user friendly. Keep reading to see my progress and learn how to apply these techniques to your script!

Report Automation with RVtools and Excel

Recently I needed to extract some configuration info from vCenter and email it someone on a semi-regular basis.

They wanted specific information about the hosts and VMs in each vCenter. I figured the easiest way to get the required info was to use Rob de Veij’s excellent RVTools.

To start, I ran RVTools, exported the host tab and the vm tab for 3 vCenters. Not the most efficient way this could be done. So, I made a simple batch file to output the info I wanted.

Now I was left with 6 spreadsheets full of data, most of which I do not need. Again, initially, I deleted the extraneous columns manually. Manual processes are tedious, boring and uninspired… Powershell to the rescue!

Since I had two different sets of output (vms and hosts) I needed two separate scripts to process them (yes I could have combined them, but I really like making things modular) The only difference is in line 2 (columns to keep), so I have only listed one here.

All these scripts do are remove the columns I do not need. A simple loop gets all the files from an array and processes them.

The only thing left is to email the modified Excel files to the person who requested the information. Why not automate that as well? And since we could add or subtract a vCenter, why not have the script e-mail every spreadsheet it finds in the output directory?

With all the pieces in place, the last step is to tie all these disparate scripts together. I created a control script that executes all the other scripts and displays the status as it goes.

And there you have it! All completely automated and no more work anytime I need the report sent.

Something I have considered changing is getting the list of files to process. I could read the directory and put them into the array automatically. Maybe next time…

I’d like to thank all the people who’s code I borrowed (who I promise to credit once I go back though my history and find them)

My vMetrics

Total Metrics:
44 GHz Total CPU
64 GB Total RAM
3,247 GB Total Disk
2 Host(s)
1 RPs
21 VMs
7 vMotions
Power States:
(3) (18) (0)
8 Physical NICs
8 Virtual PGs
DRS: DRS enabled (Full Auto)
HA: Highly Available
Current Deviation: 0.038
Target Deviation: 0.2
DRS Balance: Load Balanced
HP ProLiant DL360 G5

previous refresh
1357 days, 5 hrs and 31 min.

Recent Comments