8.1 VCAP-DCA Study Guide - Getting Started with PowerCLI
Posted on 21 Apr 2011 by Ray Heffer
If you have no or little experience with Windows PowerShell, but want to jump straight into VMware PowerCLI then you may find you have some gaps in the knowledge required to get started. How do you add the VMware PowerCLI snap-in (VMware.VimAutomation.Core) for example? How do you construct a foreach statement? How do you work with variables? There are some excellent resources available on the web, and one of my favourites is Alan Renouf’s website Virtu-Al. He also has a great article on Working with events. Definitely work a look!
Lesson 1: PowerShell ‘Need to Know’
Once you’ve installed PowerCLI, the first thing you should do is set the execution policy to allow RemoteSigned
scripts to run. This is only done one time.
Set-ExecutionPolicy RemoteSigned
Tip: Throughout PowerCLI, if you can’t remember a command type part of it and hit Tab to scroll through the available commands. For example, Set-E (hit Tab) will complete the Set-ExecutionPolicy command. Not sure what comes next, then type /? and it will show the list of available values; Unrestricted, RemoteSigned, Allsigned
etc.
Special Variables
Variable | Description |
---|---|
$_ | Pipeline variable. This is a placeholder for the current (pipeline) object, for example in a foreach loop it will contain the current object in the loop |
$? | Status of the last statement that was executed |
$Args | Used in creating functions that require parameters |
$Env:Path | Environmental path to files |
$Error | Recent error messages |
$foreach | Refers to the enumerator in a foreach loop |
$Home | Home directory of the current user (E.g. %USERPROFILE%) |
$True | Returns boolean TRUE |
$False | Returns boolean FALSE |
$Null | Null |
Operators
Operatpr | Description |
---|---|
-eq | equal to |
-ne | not equal to |
-gt | greater than |
-lt | less than |
-ge | greater or equal to |
-le | less or equal to |
-contains | contains |
-notcontains | doesn’t contain |
Aliases
Type Get-Alias to list all PowerShell aliases. Here are two of the most frequently used ones:
Alias | Description |
---|---|
% | Alias to ForEach-Object cmdlet. This is often used and can be confusing to novices trying to understand other PowerShell scripts! |
? | Alias to Where-Object cmdlet |
IF Statement
Condition | Description |
---|---|
IF | If (condition) { statement } |
ElseIf (condition) { statement } | |
Else (condition) { statement } |
Example
if ($vmguest.OSfullname -eq $null) {$vmguest.OSfullname = "Unknown OS"}
ForEach Loop
Loop | Description |
---|---|
ForEach | ForEach ($items in $array) { statement } |
Example
$getvm = Get-VM
ForEach ($vm in ($getvm | Where-Object {$_.MemoryMB -lt "2048"})) {
$vm
}
Lesson 2: Getting Started with PowerCLI
First make a connection to vCenter (or an ESX host):
Connect-VIServer -server <vcenter_hostname>
Getting help and examples
Get-Help
Get-Help Get-VMHost
Get-Help Get-VMHost -examples | more
Get-Help Get-VMHost -full | more
This is useful as it brings up the help window which you can then use to search or browse for Cmdlets and examples…
Get-PowerCLIHelp
Get Properties and Methods for an object
Get-VMHost | Get-Member
Get-VM | Get-Member
Let’s start with a basic task, then we’ll break it down to see how the command is constructed:
List all VM’s with a connected CD-ROM
Get-VM | where { $_ | Get-CDDrive | where { $_.ConnectionState.Connected -eq "true" }}
Let’s break this down…
-
Using Get-VM, pipe this to a where statement…
-
Use $_ which in this case is our VM (current object)
-
Pipe this to Get-CDDrive where $_ (current object, in this case the CDDrive) ConnectionState.Connected is true.
So how did I come up with this? Let’s break it down further.
Lets put our Get-VM into a variable $vm
$vm = Get-VM
We could type $vm and it’ll do the same as Get-VM, but now it’s in a variable we can do more with it. For example, try $vm[0], this will get the first VM in the list (or array).
$vm[0]
We could also try:
$vm | where { $_.Name -eq "DC01" }
This would list the VM with the name of DC01.
Now we can focus on one VM at a time, lets see what we can find out. Lets pipe it to Get-CDDrive.
$vm[0] | Get-CDDrive
It doesn’t give us a great deal, so lets pipe format-list (or fl)…
$vm[0] | Get-CDDrive | fl
This is more interesting, notice now we can see ConnectionState and it has Connected:False. So you can see we can use ConnectionState.Connect -eq “true” in our command.
Create a new datacenter called TestDC
You can list existing datacenters with Get-DataCenter. Use Get-Folder to see all items in the hierarchy and datacenters is at the top. To create a new datacenter at the root level we’ll use Get-Folder -NoRecursion.
New-DataCenter -Location (Get-Folder -NoRecursion) -Name TestDC
If you use Get-Datacenter
again, you should now see your new datacenter.
Adding a host to the TestDC datacenter
Add-VMHost -Location TestDC -User root -Password **** -force:$true
We need to use -force:$true
, otherwise it will fail with an error regarding a self-signed certificate (unless you have a trusted certificate of course).
Placing an ESX host into Maintenance Mode
Set-VMHost -VMHost -State "Maintenance"
or to exit maintenance mode use
Set-VMHost -VMhost -State "Connected"
Create a variable to store your ESX host credentials
$creds = Get-Credential
Connecting to vCenter or ESX
Connect-VIServer -Credential $creds
Add an ESX host to a variable, and use it to retrieve the host properties
$esxhost = Get-VMHost
Get the methods and properties of the host using $esxhost | Get-Member
Display the version of ESX
$esxhost.version
Tagged with: vmware certification