Académique Documents
Professionnel Documents
Culture Documents
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
Simple Talk
02 JUNE 2010
Jonathan Medd
Virtualised environments can range pretty wildly in size; from one physical
server in a small business or test lab with a few VMs, right up to large
enterprises with hundreds or thousands of physical servers and
seemingly limitless VMs. Whatever the size of your environment, as a
good system administrator, you know that its important to congure it in
a consistent manner, and automate as much as is reasonably possible.
If youre using VMware ESX / ESXi hypervisors and vCenter as the
management software, then you already have access to a number of
automation tools to assist with making your infrastructure function as
smoothly as possible. These include VMware vSphere PowerCLI, VMware
vSphere CLI and VMware vSphere SDK for Perl. In this article, I will be
focusing on VMware vSphere PowerCLI, the automation tool supplied as a
snapin for Microsofts Windows PowerShell command line tool and
scripting language, as well as 10 ways that any system administrator can
kick-start the automation of their virtual infrastructure.
Getting Started
As I just mentioned, vSphere PowerCLI is provided as a snapin for
Microsofts Windows PowerShell, and so to use it you will need:
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
1/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
Note
If you have Windows PowerShell 2.0, then Id advise installing
.NET Framework 3.5 SP1, since youll then have access to the
built-in PowerShell ISE and cmdlets (such as Out-GridView).
Of course, you should always refer to the VMware website for up-to-date
system requirements. At the time of writing, PowerCLI is currently in
version 4.0 Update 1, and is supported on the following 32 bit and 64 bit
platforms:
Windows Vista
Windows XP SP2
Windows 2003 Server SP2
PowerCLI itself will connect to and manage:
VMware ESX 4.0 Update 1/vCenter Server 4.0 Update 1
VMware ESX 4.0i
VMware ESX 4.0/vCenter Server 4.0
VMware ESX 3.5
VMware ESX 3i
VMware VirtualCenter 2.5
VMware ESX 3.0/VirtualCenter 2.0
There is also a separate PowerShell snapin available for VMware Update
Manager, which is the tool provided as an accompaniment to VMware
vCenter to centrally manage the patching of ESX / ESXi hosts and guest
virtual machines. This snapin has similar system requirements to the
main PowerCLI tools, although it must be noted that these cmdlets
require VMware vSphere PowerCLI 4.0 Update 1, and will only work with
VMware Update Manager 4.0 U1. So, if your vCenter is the base 4.0
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
2/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
release or earlier, you will not be able to use them, and youll need to
upgrade.
Both of the above sets of cmdlets can be found and downloaded from the
VMware PowerCLI site, which is a great reference site for everything
needed to get started with PowerCLI. If you really get stuck, there are
experts waiting in the forums to answer any queries you may have. Bear
in mind that when youre getting started, it is likely that the majority of
your questions will already have been answered in these forums, and a
simple search will nd those answers.
Once all of the system requirements have been installed, the PowerCLI
cmdlets can easily be accessed via the shortcut which the installer places
on the desktop. Alternatively, if youre already familiar with Windows
PowerShell, you may simply want to add the VMware snapins to your own
session. If youre unsure how to do this, you can just use the AddPSSnapin cmdlet to make these cmdlets available to the current session,
as shown below:
StandardPowerCLIcmdlets
AddPSSnapinVMware.VimAutomation.Core
VMwareUpdateManagercmdlets
AddPSSnapinVMware.VumAutomation
For regular use, it would make sense to add these to your default
PowerShell prole, which can be opened by entering this command from
within PowerShell:
notepad$profile
This will open a text le containing the custom conguration to run each
time a PowerShell session is opened. If you receive an error that the
prole le does not exist (i.e. one has not yet been created), a prole le
can be simply created via:
NewItemPath$profileItemTypefileForce
Once youve got your prole open, you just need to add this line to it:
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
3/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
AddPSSnapinVMware.VimAutomation.Core,VMware.VumAutomation
The system will use the credentials you are currently logged-on with to
establish a connection with the target. Alternative credentials can be
specied with the -Username and -Password parameters, if necessary. If
the current credentials are not sufcient, then you may be prompted to
enter more appropriate credentials via a standard Windows dialogue box.
4/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
found within VMware vCenter . Using the Get-VM cmdlet, and the
standard PowerShell Select-Object cmdlet, you can retrieve the particular
properties that are of interest and use Export-CSV to export that data into
the ever-useful CSV le format for further manipulation and distribution.
GetVM|SelectObjectName,NumCPU,MemoryMB,PowerState,Host|
ExportCSVVMs.csvNoTypeInformation
Another example where this cmdlet can be put to good use is to alleviate
a common stumbling block when attempting to VMotion virtual machines
from one ESX host to another (in case you dont use VMware regularly, to
VMotion virtual machines means to use VMware VMotion to Live
Migrate virtual machines to another physical host). There are a number
of reasons why a virtual machine might not be permitted to VMotion; one
of the most common is because it has a local CD-Rom drive attached.
The below set of commands will iterate through all virtual machines, lter
them based on whether the connection state of their CD-Rom drive is true,
and return the name of the offending virtual machine(s):
GetVM|WhereObject{$_|GetCDDrive|
WhereObject{$_.ConnectionState.Connectedeq"true"}}|
SelectObjectName
5/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
This is just a single example, but now that you know how to quickly gather
the data you need, you can easily target specic settings and make
appropriate congurations a snap. You can nd out more about what
cmdlets are available to adjust your VMs settings from the PowerCLI
website, as mentioned earlier.
#Totalnumberofguests
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
6/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
$TotalVMs=(GetVM).Count
WriteHost"Thereare$TotalVMsVirtualMachinesin
$DefaultVIServer"
#AverageVMsPerCluster
$Clusters=GetCluster
foreach($Clusterin$Clusters){
$VMHosts=GetCluster$Cluster|GetVMHost
$VMs=GetCluster$Cluster|GetVM
$AverageVMsPerCluster=[math]::round(($VMs.count/$VMHosts.cou
nt),1)
WriteHost"$Clusterhas$AverageVMsPerClusterguestsperVMware
host"
However, if you are running a VMware version prior to version 4.0 in your
environment, or maybe you want to rescan hosts in more than one cluster,
then the Get-VMHostStorage cmdlet has two parameters which youll nd
useful: -RescanAllHba and -RescanVmfs. Used in combination with the
Get-Cluster and Get-VMHosts cmdlets, Get-VMHostStorage can quickly
become very powerful; for example, the command below will rescan for
new storage on all of the hosts in Cluster 1:
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
7/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
GetCluster'Cluster1'|GetVMHost|
GetVMHostStorageRescanAllHbaRescanVmfs
Of course, you may not have enough resources on the target host to move
all VMs across, and consequently might wish to distribute them more
evenly across a number of hosts. Wrapping all that lovely technology into
just one elegant step, the short script below will VMotion all of the virtual
machines from one host in a cluster to random other hosts within the
same cluster.
$sourcehostname=(GetVMHost'esx01').name
$clusterhosts=GetCluster'Cluster1'|GetVMHost|
WhereObject{$_.namenotlike"$sourcehostname"}
$vms=GetVMHost$sourcehostname|GetVM
foreach($vmin$vms){
$targethost=$clusterhosts|GetRandom
MoveVMVM$vmDestination$targethost
}
First of all, the name of the source host will be stored in the variable
$sourcehostname. Then we retrieve all of the hosts within Cluster 1, drop
the source host from that list, and store the results in $clusterhosts. Next,
we retrieve all of the virtual machines on the source host, storing them in
$vms, and nally we cycle through each virtual machine, generate a
random target host and move the VM to that target host.
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
8/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
We can bring that data into the PowerShell session with the Import-CSV
cmdlet, and then feed it into the Get-VM and Move-VM cmdlets to
Storage VMotion those virtual machines to the new SAN.
$vms=ImportCSVC:\Scripts\SVMotion.csv
foreach($vmin$vms){
GetVM$vm.name|MoveVMDatastore$vm.targethostname
}
Note that the Import-CSV cmdlet will take the column headers in the CSV
le and enable access to them as property labels; i.e. for line 2, $vm.name
is Server01 and $vm.targetdatastore is Datastore1_NEW.
When carrying out such a SAN migration, you may wish to carry out the
work outside of normal ofce hours to take advantage of low-use periods
on the SAN, and consequently move the data quicker. So, why not create
a Windows Scheduled Task to kick off the above script and schedule it for
the required time? If you wanted to be really smart, you could use the
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
9/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
The virtual machines can be easily deployed by, once again, using ImportCSV to read in the input data. Then we cycle through the virtual machines
and, for each deployment, get the template, host,datastore and
customization to use, before nally executing the New-VM cmdlet and
feeding it those various parameters to use for creating each VM:
$vms=ImportCSVC:\Scripts\NewVMs.csv
foreach($vmin$vms){
$Template=GetTemplate$vm.template
$VMHost=GetVMHost$vm.host
$Datastore=GetDatastore$vm.datastore
$OSCustomization=GetOSCustomizationSpec$vm.customization
NewVMName$vm.nameOSCustomizationSpec$OSCustomization`
Template$TemplateVMHost$VMHostDatastore$DatastoreR
unAsync
}
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
10/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
Or to rst of all clear existing NTP server(s) then set the new one:
GetVMHostesx01|RemoveVMHostNtpServer`
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
11/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
NtpServer(GetVMHostesx01|GetVMHostNtpServer)
GetVMHostesx01|AddVMHostNtpServerNtpServerntpservername
Or how about bringing any newly installed ESX / ESXi host immediately
up-to-date with the current standard patch baseline? Easy.
Retrieve the required baseline:
$Baseline=GetBaseline"CurrentESXBaseline"
12/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
$Baseline|RemediateInventoryEntityesx01Confirm:$false
These commands could all be added to a short script to be run at the end
of a build process, to ensure that all newly deployed hosts are at the
required patch level.
Summary
VMware environments of any size, small or large, can benet greatly from
automation. This article has introduced you to some of these possibilities
and how you might use them to make your systems run better, in a more
consistent fashion. The beauty of this is twofold: youll nd it signicantly
easier to be aware of the status of all your virtual environments, and youll
have more time to spend on the projects that you never get around to.
Not only will you be proactive, but youll also have more time to feel selfsatised about it. PowerCLI is intuitive to use, and it is incredibly simple to
very quickly get real results with it I urge you to give it a try, if you
havent already done so!
Resources
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
13/14
11/15/2016
10StepstoKickStartYourVMwareAutomationwithPowerCLISimpleTalk
VMware Tools:
ESX / ESXi
vSphere PowerCLI
vSphere CLI
vSphere SDK for Perl
Alan Renoufs vSwitch Script
Alan Renoufs vCheck
https://www.simpletalk.com/sysadmin/virtualization/10stepstokickstartyourvmwareautomationwithpowercli/
14/14