Creating your own objects

There are several ways to create new objects in PowerCLI. In fact, you have already been creating new objects by using the Select-Object -Property command. In the following section, you will learn more ways to create new objects.

Using the New-Object cmdlet

PowerShell has its own cmdlet to create objects: New-Object. You can use this cmdlet to create a Microsoft .NET Framework or COM object.

The New-Object cmdlet has the following syntax. The first parameter set is for creating a Microsoft .NET Framework object:

New-Object [-TypeName] <String> [[-ArgumentList] <Object[]>]
    [-Property <IDictionary>] [<CommonParameters>]

The -TypeName parameter is required.

The second parameter set is for creating a COM object:

New-Object [-ComObject] <String> [-Property <IDictionary>]
    [-Strict] [<CommonParameters>]

The -ComObject parameter is required.

Using a hash table to create an object

One way to create a Microsoft .NET Framework object as the output of your PowerCLI scripts is to create a PSObject type object using a hash table. The following example creates an object with two members of member type NoteProperty: Name and VMHost :

PowerCLI C:> $Object = New-Object -TypeName PSObject -Property @{
>> Name = "VM1"
>> VMHost = "ESX1"
>> }
>>
PowerCLI C:> $Object
Name                              VMHost
----                              ------
VM1                               ESX1

If you look at the object with Get-Member, you will see four standard methods, along with Name, VMHost, and NoteProperty:

PowerCLI C:> $Object | Get-Member
TypeName: System.Management.Automation.PSCustomObject
Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Name        NoteProperty System.String Name=VM1
VMHost      NoteProperty System.String VMHost=ESX1

The four methods Equals, GetHashCode, GetType, and ToString are methods that every PowerShell object has.

This technique is fast and easy-to-use. It has a disadvantage that PowerShell might output the properties in a different order than you added them. You can solve this problem by piping the object to Select-Object and specifying the properties there in the right order. For example:

PowerCLI C:> $Object | Select-Object -Property Name,VMHost
Name                              VMHost
----                              ------
VM1                               ESX1

Another way to output the properties in the order you entered them is to use an ordered hash table. Ordered hash tables are introduced in PowerShell V3. You can create an ordered hash table by putting [ordered] in front of the hash table, for example:

PowerCLI C:> $Hash = [ordered]@{
>> Name = "VM1"
>> VMHost = "ESX1"
>> }
>> $Object = New-Object -TypeName PSObject -Property $Hash
>>

Creating objects using the Select-Object cmdlet

If you use the Select-Object cmdlet to select only specific properties of an object, then you are creating a new object type. For example, the objects returned by the Get-VM cmdlet are of type VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl. However, if you pipe the output of the Get-VM cmdlet to Select-Object, and pipe that output to the Get-Member cmdlet, you will get the following output:

PowerCLI C:> Get-VM | Select-Object -Property Name | Get-Member


        TypeName:
    Selected.VMware.VimAutomation.ViCore.Impl.V1.VM.Universal
    VirtualMachineImpl
In the TypeName, you can see that the original type name is prefixed by 
    Selected. You can create a new object from scratch if you pipe an empty
    string to Select-Object as in the following example:
PowerCLI C:> $Report = "" | Select-Object -Property VM,VMHost
PowerCLI C:> $Report.VM = "VM1"
PowerCLI C:> $Report.VMHost = "ESX1"
PowerCLI C:> $Report

    VM                                VMHost
--                                ------
VM1                               ESX1

In the first line, you have created a new object $Report that has two blank properties VM and VMHost. In the second and third lines, values are assigned to these properties. In the last line, the $Report object is returned.

Note

This technique was used a lot in PowerShell V1 and you still see it being often used. In PowerShell V3 and above, I prefer using the method from the following section.

Creating objects using [pscustomobject]

The easiest and fastest way to create objects in PowerShell is to use the [pscustomobject] type declaration introduced in PowerShell V3. Put [pscustomobject] in front of a hash table, and the output will be a PowerShell object of type System.Management.Automation.PSCustomObject, for example:

PowerCLI C:> [pscustomobject]@{
>> Name = "VM1"
>> VMHost = "ESX1"
>> }
>>
Name                               VMHost
----                               ------
VM1                                ESX1

PowerShell will output the properties of a PSCustomObject sorted in the order you specified them.

Adding properties to an object with Add-Member

If you want to add one or more properties to an existing object, you can use the Add-Member cmdlet.

The Add-Member cmdlet has the following syntax. With the first parameter set, you can specify the type of the new member:

Add-Member [-MemberType] {AliasProperty | CodeProperty | Property | NoteProperty | ScriptProperty | Properties | PropertySet | Method | CodeMethod | ScriptMethod | Methods | ParameterizedProperty | MemberSet | Event | Dynamic | All} [-Name] <String> [[-Value] <Object>] [[-SecondValue] <Object>] [-Force] -InputObject <PSObject> [-PassThru] [-TypeName <String>] [<CommonParameters>]

The -MemberType, -Name, and -InputObject parameters are require.

The second parameter set can be used to add a single NoteProperty value:

Add-Member [-NotePropertyName] <String> [-NotePropertyValue] <Object> [-Force] -InputObject <PSObject> [-PassThru] [-TypeName <String>] [<CommonParameters>]

The -NotePropertyName, -NotePropertyValue, and -InputObject parameters are required.

You can use the third parameter set to add multiple NoteProperty values:

Add-Member [-NotePropertyMembers] <IDictionary> [-Force] -InputObject <PSObject> [-PassThru] [-TypeName <String>] [<CommonParameters>]

The -NotePropertyMembers  and -InputObject parameters are required.

The fourth parameter set can be used to modify the type of the input object:

Add-Member -InputObject <PSObject> [-PassThru] -TypeName <String> [<CommonParameters>]

The -InputObject and -TypeName parameters are required.

To create a new PowerCLI object, you can use the New-Object cmdlet to create a new object of type PSObject and then use the Add-Member cmdlet to add properties, for example:

PowerCLI C:> $Object = New-Object -TypeName PSObject
PowerCLI C:> $Object | Add-member -NotePropertyName VM
    -NotePropertyValue "VM1"
PowerCLI C:> $Object | Add-member -NotePropertyName VMHost
    -NotePropertyValue "ESX1"
PowerCLI C:> $Object


    VM                                VMHost
--                                ------
VM1                               ESX1

This technique was common in PowerShell V1. Because it is the slowest method to create PowerShell objects, it is not used often anymore.

The Add-Member cmdlet adds properties to the input object and does not generate output unless you specify the -PassThru parameter. If you want to use more than one Add-Member commands in a pipeline, don't forget to use the -PassThru parameter, for example:

PowerCLI C:> New-Object -TypeName PSObject |
>> Add-member -NotePropertyName VM -NotePropertyValue "VM1" -Passthru |
>> Add-member -NotePropertyName VMHost -NotePropertyValue "ESX1"
    -Passthru
>>


    VM                                 VMHost
--                                 ------
VM1                                ESX1

An advantage of the Add-Member cmdlet over the other methods to create PowerShell objects is that you can specify the type of the member to add. All other methods only create properties of type NoteProperty.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.230.107