Even if you've always used the GUI of vSphere Client to get your work done, that doesn't mean it's always the right...
tool for the job. Save time and effort with VMware scripts.
IT pros are not immune to the saying, "If all you have is a hammer then everything looks like a nail." If you use the vSphere Client daily to manage virtual machines (VMs) and hosts, that's the tool you'll reach for in any situation. When you need to execute an action across a lot of ESXi hosts or VMs, however, that vSphere Client graphical user interface (GUI) will prove a time-consuming and frustrating tool. Go back to the toolbox and grab the command line interface (CLI).
VMware command line options
CLIs and scripts execute repetitive tasks without a lot of administrator effort. VMware provides a variety of command line tools, so even here you can pick just the right one for the task at hand.
All VMware scripting tools are really wrappers around the vSphere application programming interface (API), a set of Web services provided by vCenter and ESXi servers. The vSphere API is complex and difficult, so these wrappers -- usually called software developer's kits (SDKs) -- let you access the vSphere API from your preferred language. The oldest SDK is Perl, used by vSphere CLI (vCLI) commands to replicate the old ESX Service Console interface. There is also an SDK for Java and a PowerShell snap-in called PowerCLI.
Command line tools are like a chainsaw -- extremely useful, but if you're not careful, they will take your arm off.
VMware pros who spend their days on Windows will gravitate toward PowerCLI as the logical first choice for command line vSphere management. This is a great choice: A very flexible and powerful tool, PowerCLI allows you to work in a command prompt or run scripts. Microsoft created PowerShell to provide system administrators with automation tools, so this isn't just a repurposed developer tool like vBScript. The learning curve is relatively mild, although there are some "aha" concepts that vastly improve your PowerShell productivity. One of PowerCLI's best assets is its strong community, where beginners can learn from the masters and everyone shares solutions. Multiple vendors also embrace PowerShell, so a single script can talk to vSphere, Microsoft Exchange and your storage array.
One use for PowerCLI is report information, particularly in large-scale VMware environments. Reporting on every VM with a CD-ROM drive connected to a physical device or ISO image is a one-line PowerCLI script. The script can even report into an HTML file on a website that's shared with your management. Schedule the script to run regularly and everyone stays informed. Finding the same VM report information with the vSphere Client GUI would take you an hour or more. With the VMware script, execution takes only a few minutes and doesn't require your oversight.
Nonrecurring reports also benefit from a PowerCLI script, such as reports required when scheduling an upgrade to VMXNet3. Reporting every VM with more than two vCPUs or that use the flexible network adapter is much easier with a PowerCLI script than with the vSphere Client GUI. Even if you only need to discover this information once, keep a copy of the command line or script tucked away in case you need a similar report again.
VMware scripts you shouldn't live without
Commit these PowerCLI scripts to memory
Scripts for snapshot consolidation
If you've ever used the vSphere Management Assistant (vMA), then you've used the Perl SDK. All of VMware's vicfg commands are Perl scripts; even esxcli is a Perl script. These commands are often used to do a post-build configuration of new ESXi servers, maybe set up the Pluggable Storage claim rules or tweak advanced settings, such as the scratch and syslog paths. Similar commands aid in fully automated ESXi server builds, bypassing the install wizard with a simple text file.
Another extremely useful VMware command line is ESXi Tech Support mode, the command line on ESXi hosts. Use it to troubleshoot issues on one host; storage and networking problems are sometimes easiest to resolve in Tech Support mode. Use esxtop to get detailed performance information from the host in real time.
Mind your tool set
Command line tools allow you to do bad things just as quickly as good things. A CLI seldom asks, "Are you sure?" I liken command line tools to a chainsaw -- extremely useful, but if you're not careful, they will take your arm off. Consider, for example, a single line of PowerCLI code: Get-VM | stop-VM –confirm:$False. This one line will power off every VM in your whole environment. Do not fear the command line -- just be careful.
While many vSphere administrators will never venture beyond the vSphere Client GUI, there are many tasks that are much better suited to a command line or scripted workflow. Try not to be self-limiting. Using the right tool for the job gets things done right and is far more satisfying.
Dig Deeper on Scripting administrative tasks
Alastair Cooke asks:
What are you using for VMware admin tasks?
0 ResponsesJoin the Discussion