Retrieving a list of all of your virtual machines
Now that we know how to connect to a server, let's do something useful with PowerCLI. Most of the people who begin using PowerCLI create reports, so create a list of all of your virtual machines as your first report. You have to use the Get-VM
cmdlet to retrieve a list of your virtual machines. The syntax of the Get-VM
cmdlet is as follows:
As you can see, the Get-VM
cmdlet has four different parameter sets. The names of these parameter sets are: Default
, DistributedSwitch
, ById
, and RelatedObject
. You can use these parameter sets to filter the virtual machines based on name, server, data store, location, distributed switch, ID, or related object.
Create your first report with the following command:
This will create a list of all of your virtual machines. You will see the name, power state, number of CPU's, and the amount of memory in GB for each virtual machine, as shown in the following command-line output:
The Name
, PowerState
, NumCPU
, and MemoryGB
properties are the properties that you will see by default if you use the Get-VM
cmdlet. However, the virtual machine object in PowerCLI has a lot of other properties that are not shown by default. You can see them all by piping the output of the Get-VM
cmdlet to the Format-List
cmdlet using the pipe character '|'. The Format-List
cmdlet displays object properties and their values in a list format, as shown in the following command-line output:
You can select specific properties with the Select-Object
cmdlet. Say you want to make a report that shows the Name
, Notes
, VMHost
, and Guest
properties for all your virtual machines. You can do that with the following command:
The output of the preceding command is as follows:
Suppressing displaying deprecated warnings
You will probably have also seen the following warning messages:
These warning messages show the properties that should not be used in your scripts because they are deprecated and might be removed in a future PowerCLI release. Personally, I like these warnings because they remind me of the properties that I should not use anymore. But if you don't like these warnings, you can stop them from appearing with the following command:
Using wildcard characters
You can also use wildcard characters to select specific virtual machines. To display only the virtual machines that have names that start with an "A" or "a", type the following command:
Parameter values are not case-sensitive. The asterisk (*) is a wildcard character that matches zero or more characters, starting at the specified position. Another wildcard character is the question mark (?) which matches any character at the specified position. To get all virtual machines with a three-letter name that ends with an "e", use the following command:
You can also specify some specific characters, as shown in the following command:
The preceding command displays all the virtual machines that have names starting with "b" or "c". You can also specify a range of characters, as shown in the following command:
The preceding command lists all the virtual machines that have names ending with 0, 1, 2, 3, or 4.
If you want to filter properties that don't have their own Get-VM
parameter, you can pipe the output of the Get-VM
cmdlet to the Where-Object
cmdlet. Using the Where-Object
cmdlet, you can set the filter on any property. Let's display a list of all of your virtual machines that have more than one virtual CPU using the following command:
In this example, the Where-Object
cmdlet has a PowerShell scriptblock as a parameter. A scriptblock is a PowerShell script surrounded by braces. In this scriptblock, you see $_
. When using commands in the pipeline, $_
represents the current object. In the preceding example, $_
represents the virtual machine object that is passed through the pipeline. $_.NumCPU
is the NumCPU property of the current virtual machine in the pipeline. –gt
means greater than, so the preceding command shows all virtual machines' objects where the NumCPU property has a value greater than 1.
PowerShell v3 introduced a new, easier syntax for the Where-Object
cmdlet. You don't have to use a scriptblock anymore. You can now use the following command:
Isn't the preceding command much more like simple English?
Note
In the rest of this book, the PowerShell v2 syntax will be used by default because the v2 syntax will also work in PowerShell v3. If PowerShell v3 syntax is used anywhere, it will be specifically mentioned.
Using comparison operators
In the previous section, you saw an example of the –gt
comparison operator. In the following table, we will show you all of the PowerShell comparison operators:
In the preceding table, you see three different operators for all functions. So what is the difference? The c
variant is case-sensitive. The two-letter variant and the i
variant are case-insensitive. The i
variant is made to make it clear that you want to use the case-insensitive operator.
The Where-Object
cmdlet has an alias, ?
. Therefore, both the following commands will display a list of all your virtual machines that have more than one virtual CPU:
Using aliases will save you from the trouble of typing in the PowerCLI console. However, it is good practice to use the full cmdlet names when you write a script. This will make the script much more readable and easier to understand. To see a list of all of the aliases that are defined for cmdlets, type the following command:
You can create your own aliases using the New-Alias
cmdlet. For example, to create an alias childs
for the Get-ChildItem
cmdlet, you can use the following command: