Solved

question for powershell object

Posted on 2013-11-29
5
686 Views
Last Modified: 2013-12-04
I'm studying Powershell, was confused about following codes;

$obj = New-Object -TypeName PSObject -Property @{'prop1'='val1'}

When I do;
$obj | gm

it shows the $ojb is System.Management.Automation.PSCustomObject.

First, I created PSObject, but why it shows as PSCustomObject which is PSObject's base object class?

Second, The following code works and add MOL.SystemInfo as another object type, but PSCustomOjbect doesn't have the method 'PSObject' and Powershell ISE intellisense doesn't work. Is that 'PSObject' is some kind of casting function? Where can I find the documentation in MSDN about how this works?

$obj.PSObject.TypeNames.Insert(0,'MOL.SystemInfo')
0
Comment
Question by:crcsupport
  • 3
  • 2
5 Comments
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 500 total points
ID: 39686333
Firstly, PsObject is the base class. As soon as you add properties, the type changes to PsCustomObject (not the other way round).

Secondly, what is it that you want to achieve by adding the type? You usually add a typed value as property, like with
$obj | Add-Member NoteProperty NewProp 0

Open in new window

0
 
LVL 1

Author Comment

by:crcsupport
ID: 39691065
$obj.PSObject.TypeNames.Insert(0,'MOL.SystemInfo')

$obj becomes PSCustomObject, but PSCustomObject doesn't have any property or method of 'PSObject'. How can the above be justified when API or intellisense shows nothing about? I couldn't find any article about why the $obj is accessed through '.PSObject'. Where does it come from?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39691309
You are mixing up things. «obj».«base class» isn't a standard construct in PowerShell. It is only that PS implements hidden members for PsCustomObject, which can be seen with $obj | gm -Force. Details of each MemberSet (like PsObject can be retrieved with $obj.PsObject | gm. Such things are needed for formatting, debugging and similar stuff.

So, to be clear, PsCustomObject implements an accessor for PsObject, to allow to have access to the underlying object class the custom object is derived from.

That you can't see them usually (and also don't get Intellisense info) is based on the assumption you do not need access to the raw data in most cases, as almost everything is done "behind the scenes", and just works as you would expect.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 39696479
I searched and read multiple articles about psobject, pscustomobject and how to access membersets, none of the 1-3 page articles were clear as your short explanation.
Maybe you start writing your own book.

Thank you, Qlemo.
0
 
LVL 1

Author Comment

by:crcsupport
ID: 39696509
If anyone encounters the same problem as I had, below is my note that I want to keep;

$obj = New-Object -TypeName PSObject -Property $props
$obj.PSObject.TypeNames.Insert(0,'MOL.SystemInfo') :

- PSObject is base class. Once property is added, it becomes PSCustomObject.
- Now $obj is PSCustomObject. To access members of its base class, use '$obj.PSObject' .PSObject is MemeberSet of $obj allows to access properties of its base class. It's hidden. To show all membersets of its base class, do '$obj | gm -force'
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now