By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
In part one of this tip, we showed you how to install and use PowerShell as well as install the VMware Infrastructure Toolkit (VI Toolkit) component that allows you to use PowerShell with your VMware environment. Now in part two, we offer some additional tips on PowerShell, show you how to run some of the great scripts that others have written and how to add a graphical user interface (GUI) to PowerShell to enhance its functionality and usability.
If you're not sure where to start when creating your own scripts, begin with the basics. First, identify areas in your virtual environment that could benefit from scripting. For example, you may want to make up for features that are lacking in the VI Client or in vCenter Server, such as snapshot reporting. Additionally scripts are useful for automated time-consuming or repetitive administration tasks, such as setting the video hardware acceleration level on Windows virtual machines (VMs) after you have installed VMware tools to achieve the best mouse performance.
Consider using PowerGUI
Starting from scratch can be difficult when you are new to a scripting language. I find that the easiest way to learn scripting is by using reference materials available and looking at scripts built by others to see how the code is written and executed. If you are uncomfortable with traditional manual coding and prefer using a GUI then you should try using the PowerGUI application. PowerGUI is a free application that adds a GUI and script editor to PowerShell to make it easier for you to develop code and use PowerShell. The GUI allows you to use PowerShell without having to know how to code. The code is automatically generated when you perform certain actions. It also comes with a great script editor that you can use to write scripts with. You can extend PowerGUI by adding PowerPacks, which are add-on modules written specifically for certain applications and purposes such as VMware. To get started with PowerGUI follow the below steps:
Make sure you already have PowerShell and the VMware VI Toolkit installed on your workstation, as covered in part one of this series, Installing and using the PowerShell tool.
Download PowerGUI here and install it on your workstation. The installation is pretty straightforward. You can just accept the defaults and click Next through the installation wizard.
PowerGUI is integrated directly with the VI Client by default. Once PowerGUI is installed, close down your VI Client if it was open and restart it. Go to Plugins on the top menu and select Manage Plugins, click on the Installed tab and check the Enabled box for the PowerGUI plugin. Then click OK.
Once you do this, select the Plugins menu again. You should see two new options tolaunch PowerGUI and automatically inherit your current connection to your host or vCenter Server. These options set it up so that you will not be prompted to log in again when your scripts run.
Next you need to download the VMware VI Toolkit Powerpack to your workstation. If you do not already have it open, launch PowerGUI, right-click on the top-level item and select Import.
Browse to the Powerpack that you downloaded and select it. Once it is imported, you will see a new VMware folder added. If you expand the folder, you will see lots of objects below it.
Next we need to connect to a vCenter Server or host. To do so, select the Managed Hosts object under the VMware folder in the left pane. Then, in the right pane under Actions, click Add Connection. A window will appear where you can specify the connection parameters including the vCenter Server or host name/IP address, port number, protocol and a log-in account. Click the dots next to the HostID and add a value for your vCenter Server or host name/IP address. If you launched PowerGUI through the VI Client plug-in you do not need to enter a log-in account, as it will use the currently authenticated account. Otherwise enter a log-in account name.
Click to enlarge.Once you are connected, you can start exploring your environment. In the left pane under your host, you can select various objects, such as VMs, and click on them to display associated information in the middle output pane. You can than select one or more VMs to either generate reports or perform different actions. In the right-hand panes, you will see the links and actions you can select. Links basically return information and actions perform some type of action on one or more objects. The pre-built Links and Actions that you see are basically PowerShell code, or cmdlets. If you select one and go to its properties, you will see the code or command that it uses. You can click the Add New Item links to add custom links. Notice that you can click on the column headings to sort them, and at the bottom there are tabs for user interface (UI) and PowerShell Code. The UI tab shows the output from Links and Actions in column format; the PowerShell Code tab shows the underlying code that is executing for the particular Link or Action that you use. Paying attention to these areas will help you learn how to write PowerShell code.
So let's try out a few options. First let's display a listing of all VM snapshots in a cluster. Select a Cluster in the left pane and then, in the middle pane, select a Cluster from the resulting listing. Next, select snapshots in the Links section and a list of all snapshots that exist on any VM in that cluster will be displayed. In the Actions section you can choose to Revert or Remove any of the snapshots in the middle output window.
To display all your Virtual Machine File System, or VMFS, data stores along with their free space and total size statistics, select the Datastore object in the left pane. A listing will appear in the output pane. Note in the Actions: Common section you can also export the results to a XML, CSV or HTML file.
Lastly, to see network adapter information on your VMs and make changes to several at once, select the Virtual Machines object in the left pane. In the output pane, select one or more VMs. In the Links section, select Network Adapters. A listing will be displayed of the MAC address, WOL (or Wake on LAN ) Enable, Network Name, vNIC type and more.
If you select one or more network adapters and select Change Properties in the Actions section, a dialog window will open that allows you to change multiple parameters for the virtual network interface card (vNIC) such as the Network Name (seen below) or MAC Address.
There are a lot of things that you can do with PowerGUI so spend some time exploring it. The other component of PowerGUI is the PowerGUI Script Editor. The script editor is basically a text editor with severaladditional features to assist with writing PowerShell code. Your code is formatted and laid out nicely, so it is easy to follow and provides options for tasks such as debugging and running your code in a PowerShell window. You can also launch the Script Editor from the VI Client Plugins menu and use it to open existing scripts or write new ones.
Using the PowerGUI Script Editor
Let's open the editor and load an existing script that creates a list of snapshots and sends the report to an email address. Since this script includes hard-coded username/password fields in the code we need to modify the username, password and server list for our environment. Change these fields to reflect your vCenter Server or host log-in username/password and host names. Optionally, if you launch the script editor from the VI Client, you can remove the –user and –password parameters from the Connect-VIServer line as your connection to the vCenter Server has already established.
Now we are ready to run the script. Press Alt-F5 or click the Run in External PowerShell Window button and a new window will open that the script will run inside. Once the script completes, you will be at the PowerShell prompt and you can type exit to close it. The email report below is an example of the report that the script produces.
PowerGUI makes using PowerShell easier, especially for system administrators who have not written code before. The power and flexibility of PowerShell are its greatest strengths and there are unlimited possibilities for using it with your VMware virtual environment thanks to the VMware VI Toolkit PowerPack. PowerShell, however, is not limited to VMware and can be used for administering Windows environments as well.
PowerShell for VMware resources
To make scripting easier, you should equip yourself with resources on the process. Here is a list of the most helpful resources I've used:
- Alan Renouf's excellent quick reference guide for PowerShell.
VMware VI Toolkit blog – A blog by VMware employees including Carter Shanklin that covers many topics and the latest news on the VI Toolkit.
VI Toolkit hands-on lab manual – This lab manual from VMworld 2009 is a great resource for getting started with PowerShell and the VI Toolkit.
VI Toolkit community – Forums where you can post questions and review documents that contain scripts by others.
Managing VMware with PowerShell FAQ – A useful document for beginners.
Scripting with PowerShell Center – Microsoft's homepage for PowerShell with lots of good documentation and resources.
Get Scripting website – A website that features PowerShell information and a podcast that is recorded periodically on scripting with PowerShell.
- Scott Herold's website – Herold works for Quest Software, which developed the PowerGUI and the VMware Powerpack. He is passionate about scripting in PowerShell and has a useful video how-to series on it.
|Eric Siebert is a 25-year IT veteran with experience in programming, networking, telecom and systems administration. He is a guru-status moderator on the VMware community VMTN forums and maintains VMware-land.com, a VI3 information site.|