Home > Uncategorized > VMware ESX: Quick script for VM management

VMware ESX: Quick script for VM management

This article will expand over time.

I decided today that I was going to stop messing with the vSphere Client, and start doing all my work with VMs through the command line interface presented through SSH.

I soon realized that some of the interface is not so nice. I really want to bang out commands with tab completion, in luie of no true console like interface. I figured I’d throw together (and expect to expand) some bash scripts for ease of use.

After switching user context with su, the first thing anyone who wants to do the most simple of tasks (such as start a VM) realizes is that you must parse the following command to find the Vmid of the desired VM then feed it into another command:

vim-cmd vmsvc/getallvms

In order to just have the Vmid returned, it’s quite simple:

echo "vim-cmd vmsvc/getallvms | grep \$1 | awk '{print \$1}'" > /bin/vmidbyname
chmod +x /bin/vmidbyname
vmidbyname [name of VM]

There are tons of functions revealed by vim-cmd vmsvc, but to generate a few very simple ones, such as power on, restart, power off, we will create a quick bash script for these functions, which is sort of just like an alias…

echo "if [ \$1 == help ]; then" > /bin/vmpower
echo "vim-cmd help vmsvc | grep power | awk '{print \$2}'" >> /bin/vmpower
echo "else" >> /bin/vmpower
echo "vim-cmd vmsvc/power.\$1 \$2" >> /bin/vmpower
echo "fi" >> /bin/vmpower
chmod +x /bin/vmpower

check out vim-cmd help vmsvc.

Advertisements
Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: