?
Solved

question for powershell object

Posted on 2013-11-29
5
Medium Priority
?
735 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 70

Assisted Solution

by:Qlemo
Qlemo earned 2000 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 70

Accepted Solution

by:
Qlemo earned 2000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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