Learning PowerCLI
上QQ阅读APP看书,第一时间看更新

Extending PowerCLI objects with the New-VIProperty cmdlet

Sometimes you can have the feeling that a PowerCLI object is missing a property. Although the VMware PowerCLI team tried to include the most useful properties in the objects, you can have the need for an extra property. Luckily, PowerCLI has a way to extend a PowerCLI object using the New-VIProperty cmdlet. This cmdlet has the following syntax:

New-VIProperty [-Name] <String> [-ObjectType] <String[]> [-Value] <ScriptBlock> [-Force] [-BasedOnExtensionProperty <String[]>] [-WhatIf] [-Confirm][<CommonParameters>]
New-VIProperty [-Name] <String> [-ObjectType] <String[]> [-Force] [-ValueFromExtensionProperty] <String> [-WhatIf] [-Confirm] [<CommonParameters>]

Let's start with an example. You will add the VMware Tools' running statuses used in a previous example to the VirtualMachineImpl object using the New-VIProperty cmdlet:

PowerCLI C:\> New-VIProperty -ObjectType VirtualMachine -Name ToolsRunningStatus -ValueFromExtensionProperty 'Guest.ToolsRunningStatus'

Name RetrievingType DeclaringType Value
---- -------------- ------------- -----
ToolsRunning... VirtualMachine VirtualMachine Guest.ToolsRunningStatus

Now you can get the tools' running statuses of all of your virtual machines with:

PowerCLI C:\> Get-VM | Select-Object -Property Name, ToolsRunningStatus

Isn't this much easier?

In the next example, you will add the vCenterServer property to the VirtualMachineImpl object. The name of the vCenter Server is part of the VirtualMachineImpl Uid property. The Uid property is a string that looks like /VIServer=domain\account@vCenter:443/VirtualMachine=VirtualMachine-vm-239/

You can use the Split() method to split the string. For example, the following command splits the string 192.168.0.1 into an array with four elements:

PowerCLI C:\> "192.168.0.1".Split('.')
192
168
0
1

The first element is 192, the second element 168, the third element 0, and the fourth and last element 1. If you assign the array to a variable then you can use an index to specify a certain element of the array:

PowerCLI C:\> $Array = "192.168.0.1".Split('.')

The index is 0 for the first element, 1 for the second element, and so on. If you want to specify the last element of the array, you can use the index -1. For example:

PowerCLI C:\> $Array[0]
192

In the Uid property, the name of the vCenter Server is between the @ sign and the colon. So you can use those two characters to split the string. First you split the string at the colon and take the part before the colon. That is the first element of the resulting array:

PowerCLI C:\> $Uid = '/VIServer=domain\account@vCenter:443/VirtualMachine=VirtualMachine-vm-239/'
PowerCLI C:\> $Uid.Split(':')[0]
/VIServer=domain\account@vCenter

Split the resulting part at the @ sign and take the second element of the resulting array to get the name of the vCenter Server:

PowerCLI C:\> $String = '/VIServer=domain\account@vCenter'
PowerCLI C:\> $String.Split('@')[1]
vCenter

You can do this splitting with one line of code:

PowerCLI C:\> $Uid = '/VIServer=domain\account@vCenter:443/VirtualMachine=VirtualMachine-vm-239/'
PowerCLI C:\> $Uid.Split(':')[0].Split('@')[1]
vCenter

Use the –Value parameter of the New-VIProperty cmdlet to specify a scriptblock. In this scriptblock, $Args[0] is the object with which you want to retrieve the name of the vCenter Server:

PowerCLI C:\> New-VIProperty -Name vCenterServer -ObjectType VirtualMachine -Value {$Args[0].Uid.Split(":")[0].Split("@")[1]} –Force

The New-VIProperty –Force parameter indicates that you want to create the new property even if another property with the same name already exists for the specified object type.

Now you can get a list of all of your virtual machines and their vCenter Servers with:

PowerCLI C:\> Get-VM | Select-Object -Property Name,vCenterServer