Solved

question for powershell object

Posted on 2013-11-29
5
693 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

863 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

24 Experts available now in Live!

Get 1:1 Help Now