By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
|Eric Siebert, Contributor|
Scripting tools help manage a VMware environment by automating common administration tasks. But some VMware scripting tools, such as Perl, come with a learning curve. Fortunately, each VMware Infrastructure Toolkit (VI Toolkit) release features increased PowerShell integration. Because many VMware admins are also Windows admins (and are familiar with PowerShell), VMware scripting is now more accessible. So let's review what PowerShell is and how it can work with VMware. I'll also help you start using PowerShell with your own VMware environment.
PowerShell is a command-line shell and associated scripting language developed by Microsoft to help automate common administration tasks. It can also provide information about your VMware environment. In the past, admins have used PowerShell to script different tasks in a Windows environment. The latest versions of the VI Toolkit provide PowerShell with access to the VMware API, which enables admins to use PowerShell to execute VMware-related commands. Many VMware administrators are also Windows system administrators, so VMware admins interested in scripting may find PowerShell easier to learn than other scripting languages such as Perl. PowerShell is fairly easy to install and use, and there are many useful scripts available for VMware environments.
The first step is to install PowerShell on your workstation. PowerShell is available as a small (less then 2 MB) download from Microsoft's website. Once you've downloaded the file, you can install it on your workstation. A shortcut is then added to the PowerShell command prompt, which is where you run PowerShell scripts. The PowerShell command prompt looks similar to a MS-DOS command prompt, and all standard MS-DOS commands work in it (i.e., dir, cd) in addition to PowerShell scripts and commands.
Before we install the VI Toolkit, let's try a few basic PowerShell commands. Open the PowerShell command prompt and type
This command will return a list of all running processes. Next, try typing
This command returns your computer's BIOS information.
PowerShell scripts are a series of commands. They have a .ps1 extension and are referred to as cmdlets. For security purposes, the PowerShell execution policy is set to Restricted by default. This setting prevents cmdlets from running, so you need to change it. To do so, open a PowerShell prompt and type
This returns the current setting (Restricted). Next, type
to change the setting from Restricted to RemoteSigned, which enables scripts to run locally but prevents them from running remotely. You can also set this to Unrestricted or AllSigned, but these settings are less secure. Both Windows PowerShell commands such as
get-ExecutionPolicy and get-Process
and VMware PowerShell commands, such as
are also referred to as cmdlets. You can see a list of Windows cmdlets by typing
You can get help on specific cmdlets by typing
Get-Help <cmdlet name>.
Installing the VI Toolkit
Now that you have PowerShell installed, let's install the VI Toolkit so you can run VMware PowerShell commands. First, download the latest VI Toolkit from VMware's website. (The Toolkit is updated periodically with new commands, so check back periodically and update to the latest release if necessary.) Next, install the downloaded file. This installs the VI Toolkit libraries (dll and xml files) for PowerShell, creates shortcuts for several documentation resources, and create a shortcut for a special PowerShell command prompt to use with the VI Toolkit. Without this prompt you can run into trouble. If, for example, you run cmdlets from the VI Toolkit with the originally installed PowerShell command prompt, cmdlets won't work because PowerShell is not aware of them. The new shortcut that is created called the VMware VI Toolkit calls PowerShell but also references a special .psc1 file so the VI Toolkit cmdlets are added as "SnapIns". SnapIns are used to extend PowerShell, add additional cmdlets and provide increased functionality. You can get a list of which SnapIns are loaded by typing
This command displays the VMware.VimAutomation.Core 1.0 SnapIn otherwise known as the VI Toolkit.
Using the VI Toolkit
Now that you have the VI Toolkit installed, you can use it with your VMware environment. But before we begin, let's review the syntax for PowerShell cmdlets. Most cmdlets have options and parameters. To see the available options, use the
cmdlet. You can also use multiple cmdlets simultaneously by stringing them together using the pipe (|) symbol. This is called pipelining and joins the cmdlets together so the output of the first cmdlet becomes the input for the second. For example, typing
Get-VM | Get-Snapshot
would instruct the program to use the VM list returned by the Get-VM command for the second command, Get-Snapshot. This would ultimately display a list of snapshots that exist on the VMs.
The first thing you need to do is connect to a host or vCenter Server, to do so you can use the Connect-VIServer cmdlet. There are many options for this cmdlet, but in its simplest form you can just type the command and it will prompt you for a host to connect to, followed by a login window. You can also instruct PowerShell which host to connect to and supply login information by using the following command syntax:
Connect-VIServer –server <host name> -user <user name> -password <password>
Once you are connected to vCenter or the host server, you can use other commands to get or set information. Try typing
for a list of all VMs on the host,
for a list of all virtual switches and
get-vm | get-cddrive
for status information on all your VMs' CD/DVD-ROM drives.
You can see a list of all the available cmdlets as well as descriptions, syntax and usage examples in the VI Toolkit help file that was installed as a shortcut. Building your own scripts is a helpful way to use cmdlets, because it allows you to type one command that executes multiple cmdlets. Fortunately, VMware users have written and published many PowerShell scripts on various websites that you can download and use in your environment.
Once you are comfortable with using PowerShell with VMware, you may want to set up a graphic user interface to replace the command line interface. In my next tip, I'll outline this process.
ABOUT THE AUTHOR: Eric Siebert is a 25-year IT veteran who specializes in Windows and VMware system administration. He is a guru-status moderator on the VMware community VMTN forums and maintains VMware-land.com, a VI3 information site. He is also the author of an upcoming book titled VI3 Implementation and Administration due out in June 2009 from Pearson Publishing and is a regular on VMware's weekly VMTN Roundtable podcast.