Uncategorized

Ability to customize VI objects

Introduction

 

New-VIProperty is commandlet that gives the ability to
customize all VI objects and make them more informative. For example it is
possible to add a property “OverallStatus” to each inventory object /VM host, virtual
machine, resource pool, cluster, etc./ that shows the overall status of the
concrete inventory item – green, gray, etc.

Let see how it works – first a new property is created using
the commandlet New-VIProperty. Afterward using the Get-Inventory commandlet retreive
the name and the overall status of all inventory items.

PS> New-VIProperty -ObjectType InventoryItem -Name
OverallStatus -ValueFromExtensionProperty ‘OverallStatus’

 

PS> Get-Inventory | select Name,OverallStatus |
Format-Table -AutoSize

Name                              OverallStatus

—-                              ————-

WinXP-1                                   green

WinXP-2                                   green

WinXP-3                                   green

WinXP-4                                   green

Cluster-1                                 green

Cluster-1                                 green

Cluster-1                                 green

…                                         …

…                                         …

…                                         …

 

Where does come from the information
about the overall status?

The VI SDK has a type that represents each infrastructure
object. PowerCLI works with objects that contain some subset of properties of
the corresponding SDK object. PowerCLI does not represent the whole SDK object
because its retrieval is time consuming operation and it would not be
practical.

PowerCLI gives the ability to obtain the full SDK objects by
the commandlet Get-View. Get-View commandlet returns vSphere .Net view objects.

Another way to retrieve a .Net View
representation of given PowerCLI object, is to refer to its
ExtensionData
property.

The ExtensionData property is “on-demand” property. This
means that the value of the property is initialized on the first referral to
it. This approach is used again because of the slow retrieval of the whole .Net
view object.

New-VIProperty commandlet gives the ability to define custom
property to VI types. These properties can refer directly property of the .Net
view object or can be powershell script block which evaluates to some value.

Each instance of the customized type or instance of
its inheritor types will has the customized property.

All customized properties are available during the
life of the powershell process or until they are removed using
Remove-VIProperty commandlet.

Type of custom properties

 

There are two types of custom properties – script properties
and properties based on extension data property.

Properties based on extension data

Property of this type refers directly property of the .Net
view object.

Creating custom properties on virtual machine type:

         
ToolsStatus – shows status of the VM Tools

         
ToolsVersion – shows version of the VM Tools

PS> New-VIProperty -ObjectType VirtualMachine -Name
ToolsStatus -ValueFromExtensionProperty ‘Guest.ToolsStatus’

PS> New-VIProperty -ObjectType VirtualMachine -Name
ToolsVersion -ValueFromExtensionProperty ‘Guest.ToolsVersion’

 

PS> Get-VM | Select Name,ToolsStatus,ToolsVersion

Name                                    ToolsStatus
ToolsVersion

—-                                    ———–
————

Env3-VC4u1-2-yavor                          toolsOk 8193

Env5-VC4-1-vbaruh                           toolsOk 8193

Env3-VC4.1KL.Next-1                         toolsOk 8193

TestMachin2003-32bit-CS             toolsNotRunning

testMachine_Ubuntu                          toolsOk 8193

KLNext-VisorEnv-VC2                         toolsOk 8193

Env3-VC4-1                                  toolsOk 8193

VC20_TM22-Win2003                   toolsNotRunning 8193

PS20_win2003                        toolsNotRunning

testMachine_2003_32bit                      toolsOk 8193

VC4.0                               toolsNotRunning 8193

TestRunner-Win7-64bit                       toolsOk 8193

TestRunner-Win2003_64bit                    toolsOk 8193

TestRunner-WinXP-SP3-32bit                  toolsOk 8193

Env4-VC41-VC1                               toolsOk 8193

testMachine_Vista_32bit             toolsNotRunning

…                                             …   …

…                                             …   …

…                                             …   …

 

The value passed to the parameter ValueFromExtensionProperty
is path to the sub-property of the .Net view object. In this example two
properties are created that refer properties from the virtual machine .Net view
object.

New-VIProperty performs verification for existence of
the referred .Net view property. If it does not exist the commandlet will write
an error.

For example creation of property that refers
“Guest.ToolsStatus” on type “Cluster” will throw an error. See:

PS> New-VIProperty -ObjectType Cluster -Name ToolsStatus
-ValueFromExtensionProperty ‘Guest.ToolsStatus’

New-VIProperty : 04.5.2010 г. 21:00:17    New-VIProperty       
Could not validate the specified path ‘Guest.ToolsStatus’ for any of the
following types

 (or their inheritors): ‘ClusterComputeResource’.

 

Creation of properties to ancestor types

 

Because SDK objects are hierarchal related it is possible to
define custom property on ancestor type and all its inheritors will have the
property too. In the following example the customized type is InventoryItem.
InventoryItem represents ManagedEntity SDK type. SDK types like VirtualMachine,
HostSystem,Folder, Datacenter inherit ManagedEntity, so they have all
properties defined in ManagedEntity.

Because of that it is possible to define property that
refers “OverallStatus” to InventoryItem and this property will appear in all
PowerCLI objects that inherits InventoryItem. Such types are VirtualMachine,
VMHost, Datacenter, Folder, Cluster, etc.

PS> New-VIProperty -ObjectType InventoryItem -Name
OverallStatus -ValueFromExtensionProperty ‘OverallStatus’

 

 

Creating of properties that point to array fields

.Net view objects have properties of array type. It is
possible to create customize field that points to such array fields. In that
case the custom property is an array too. In this example the custom property
points to all virtual machine files.

PS> New-VIProperty -ObjectType VirtualMachine -Name
FileList -ValueFromExtensionProperty ‘LayoutEx.File’

PS> $vm = Get-VM TestMachin2003-32bit-CS

PS> $vm.FileList

 

Key             : 0

Name            : [storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS.vmdk

Type            : diskDescriptor

Size            : 537

DynamicType     :

DynamicProperty :

Key             : 1

Name            : [storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS-flat.vmdk

Type            : diskExtent

Size            : 8477736960

DynamicType     :

DynamicProperty :

Key             : 2

Name            : [storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS-000001.vmdk

Type            : diskDescriptor

Size            : 327

DynamicType     :

DynamicProperty :

 

 

It is also possible to create property that points to
sub-field of array field. For example it is possible to create property that
will hold only file names of the virtual machine files instead of holding the
whole information for each file.

PS> New-VIProperty -ObjectType VirtualMachine -Name
FileNameList -ValueFromExtensionProperty ‘LayoutEx.File.Name’

PS> $vm = Get-VM TestMachin2003-32bit-CS

PS> $vm.FileNameList

 

[storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS.vmdk

[storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS-flat.vmdk

[storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS-000001.vmdk

[storage_114_58]
TestMachin2003-32bit-CS/TestMachin2003-32bit-CS-000001-delta.vmdk

Using aggregate function on array properties

New-VIProperty can create property that is aggregation on
array values or sub-properties of array values. For example it is possible to
create property that gives the sum of size of all virtual machine files.

PS>  New-VIProperty -ObjectType VirtualMachine -Name
TotalFileSize -ValueFromExtensionProperty ‘SUM LayoutEx.File.Size’

PS> $vm = Get-VM TestMachin2003-32bit-CS

PS> $vm.TotalFileSize

8478325916

 

In this example at the beginning of the property path
definition is defined the function that should be applied. Possible functions
are SUM,COUNT,AVG (AVERAGE), MIN, MAX, UNIQUE.

Similarly it is possible to define properties that shows the
size of the smallest file, of the largest file and the average file size.

PS> New-VIProperty -ObjectType VirtualMachine -Name
MaxFileSize -ValueFromExtensionProperty ‘MAX LayoutEx.File.Size’

PS> New-VIProperty -ObjectType VirtualMachine -Name
MinFileSize -ValueFromExtensionProperty ‘MIN LayoutEx.File.Size’

PS> New-VIProperty -ObjectType VirtualMachine -Name
AvgFileSize -ValueFromExtensionProperty ‘AVG LayoutEx.File.Size’

 

PS> $vm | select
TotalFileSize,MaxFileSize,MinFileSize,AvgFileSize | Format-Table -AutoSize

TotalFileSize MaxFileSize MinFileSize     AvgFileSize

————- ———– ———–     ———–

   8478325916  8477736960           0 403729805.52381

 

Script properties

 

Script properties are defined by name and script that will
evaluate on the first retrieving of the property.

Properties that refers persistent properties of the target object

The simplest example is property that refers to one or more
properties of the customized type. This example creates a property that
evaluates to the name of the VM Host in which the virtual machine resides.

PS> New-VIProperty –Name NameOfHost –ObjectType
VirtualMachine –Value { return $args[0].VMHost.Name }

 

PS> get-VM | select Name,NameOfHost | Format-Table
-AutoSize

Name                              NameOfHost

—-                              ———-

Env3-VC4u1-2-yavor                10.23.114.187

Env5-VC4-1-vbaruh                 10.23.114.187

Env3-VC4.1KL.Next-1               10.23.114.58

TestMachin2003-32bit-CS           10.23.114.58

testMachine_Ubuntu                10.23.114.58

KLNext-VisorEnv-VC2               10.23.114.187

Env3-VC4-1                        10.23.114.187

VC20_TM22-Win2003                 10.23.114.162

PS20_win2003                      10.23.114.102

…                                 …

…                                 …

…                                 …

 

How should be the script block structured?

When vSphere PowerCLI commandlet see that it should evaluate
script property for given instance it passes that instances as parameter to the
script block. Because of that the object can be referenced as $args[0] inside
the script block.

It is possible to give name of the first parameter using the
Powershell key word param. See the above example written in
better way:

PS> New-VIProperty –Name NameOfHost –ObjectType
VirtualMachine –Value { param ($vm) return $vm.VMHost.Name }

 

Here the first parameter is named as $vm and this name is
used inside the whole script block.

Script-based properties that refers values from extension data

 

As it shown in the above example, inside the script block,
it is possible to work with each property of the customized type. This mean
that it is possible to work with the new property ExtensionData. As it was
mention above the retrieving of the whole ExtensionData is slow operation. This
slow initialization can be easily observed in the following example.

PS> New-VIProperty -Name ToolsStatus -ObjectType VirtualMachine
-Value { param ($vm) return $vm.ExtensionData.Guest.ToolsStatus; }

PS> Get-VM | select Name,ToolsStatus

…   … …

 

After calling the above command it can be observed that
there is some delay before each new row is displayed. This delay is caused by
the whole retrieval of the ExtensionData object.

To avoid this slow initialization New-VIProperty gives an
option to specify which properties from the ExtensionData to be retrieved. In
this way only the needed portion of the .Net view object will be retrieved and
the delay will be smaller.

This mechanism is called “hinting”.

PS> New-VIProperty -Name ToolsStatus -ObjectType
VirtualMachine -Value { param ($vm) return $vm.ExtensionData.Guest.ToolsStatus;
} -BasedOnExtensionProperty ‘Guest.ToolsStatus’

 

PS> Get-VM | Select Name,ToolsStatus

 

This time the above call will finish without any obvious
delay. This is that because during this call only the “Guest.ToolsStatus” is
retrieved from the ExtensionData property.

This can be easily checked if the script block returns value
from property that is not defined to parameter “BasedOnExtensionProperty’. See
the example:

PS> New-VIProperty -Name ToolsVersion -ObjectType
VirtualMachine -Value { param ($vm) return $vm.ExtensionData.Guest.ToolsVersion;
} -BasedOnExtensionProperty ‘Guest.ToolsStatus’

 

PS> Get-VM | select Name,ToolsVersion

Name                              ToolsVersion

—-                              ————

Env3-VC4u1-2-yavor

Env5-VC4-1-vbaruh

Env3-VC4.1KL.Next-1

TestMachin2003-32bit-CS

testMachine_Ubuntu

KLNext-VisorEnv-VC2

Env3-VC4-1

VC20_TM22-Win2003

PS20_win2003

 

More on ExtensionData initialization

It should be noticed that ExtensionData is partially filled
up only during the custom properties initialization. Let see the following
sequence of commands:

PS> New-VIProperty -Name ToolsStatus -ObjectType
VirtualMachine -Value { param ($vm) return $vm.ExtensionData.Guest.ToolsStatus;
} -BasedOnExtensionProperty ‘Guest.ToolsStatus’

PS> $vm = Get-VM | Select –First 1

# At this point ExtensionData of the $vm instance is not
initialized

 

PS> $vm.ToolsStatus

toolsOk

# At this point ExtensionData property is initialized, but it is not
fully filled up. Two different list of fields are filled up:

 – Fields needed to be initialized persistent properties
of the instance type

 – Fields pointed to BasedOnExtensionProperty parameter
– in this example ‘Guest.ToolsStatus’

 

PS> $vm.ExtensionData.Guest.ToolsVersion

# At this point the whole ExtensionData is filled up.
The reason is the direct access to the ExtensionData property which activates
the full retrieval.

Other examples of script based properties

Adding property to all VI objects that shows the name of the
server to which the object belongs.

PS> New-VIProperty -ObjectType VIObjectCore  -Name
VIServerName -Value { param($obj) if ($obj.UId -match
"/VIserver=[\w]+@(.*):.*" ) { return $matches[1] } else { return
"" } }

 

PS> get-vm test* | select Name,VIServerName  |
Format-Table -AutoSize

Name                              VIServerName

—-                              ————

TestMachin2003-32bit-CS           10.23.115.246

…                                 …

 

PS> get-vmhost | select Name,VIServerName | Format-Table
-AutoSize

Name          VIServerName

—-          ————

10.23.114.187 10.23.115.246

10.23.114.102 10.23.115.246

10.23.114.162 10.23.115.246

10.23.114.58  10.23.115.246

 

Creating properties that show the total committed space in
MB and the total un-committed space in MB.

PS> New-VIProperty -ObjectType VirtualMachine -Name CommittedSpaceMB
-Value { param($vm) $sum = 0; $vm.ExtensionData.Storage.PerDatastoreUsage |
foreach { $sum += $_.Uncommitted} ; $sum = [int]($sum / 1024 / 1024); return
$sum } -BasedOnExtensionProperty ‘Storage.PerDatastoreUsage.Uncommitted’

 

PS> New-VIProperty -ObjectType VirtualMachine -Name
UncommittedSpaceMB -Value { param($vm) $sum = 0;
$vm.ExtensionData.Storage.PerDatastoreUsage | foreach { $sum += $_.Uncommitted}
; $sum =

[int]($sum / 1024 / 1024); return $sum }
-BasedOnExtensionProperty ‘Storage.PerDatastoreUsage.Uncommitted’

 

PS> get-vm test* | select
Name,CommittedSpaceMB,UncommittedSpaceMB | ft -AutoSize

Name                              CommittedSpaceMB
UncommittedSpaceMB

—-                              —————-
——————

TestMachin2003-32bit-CS                      
8086              20662

testMachine_Vista_32bit                     
32114              14320

testMachine_Ubuntu                           
3072                  0

testMachine_Vista_32bit_2                     2048               
512

TestRunner-Win7-32bit                       
22529                  0

Pre-defining custom properties

 

When defining a custom property if there is already defined
property to the same type with the same name New-VIProperty will fail. It is
possible to override definition of existing property using the –Force switch.

Getting and removing custom properties

vSphere PowerCLI has cmdlets Get-VIProperty and Remove-VIProperty.

Both cmdlets allow to filter defined custom properties by type and name.

For example, the following line will remove all defined properties for
the type VirtualMachine

PS> Remove-VIProperty –ObjectType VirtualMachine –Name *

 

The following line will remove all properties that contains
“space” in their names in all types.

PS> Remove-VIProperty –ObjectType * -Name *space*

Cheers!!!
PowerCLI Team