PowerShell Scripting Examples for VMware Virtual Infrastructure
Like a lot of virtual infrastructure administrators already seem to be doing, I decided I better start learning about PowerShell scripting for VMware ESX and VirtualCenter. There has been a lot written on this topic in the recent months, but I have been just been speed reading through with the idea that “I’ll get around to understanding and learning this later when I have more time”. Now I am holding myself to this promise, so this post is my first steps to making myself learn. I am also looking forward to attending the Scripting VMware Infrastructure: Automating, Integrating, and Extending VI VMWorld Hands On Lab (Lab 09) already on my schedule for the conference.
Here’s what little I know about Powershell for VMware so far:
- PowerShell is actually a Microsoft application that is the base of the VMware VI Toolkit
- The best way for getting started with PowerShell is to download the Windows PowerShell 1.0 Documentation Pack
- The VMware Communities is home to the VI PowerShell Blog. They are actually running a scripting contest right now.
Here are links to several useful PowerShell scripts created by the user community:
baeke.info :: PowerShell script to check ESX patch status
“You can use the simple PowerShell script below to check ESX server patching status from your Windows client.”
VMware Powershell script - remove connected iso
“… is a script which can remove the connected iso files from the CD drive. Some administrators do not disconnect a CD drive when the have finished use the ISO, the side effects of this behavior are: the VM regularly polls the CD drive which creates a slight overhead and a VMotion may fail. This script can run unattended e.g. every night so even when an ISO file remains mounted it is ejected every night.”
Adding and re-adding ESX servers to VirtualCenter
“This script automatically detects any disconnected host and reconnects it (be sure you connect to VirtualCenter before running it though!) On the first line, the script also allows setting a password Sometimes you can’t reconnect a host without a password, usually because the password has been changed. If this applies to you, set the password here and you’ll be re-connecting hosts faster than ever.”
[omitted]
If an ESX host you want to manage doesn’t appear in VirtualCenter, you need to add it. This is a bit tricker than reconnecting since there’s no inventory in VirtualCenter to tell you the IP addresses of all the hosts you use, and you also need to know a host’s password in order to add it. This is another case where entering things in a spreadsheet can really speed things up. I made just such a spreadsheet, and here’s a video of me using it.”
PowerShell Script for VDI Mass Replicas
“Folks, many folks have asked HOW we do the “thousands of desktops, instantly, consuming the space of one” that I demoed and described about here.
Here are the step-by-step instructions and PowerShell scripts we use.”
Automatically Configure VirtualCenter Virtual Networking Using PowerShell
“I wrote a little PowerShell script that goes out to a VirtualCenter instance and grabs a list of all the Port Groups in use by every ESX host under VC management. It ignores the default ‘VM Network’, ‘Service Console’, and ‘VMKernel’ PortGroups. Then it goes out to each ESX host under management and makes sure that it has all of the VLans and that they are using the correct VLanId. It does some simple error checking. If the VLan ID’s don’t match up, the script prompts you to ask you to pick the right one. So now when I need to add a network, I can just add it to any host under management and then run my script to reconcile everything. I think its neat. Saves me time and human error.”
Scripting VMware ESX servers to fail over to other ESX servers
“The solution used in this article gets by the downtime limitation by using VMware Converter 3.0.1 Enterprise to schedule a “conversion” of the target virtual machine to a new location on another ESX server while the target virtual machine is still running.
VMware Converter 3.0.1 Enterprise allows you to use a tool called p2vtool.exe. This is a command-line interface for VMware Converter 3.0.1. It should be noted that there is only experimental support for p2vtool.exe. However, I have tested the scripted conversion presented in this article several times with no failures.”
Related Posts
Tags: powershell, scripting, vi toolkit, virtualization, vmworld2008









