Beginners Guide to Managing VMware using PowerShell

white paper
Beginners Guide to Managing VMware
using PowerShell
Title: Author(s): Target Audience: Current Revision: First Published: Product(s): UID: Beginners Guide to Managing VMware using PowerShell
Xtravirt (Peter Grant)
Technical - Novice
1.0 (Nov 2009)
Nov 2009
VMware, Microsoft PowerShell
XD10133
Content Overview: •
PowerShell Introduction
•
Install Steps and First Commands
•
Creating PowerShell Scripts
•
Running PowerShell Scripts from DOS Command Prompts or Task Scheduler
1.0 Introduction
This document is a beginner’s guide to configuring
and using PowerShell to perform administration tasks
within a VMware Infrastructure. This assumes little or
no experience with PowerShell and will go through
the steps of downloading, configuring and using it to
perform simple tasks.
It is meant as a basic introduction to show the steps
required to get started.
2.0 PowerShell Introduction
PowerShell is Microsoft’s newest scripting language
focused on providing administrators with an easy and
powerful way to perform certain tasks. PowerShell is
either included or can be downloaded and installed on
the following operating systems:
Native Support:
- Windows 2008
- Windows 7
Requires PowerShell Download: (http://tinyurl.
com/xtravirt-xd10133-1)
-
-
-
command prompt is similar to the traditional ‘DOS’
Command prompt but is used specifically to execute
PowerShell commands. It is found in “Start / Programs /
Windows PowerShell” or similar depending on your OS.
Note: You cannot execute native PowerShell commands by
simply typing them into a DOS Command Prompt
Cmdlets are chunks of PowerShell code that perform
certain predefined actions. VMware has written their
own Cmdlets which can be used to manage the Virtual
Infrastructure.
An example of a VMware Cmdlet is “Start-VM
<VMname>”
Before the above VMware Cmdlets can be run there are
3 things that need to happen.
1.
2.
3.
PowerShell needs to be installed
The VMware PowerShell add-ins need to be
installed
A connection must be made to the vCenter server
or ESX Host
The next section goes through the steps of installing
and running VMware PowerShell commands.
Windows 2003 Server
Windows Vista
Windows XP SP2
Once installed there are several native PowerShell
commands, called Cmdlets, that can be executed via
the PowerShell command prompt. The PowerShell
information@xtravirt.com
www.xtravirt.com © 2009 |
1
visit www.xtravirt.com for the
latest version of this document
Online resources for the virtualization industry
We simplify complex concepts and make them valuable, practical
assets. Free white papers, articles, how-to-guides, e-books,
presentation tools and much more.
3.0 Installation Steps and First
Commands
Connect-VIServer -Server
<vCenterIPAddress> -User
<adminUsername> -Password <password>
Note: The command above is entered as one line
1.
Install Windows PowerShell
Note: Skip this step if using Windows 7 or Windows 2008
Browse to the following Microsoft link and install
PowerShell for the correct OS being used: http://www.
microsoft.com/windowsserver2003/technologies/
management/powershell/download.mspx
2.
If Certificates haven’t been configured then there will
be a warning as seen below. This can be ignored.
Enable PowerShell Script Execution
Note: As a security precaution remote PowerShell scripts cannot
be executed unless you enable it. This is required before VMware
commands can be executed.
Open up the Windows PowerShell command
prompt and type set-ExecutionPolicy
RemoteSigned
5.
Run the command(s)
Now you’re ready to run some commands.
Try the following:
Get-VM
3.
Install the VMware PowerShell Components
A list of all your VM’s should be shown.
Install the ‘PowerCLI’ onto your local PC. This contains
the VMware software management objects and
Cmdlets
Now type:
Download from here: www.vmware.com/go/powercli
This will display all the VMware CmdLets
4.
Get-Command
Connect to vCenter or ESX
Now Open the VMware (not Windows) PowerShell
Command Prompt and type the following:
information@xtravirt.com
www.xtravirt.com © 2009 |
2
visit www.xtravirt.com for the
latest version of this document
Here’s just a tiny sample of the example commands
showing what can be done:
PowerShell Command
Action
Start-VM <guestVMname>
Power on the VM specified
ShutDown-VMGuest <guestVMname>
Shuts down Guest OS
Get-DRSRule –Cluster
Displays DRS rules for
specified cluster
<ClusterName>
Note: All commands above are entered as one line
6.
Disconnect from vCenter or ESX
Once finished, it is good practice to disconnect. To do
this type the following command:
Disconnect-VIServer
C:\WINDOWS\system32\
windowspowershell\v1.0\powershell.
exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\VSphere
PowerCLI\vim.psc1” -NoExit –Command
Note: The command above is entered as one line
Don’t forget you still must connect to a vCenter Server
or ESX Host so the first command might look like this:
C:\WINDOWS\system32\
windowspowershell\v1.0\powershell.
exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\
VSphere PowerCLI\vim.psc1”
-NoExit –Command Connect-VIServer
-Server <vCenterIPAddress> -User
<adminUsername> -Password <password>
Note: The command above is entered as one line
4.0 Creating PowerShell Scripts
Just like batch files it is possible to run PowerShell
commands in a script.
To do this simply create a file using notepad with a .ps1
extension. i.e. MyFirstPSscript.ps1
Type your PowerShell commands into this file and then
save it. To execute it simply type the name of the file
into a VMware PowerShell Command prompt.
5.0 Running PowerShell Scripts
from DOS Command Prompts or
Task Scheduler
Now that you know how to run PowerShell commands
to control your VMware Infrastructure you might be
wondering how to schedule these commands using
Task Scheduler which only recognises ‘DOS’ commands
or batch files.
This is actually quite simple. PowerShell commands
can in fact be executed from a DOS Command prompt
by adding the following prefix to the command so
Windows knows to call PowerShell and reference the
VMware add-ins to execute it.
Likewise a PowerShell script named C:\MyFirstPSscript.
ps1 can be called by typing:
C:\WINDOWS\system32\
windowspowershell\v1.0\powershell.
exe -PSConsoleFile “C:\Program
Files\VMware\Infrastructure\VSphere
PowerCLI\vim.psc1” -NoExit –Command
C:\MyFirstPSscript.ps1
Note: The command above is entered as one line
The above line is what would be typed into Windows
Task Scheduler.
Note: These commands assume a vSphere PowerCLI install with
default directory locations.
6.0 Summary
This article introduces the very basics of VMware
PowerShell commands for someone who hasn’t used
these before. There is a wealth of information on
PowerShell scripting on the VMware and Microsoft
web sites along with a number of sample scripts. Most
things that can be done via the VI Client can also be
scripted using PowerShell making this a powerful tool
for the administrator.
This concludes the white paper. References and Useful
links are located on the following page.
At a DOS Command prompt type the following and
then add any VMware PowerShell CmdLet command:
information@xtravirt.com
www.xtravirt.com © 2009 |
3
visit www.xtravirt.com for the
latest version of this document
About Xtravirt
Xtravirt is a knowledge-based company that delivers its expertise in virtualization online and in person. We have developed a reputation for astute
leadership and expertise through our work with an impressive array of organisations. It is this real-world experience that drives our ability to provide
independent, current and free advice online.
We work with organisations whose IT staff are frustrated with how hard it is to find detailed information and skills around virtualization. We help our clients
deliver the true benefits of virtualization, resulting in cost and time savings.
For more information contact:
Dorset House, Regent Park
297 Kingston Road, Leatherhead
Surrey KT22 7PL
t +44 (0) 1372 824 296
f +44 (0) 1372 824 576
e information@xtravirt.com
w www.xtravirt.com
© Copyright 2009 Xtravirt Ltd. All rights reserved. The information contained herein is subject to change without notice. Xtravirt Ltd shall not be liable for technical or editorial errors
or omissions contained herein. Xtravirt and the Xtravirt logo are registered trademarks of Xtravirt Ltd. The names of actual companies and products mentioned herein may be the
trademarks or registered trademarks of their respective owners.
This document remains the property of Xtravirt Ltd. Contents may not be copied, reproduced or modified without written consent from Xtravirt Ltd.
References
Useful Links
1.
1.
2.
3.
Microsoft PowerShell Download, http://www.microsoft.com/
windowsserver2003/technologies/management/powershell/
download.mspx
VMware PowerShell Components Download and
Documentation, www.vmware.com/go/powercli
VMware PowerShell Components Install Guide, http://www.
vmware.com/support/developer/windowstoolkit/wintk40/
doc/viwin_install.pdf
information@xtravirt.com
2.
vSphere PowerCLI Administration Guide, http://www.vmware.
com/support/developer/windowstoolkit/wintk40/doc/
viwin_admg.pdf
vSphere PowerCLI Documentation Guide, http://www.
vmware.com/support/developer/windowstoolkit/
Tags
VMware, PowerShell, Scripting, PowerCLI, vCenter, ESX, ESXi
www.xtravirt.com © 2009 |
4
Study collections