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:
Get-VM [[-Name] <String[]>] [-Server <VIServer[]>] [-Datastore <StorageResource[]>] [-Location <VIContainer[]>][-Tag <Tag[]>] [-NoRecursion] [<CommonParameters>] Get-VM [[-Name] <String[]>] [-Server <VIServer[]>] [-DistributedSwitch <DistributedSwitch[]>] [-Tag <Tag[]>][<CommonParameters>] Get-VM [-Server <VIServer[]>] -Id <String[]> [<CommonParameters>] Get-VM -RelatedObject <VmRelatedObjectBase[]> [<CommonParameters>]
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:
PowerCLI C:\> Get-VM
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:
Name PowerState Num CPUs MemoryGB ---- ---------- -------- -------- Dc1 PoweredOn 2 8.000 VM1 PoweredOn 1 0.250 DNS1 PoweredOn 2 8.000
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:
PowerCLI C:\> Get-VM -Name DC1 | Format-List -Property * PowerState : PoweredOn Version : v10 Description : Domain controller Notes : Domain controller Guest : DC1: Microsoft Windows Server 2012 (64-bit) NumCpu : 2 MemoryMB : 8192 MemoryGB : 8 HardDisks : {Hard disk 1, Hard disk 2} NetworkAdapters : {Network adapter 1} UsbDevices : {} CDDrives : {CD/DVD drive 1} FloppyDrives : {} Host : ESX1.blackmilktea.com HostId : HostSystem-host-10 VMHostId : HostSystem-host-10 VMHost : ESX1.blackmilktea.com VApp : FolderId : Folder-group-v9 Folder : Discovered virtual machine ResourcePoolId : ResourcePool-resgroup-8 ResourcePool : Resources PersistentId : 50289ad0-a545-1299-5a75-811049f98276 UsedSpaceGB : 71.268582795746624469757080078 ProvisionedSpaceGB : 78.204129670746624469757080078 DatastoreIdList : {Datastore-datastore-14} HARestartPriority : ClusterRestartPriority HAIsolationResponse : AsSpecifiedByCluster DrsAutomationLevel : AsSpecifiedByCluster VMSwapfilePolicy : Inherit VMResourceConfiguration : CpuShares:Normal/2000 MemShares:Normal/81920 Name : DC1 CustomFields : {[Application code, ], [CreatedBy, Unknown], [CreatedOn, Unknown], [Function, ]...} ExtensionData : VMware.Vim.VirtualMachine Id : VirtualMachine-vm-16 Uid : /VIServer=blackteamilk\[email protected]:443/VirtualMachine=VirtualMachine-vm-16/ Client : VMware.VimAutomation.ViCore.Impl.V1.VimClient
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:
PowerCLI C:\> Get-VM | Select-Object –Property Name,Notes,VMHost,Guest
The output of the preceding command is as follows:
Name Notes VMHost Guest ---- ----- ------ ----- DC1 192.168.0.133 DC1: VM1 192.168.0.134 VM1: DNS1 DNS Server 192.168.0.134 DNS1:
Suppressing displaying deprecated warnings
You will probably have also seen the following warning messages:
WARNING: The 'Description' property of VirtualMachine type is deprecated. Use the 'Notes' property instead. WARNING: The 'HardDisks' property of VirtualMachine type is deprecated. Use 'Get-HardDisk' cmdlet instead. WARNING: The 'NetworkAdapters' property of VirtualMachine type is deprecated. Use 'Get-NetworkAdapter' cmdlet instead. WARNING: The 'UsbDevices' property of VirtualMachine type is deprecated. Use 'Get-UsbDevice' cmdlet instead. WARNING: The 'CDDrives' property of VirtualMachine type is deprecated. Use 'Get-CDDrive' cmdlet instead. WARNING: The 'FloppyDrives' property of VirtualMachine type is deprecated. Use 'Get-FloppyDrive' cmdlet instead. WARNING: The 'Host' property of VirtualMachine type is deprecated. Use the 'VMHost' property instead. WARNING: The 'HostId' property of VirtualMachine type is deprecated. Use the 'VMHostId' property instead. WARNING: PowerCLI scripts should not use the 'Client' property. The property will be removed in a future release.
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:
PowerCLI C:\> Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Scope User
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:
PowerCLI C:\> Get-VM -Name A*
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:
PowerCLI C:\> Get-VM -Name ??e
You can also specify some specific characters, as shown in the following command:
PowerCLI C:\> Get-VM -Name [bc]*
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:
PowerCLI C:\> Get-VM -Name *[0-4]
The preceding command lists all the virtual machines that have names ending with 0, 1, 2, 3, or 4.
Filtering objects
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:
PowerCLI C:\> Get-VM | Where-Object {$_.NumCPU -gt 1}
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:
PowerCLI C:\> Get-VM | Where-Object NumCPU -gt 1
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:
Operator |
Description |
---|---|
|
Equals |
|
Not equals |
|
Greater than |
|
Greater than or equal to |
|
Less than |
|
Less than or equal to |
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.
Using aliases
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:
PowerCLI C:\> Get-VM | ? {$_.NumCPU -gt 1}
PowerCLI C:\> Get-VM | Where-Object NumCPU -gt 1
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:
PowerCLI C:\> Get-Alias
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:
PowerCLI C:\> New-Alias -Name childs -Value Get-ChildItem