Solved

question for powershell object

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

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 69

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot Change Local DNS 9 41
Never expired checked 2 18
Powershell scheduled task with arguments 7 30
Power shell Script - Help v2 7 19
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
This article will help you understand what HashTables are and how to use them in PowerShell.
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 join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

815 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

8 Experts available now in Live!

Get 1:1 Help Now