halr9000 Asked “can you explain this construct for me?”
[code]$myobj = “”| select-Object Server,Result[/code]
IMO this is the simplest way to create custom objects.
Effectively what happens is you set your $myobj to a empty string and pipe to select-object.
While this may seem odd, its actually quite useful because what select-object returns is
a PSCustomObject with the properties that you specify. This allows you to create you object
with defined properties that you can fill out later in one line.
I guess the “proper” way to create a custom object is to do this.
$myobj = new-object System.Object
$myobj | add-member -membertype noteproperty -name Server -value $sname
$myobj | add-member -membertype noteproperty -name Result -value $sResult
While this isnt that much more work… I think its harder to read so I go with my shortcut like
$myobj = “” | select-Object Server,Result
$myobj.Server = $sname
$myobj.Result = $sResult
I will give you one warning about select-object (not related, but while I’m on this subject.) select-object does Change the object type and therefore you lose methods and properties you may expect to be there. I have seen numerous post on the news groups that have this exact problem.
If you pipe Get-ChildItem to Get-member you get tons of methods and
properties for both System.IO.FileInfo and System.IO.DirectoryInfo
[code]4# get-childitem | gm[/code]
Now pipe it to select-object and do a Get-Member
5# (get-childitem | select-object Fullname,length) | gm
Name MemberType Definition
—- ———- ———-
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
FullName NoteProperty System.String FullName=C:\Windows\System32\0409
length NoteProperty length=null
Just be careful to remember this. I actually avoid using select-object to filter objects simply because of this. Instead… I just use the properties.