Difference between PSCustomObject Vs ManagementObject

by jmp8600 at 2013-03-19 11:03:48

Hi There,

Can someone please tell me difference between PSCustomObject Vs ManagementObject? I am asking this because in Don’s book Powershell lunches page 217 he is showing us to use New-Object cmdlet and create variables as PSCustomObject. like below:

$obj = New-Object -TypeName PSObject

in previous examples(listing 19.3 and 19.4) we tried something like below:
$os = Get-WmiObject Win32_OperatingSystem -computer $computername |
Select @{l=‘ComputerName’;e={$.__SERVER}}, BuildNumber,ServicePackMajorVersion

when i do $os|gm its of type TypeName: Selected.System.Management.ManagementObject

Now i can also add members to ManagementObject same way as we are doing with PSCustomObject(listing 19.5) with below command and its much easier approach then defining PSCustomObject as we can use existing variable which already have most of the properties we want.
$os|Add-Member -membertype NoteProperty -Name DiskInfo -Value $disk.SysDriveFree

hopefully i am not creating confusion here…

thx,
-jp
by poshoholic at 2013-03-19 11:53:42
In both cases, you’re working with the PowerShell Extended Type System (ETS). PowerShell’s ETS allows you to add members to any object. In the case of New-Object -TypeName PSObject, you’re creating a new object (of type PSCustomObject) that doesn’t have any members, and then adding members to it. In other cases, such as using Select-Object or just working with an object directly, you’re adding members to an object of another type (in your example, one of type Selected.System.Management.ManagementObject) that already has other members. The type name gives you an indication where the object came from.
by MasterOfTheHat at 2013-03-19 12:10:40
Each one has it’s own merits, jmp. It’s sometimes easier, at least for me, to work with a smaller custom object than to add several properties to an existing object. Also, in my mind it helps with performance to keep a smaller object in memory than the big one when all I want are a few properties. In reality, with all of the memory that current machines have, it probably doesn’t matter. And then there’s the programmer in me who is used to creating his own objects and just can’t help himself!

With Don’s permission, maybe you could share the context of the listing on pg 217 and we could see if there was a reason he chose to use a custom object instead of modifying an existing one? I haven’t bought his book, (sorry DonJ!).
by jmp8600 at 2013-03-19 12:23:45
Charles,

Below is the script from page 217:

function Get-ServerInfo {
param (
$computername = ‘localhost’
)
$os = Get-WmiObject <br>? Win32_OperatingSystem -computer $computername<br>$disk = Get-WmiObject Win32_LogicalDisk -filter &quot;DeviceID='C:'&quot;
-computer $computername
$obj = New-Object -TypeName PSObject
$obj | Add-Member -MemberType NoteProperty <br>-Name ComputerName -Value $computername<br>$obj | Add-Member -MemberType NoteProperty
-Name BuildNumber -Value ($os.BuildNumber)
$obj | Add-Member -MemberType NoteProperty <br>-Name SPVersion -Value ($os.ServicePackMajorVersion)<br>$obj | Add-Member -MemberType NoteProperty
-Name SysDriveFree -Value ($disk.free / 1MB -as [int])
Write-Output $obj
}
Get-ServerInfo | Format-Table -auto

I created below instead:

Function Get-ServerInfo {
Param (
$computername = ‘localhost’
)
$os = Get-WmiObject Win32_OperatingSystem -computer $computername |
Select @{l=‘ComputerName’;e={$
.__SERVER}},
BuildNumber,ServicePackMajorVersion
$disk = Get-WmiObject Win32_LogicalDisk -filter "DeviceID=‘C:’" `
-computer $computername |
Select @{l=‘SysDriveFree’;e={$_.FreeSpace / 1MB -as [int]}}


$os|Add-Member -membertype NoteProperty -Name DiskInfo -Value $disk.SysDriveFree
write-output $os

}
Get-ServerInfo|ft -autosize
by mjolinor at 2013-03-19 12:28:35
I don’t think object size is really an issue in this case. The object type indicates it is a selected object, meaning it was derived from a WMI object using select-object. Once you do this, it is essentially the same as a PS Custom object, having just the properties selected. The property types will be NoteProperties, and it won’t have any methods.
by jmp8600 at 2013-03-19 13:20:08
Thanks!!!