Backup VMs with PowerShell and PowerCLI.

PowerCLIIn my lab, I back up my VMs by exporting OVA templates and saving the files on a NAS device. My typical routine involves powering down the VM, exporting an OVA template, waiting about 30 minutes for the export to complete, powering it back up, rinse and repeat. After a few backup iterations, I found this process to be a chore that I occasionally put off. Today, I decided to automate the task and pay down some principle on that technical debt.

I’m fairly comfortable in PowerShell and some time ago I downloaded VMware’s PowerCLI. I decided to give it another test drive and knock out a real world problem. The first hurdle was how to load the modules. The PowerCLI loads via a snap-in. Therefore, we can add the snap-in and view the available cmdlets thusly.

PowerShellPowerCLI

Okay, great! We have several hundred cmdlets and they have fairly decent naming conventions. Now let’s repro my manual workflow in code. First, I need to connect to a host and get a list of all my VM’s.

PowerShellPowerCLI2

Next, I need to power down running VMs and bring them back online after backing them up.

The last thing I need is to export the OVA to my NAS device. The command we will need is Export-VApp. (I prefer to power down the VM before running this.)

Finally, let’s pull it all together and backup all the VMs on the host. This is the completed script.

While it is possible to use Connect-VIServer without having to manually enter credentials, I’ll leave that step up to you. Google “PowerShell credentials SecureString” for more information.

Ideas for the next step? Addressing the possible infinite loop would be wise if you don’t want to periodically check your backup job. Backing up the ESX Host OS and configuration is important especially if your environment is complex. Adding some logging for backup date, VM size, and other metrics would be easy and valuable. You could send “Successful” or “Failed” email alerts if you’re okay with a little more spam. Let me know what you come up with.

Try backing up your VMs today. Tomorrow you might be glad you did.

Arduino Memory Management.

Arduino UnoI had been playing with my Arduino Uno for about 3 months when I decided I needed a real project to put my skills to the test. My first real project was to web enable my garage door. I planned to use the Ethernet Shield to host a simple web page. However, during the development I started getting strange behavior. Only the first portion of my web page was being served. Not yet knowing my problem was related to memory, I tried in vain to comment out different parts of my code to find an elusive bug. After a bit of frustrating troubleshooting, I discovered that my problem wasn’t a code bug, it was memory based.

As it turns out, the ATMega328 chip for my Arduino Uno has 3 kinds of memory…

ATMega328 Memory
Type Amount Description
SRAM 2 Kb RAM for your program to run
EEPROM 1 Kb Can be used as durable storage
FLASH 32 Kb This is what you upload your sketch to

SRAM is the static random access (i.e. volatile) memory for your sketches. The variables you create and the Libraries you use can quickly eat up this limited memory resource. To get an idea of how small 2 Kb is, generate a 2 Kb Lorem Ipsum.

Since I was using three libraries, Ultrasonic.h, SPI.h, and Ethernet.h, I suspected I might have a problem there. I loaded up the following simple sketch to see what my base memory footprint was before writing any code.


I examined the SRAM consumption and found I had only consumed about 273 bytes (~15%) of the SRAM. Therefore, the memory consumption from the libraries I was using was not an issue. Next, I started adding the code for my simple web page to see if something I was doing there was problematic.


After adding the above code, and examining the SRAM consumption, I was starting to understand where my memory problems were coming from. The strings in my println functions were being consumed by SRAM even though they were static. At runtime, such strings are copied from FLASH to SRAM. I could tell, at my current rate of SRAM consumption, my sketch wasn’t going to cut it. By this point, I wasn’t yet reading incoming data from submitting the form and I was already consuming 1236 bytes (~60%) of the available SRAM. Also, I still needed to add code for using the ultrasonic sensor to read if the garage door was up or down and inject that into the HTML.

Luckily, I discovered the F() function for leaving static strings in FLASH memory instead of copying them to SRAM. The F() function basically tells your Arduino not to copy the strings into SRAM. Rather, a pointer is used to read the strings from FLASH. Since every character in my HTML consumes a byte of memory, this trick saves a lot of SRAM. The following demonstrates the kind of changes I needed to make.

After making this change, my SRAM memory consumption was only about 400 bytes (21%).

This simple trick works for static strings in sketches but what if you wanted declare a variable in a sketch and not have that variable copied to SRAM at runtime?

PROGMEM is a variable modifier specifically for this purpose. In my case, I had a global constant that was being used by my sketch. Using the PROGMEM variable modifier, I was able to keep the variable value in FLASH and out of SRAM.

It’s worth noting that, in my case, I’m really only saving myself a few bytes of SRAM with the above variable. But the example illustrates that you can keep variables in FLASH to preserve what precious little SRAM you are limited with.

The last form of memory on the ATMega328 is EEPROM (1 Kb). This form of memory works like durable storage. Values stored in EEPROM are not volatile and persist even if the Arduino loses power. You can store one byte in EEPROM by supplying the memory address and the byte value. Then you can retrieve the byte by supplying the address. Therefore, it’s not practical to try to optimize SRAM memory consumption by saving data in EEPROM. The real benefit of using EEPROM is data durability.

For completeness, it’s worth mentioning that the data write/erase cycle lifespan is 100,000 for EEPROM, 10,000 for FLASH, and basically unlimited for SRAM. Therefore, choosing the appropriate memory location for volatile data would be prudent so that you don’t prematurely wear out the chip.

There is a great memory optimization series on Adafruit’s web site. Check out Memories of an Arduino for more information. Also, there is a good tutorial on Arduino.cc that covers the basics.

Monitoring Host System Temperature with Powershell.

FireI recently made some custom modifications to an old ESX virtual host to limit the noise produced by the CPU fans. After injecting resistors into the CPU fan circuits, I wanted to monitor the system temperature in case it ever exceeded the normal operating range. On my laptop, I prototyped a temperature monitoring script using WMI and PowerShell.

Everything was working beautiful until I tried scheduling the script on one of my VMs. The problem is, guest VMs know very little about the underlying host that they are running on. Since VMs can be migrated from host to host without interruption, it’s pointless for the OS to ask “What’s my temperature?” What I should have done, instead, is have the guest OS ask “EsxHost1, what is your temperature?” I admit, I had to laugh, when I realized the problem because I should have seen it coming. Back to the drawing board I went.

After my initial SNAFU, I abandoned WMI because it wasn’t going to work against ESX. Next, I decided to try VMware’s PowerCLI. Unfortunately, after a 200MB download and an hour of digging around, host temperature was nowhere to be found. Then I discovered VMware’s CIM interface. CIM is an open standard, similar to WMI, that allows you to control and manage hardware. PowerShell 3.0 has some new Cmdlets that improve the user experience of working with CIM. After a little Googling I found the class “CIM_NumericSensor” which contains, among other things, “Ambient Temp”.


Using the above script, I can remotely ask an ESX host for its system temperature; so far, everything has been “cool”. If you need to monitor your host from PowerShell 2.0, check out the cmdlet “New-WSManInstance”. Carter Shanklin wrote a post entitled “Monitoring ESX hardware with Powershell” on blogs.vmware.com. It should get you going in the right direction.