Solved

question for powershell object

Posted on 2013-11-29
5
712 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

697 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