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!
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.
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 |
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 |
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 |
Condition | Description |
---|---|
IF | If (condition) { statement } |
ElseIf (condition) { statement } | |
Else (condition) { statement } |
if ($vmguest.OSfullname -eq $null) {$vmguest.OSfullname = "Unknown OS"}
Loop | Description |
---|---|
ForEach | ForEach ($items in $array) { statement } |
$getvm = Get-VM
ForEach ($vm in ($getvm | Where-Object {$_.MemoryMB -lt "2048"})) {
$vm
}
First make a connection to vCenter (or an ESX host):
Connect-VIServer -server <vcenter_hostname>
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-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:
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.
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.
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).
Set-VMHost -VMHost -State "Maintenance"
Set-VMHost -VMhost -State "Connected"
$creds = Get-Credential
Connect-VIServer -Credential $creds
$esxhost = Get-VMHost
Get the methods and properties of the host using $esxhost | Get-Member
$esxhost.version
Tagged with: vmware certification
Comments are closed for this post.