PowerCLI: Lesson #5
Lesson # 5
Ok if you look back at Lesson #3 we talked about a command to list out all the PowerCLI Get- cmdlets, which was >gcm –Module vmware* -verb get well let us come back to that now. Its traditional for PowerShell cmdlets to be constructed like so:
Verb-MeaningfulWord for example Get-VM
Well Get- isn’t the only verb used in PowerShell, you’ll also find:
- Set-
- New-
- Add– (there are only 3 of these in PowerCLI)
We’ll try and spend this lesson looking at some more PowerCLI cmdlets before spending more time on PowerShell fundamentals.
So we know how to list all the PowerCLI cmdlets but how do we find out how to use them? Well we have different options.
- Read the help that comes with the command
- Refer to the Guru’s :
- Hal Rottenberg – http://halr9000.com
- Alan Renouf – http://www.virtu-al.ent
- Luc Dekens – http://www.lucd.info
- Or just plain old Google it.
So I listed the PowerCLI Get- cmdlets and found a command Get-VMHost and wanted to learn how to use it. So I probed its help by placing “-?” after the cmdlet like so:
This is great to give you an insight in to the parameters to make the cmdlet work but I needed an example. If you look at the bottom of the help output you see it provides you with a way of showing some examples:
From this information I was able to see that the Get-VMHost cmdlet is used for finding out which host a VM is running on within a specific datacenter. So I tried it for myself:
Breakdown:
Explained:
a) We run the Get-VMhost cmdlet
b) Use the –Location switch to pass in the name of the datacenter.
c) We use the Get-Datacenter cmdlet to pass the object of the datacenter into the location parameter.
d) Use the –VM switch to pass in the name of the VM.
e) We use the Get-VM cmdlet to pass the object of the VM into the VM parameter.
And the result is the host system information that the VM “VeeamRicky” is running in.
So you can see it didn’t take much to substitute my lab information into the example to make the whole string of commands to work.
Now following on from that, what if we don’t know the Datacenter name! Well we can of course run the Get-Datacenter cmdlet which will enumerate the datacenter’s associated with you vCenter. Now in my Lab I only have one but if you have a few what you could do is store the enumeration in a variable.
>$mydatacenters = Get-Datacenter
So if we looked into the variable we see server entries potentially that are indexed for example:
[0] VeeamUKVsphereDatacentre1 Datacenter-datacenter-21
[1] VeeamUKVsphereDatacentre2 Datacenter-datacenter-22
[2] VeeamUKVsphereDatacentre3 Datacenter-datacenter-23
Because this an enumeration (collection) then your variable is holding multiple entries. This format of a variable is known as an Array. A single entry can be referenced by call the variable with an index like so:
>mydatacenter[1]
> Name Id
—- —
VeeamUKVsphereDatacentre2 Datacenter-datacenter-22
So in our Get-VMHost cmdlet we can substitute the Datacenter query with a our variable like so:
>Get-VMHost -location $mydatacenter[1] -VM (Get-VM -Name “VeeamRicky”)
And if you wanted to you could do the same with VM list retrieved with the Get-VM cmdlet
OK let’s break off there. Look out for Lesson6.
Leave a Reply
You must be logged in to post a comment.