Introduction
Sometimes, there may be a need to collect similar data from different sources and then display that day in a simple way. This can be accomplished using generic, custom objects. This posting walks through the steps for building a simple custom collection object that displays a listing of objects and their properties.Procedure
- Create an empty collection object. Creating a simple system.object is not enough. This type of object cannot contain a suite of objects: it can only contain a single object. What is needed is an object that can in turn contain other objects. For this, use an instance of the System.Collections.ArrayList class, declared like so:
$MyCollection = New-Object System.Collections.ArrayList
Leaving off the parentheses creates an empty collection. Here's a short hand way of declaring the same thing:$MyCollection = @()
To declare a finite collection object, use the ArrayList constructor like so:$MyCollection = New-Object System.Collections.ArrayList(N)
where N is some integer. Or it's possible to rely upon the default value of the constructor to create a typed array, like so:$MyCollection = new-object system.object[] N
where N is again any integer. Note: this method works for the other types as well (cfi, system.boolean).
- Create an object with the members you want. next task is to create an object that contains the properties and values you want to save. Another name for this is Key:Value pairs. There are several methods for creating such an object. One method is to create a standard system.object and then add members to it. This is the formal textbook script for doing this:
$MyCollection1 = New-Object System.Object
To do a quick check on the values of this object, just use:
$MyCollection1 | Add-Member -type "NoteProperty" -name "Property Name" -value "This is property #1"
$MyCollection1 | Add-Member -type "NoteProperty" -name "Property Value" -value "Red"
$MyCollection1 | Add-Member -type "NoteProperty" -name "Property Comment" -value "Always used"$MyCollection1 | ft -auto
This is what would be seen in the command shell:$MyCollection2 = New-Object System.Object
After executing the above script, something like this would be seen in the command shell:
$MyCollection2 | Add-Member -type "NoteProperty" -name "Property Name" -value "This is property #2"
$MyCollection2 | Add-Member -type NoteProperty -name "Property Value" -value "Blue"
$MyCollection2 | Add-Member -type NoteProperty -name "Property Comment" -value "Sometimes used"
$MyCollection2 | ft -auto
$MyCollection3 = New-Object System.Object
$MyCollection3 | Add-Member -type NoteProperty -name "Property Name" -value "This is property #3"
$MyCollection3 | Add-Member -type NoteProperty -name "Property Value" -value "yellow"
$MyCollection3 | Add-Member -type NoteProperty -name "Property Comment" -value "Never used"
$MyCollection3 | ft -auto
$MyCollection = New-Object System.Collections.ArrayList
Or, in shorthand:
$MyCollection = $MyCollection + $MyCollection1
$MyCollection = $MyCollection + $MyCollection2
$MyCollection = $MyCollection + $MyCollection3$MyCollection = @()
The command shell will now display this:
$MyCollection += $MyCollection1
$MyCollection += $MyCollection2
$MyCollection += $MyCollection3 - Retrieve object values. The objects are added to the collection and are indexed in the order in which they were added. To list the contents of the collection, simply enter the collection name itself and pipe it into the Format-Table object, like so:
$MyCollection | Format-Table -autowhich will be displayed in the shell as:
Once the data has been added to a collection object, use standard collection indexing and object notation to retrieve collection values. For example, to retrieve the first object that was added to the collection, just enter:
$MyCollection[0]To retrieve the value of the Property Name field for this object, just enter:
$MyCollection[0]."Property Name"Quotation marks had to be used in this instance due to the field name containing two words. Anyway, this works for all the other object fields as well:
Here's what it would look like if single-word property names had been used:
Note how easy it is then to access specific object member values.
References
- MSDN: Object Class
- MSDN: ArrayList Class
- MSDN: Boolean Structure
- SS64: Add-Member
- Windows PowerShell Tip of the Week: Working With Custom Objects
- Windows PowerShell Tip of the Week: Even More Things You Can Do With Arrays
- Windows PowerShell Tip of the Week: Working with Hash Tables
- C# array of different objects
- Windows PowerShell Blog: New-Object PSObject –Property [HashTable]
No comments:
Post a Comment