Solved

after creating custom object, how can i add a new member ? what's wrong in the following code ?

Posted on 2015-01-06
3
130 Views
Last Modified: 2015-01-08
Hello!

I'm creating custom objects in a loop, my object has only two members. I can display, i know this part is working.
Then i want to add a third member, this time with Add-Member in the same loop.

the problem is that my third member is not display.

here's my function so far :


function Get_tshr_TestUsersInfos_InObject( [string]$aFilePath="M:\PS_scripts\machinesTest.otl" )
{
    $linesWithInfos = Get-Content $aFilePath

    $oMyCouples = @()

    foreach($lineOneMachine in $linesWithInfos)
      {
            $hostname = $lineOneMachine.Split(";")[0].Trim();
            $username = $lineOneMachine.Split(";")[1].Trim();

            $aCouple = New-Object -TypeName PSObject -Property ([Ordered] @{
                  "hostname3"=$hostname;
                  "username"=$username;
            })
       
        #$aCouple | Add-Member PropertySet -Name "Blabla" -Value 'adding extras infos'
        #$aCouple | Add-Member PropertySet -Name "enplus" @("adding extras infos")
        $aCouple | Add-Member PropertySet -Name "enplus" @("adding extras infos")
       
        $oMyCouples += $aCouple
      }

    return $oMyCouples
}

Here's what it display :
"hostname"      "username"
aHostName     aUserName
anotherHN      aSecName
....


no display of the third member.

thank you in advance for alll your help.

toshi
0
Comment
Question by:toshi_
[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
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
footech earned 500 total points
ID: 40533703
I think what you're trying to do is add another NoteProperty to the object.  So try the following:
$aCouple | Add-Member NoteProperty -Name "Blabla" -Value 'adding extras infos'

Open in new window

0
 

Author Comment

by:toshi_
ID: 40533719
that's it !

thank you ... but can you just help me understading the diff between NoteProperty and PropoertySet `?

if you think this is another question i close and give you the points, but it would be nice to have some explanation. thank you in advance.

Toshi
0
 
LVL 40

Expert Comment

by:footech
ID: 40533774
If you take an object and input it to Get-Member, you can see existing properties and methods for the object. For example, try piping $aCouple to Get-Member ( $aCouple | gm ).  Or for an even better example, examine the output from
get-process | gm -force

Open in new window


The following was taken from http://msdn.microsoft.com/en-us/library/system.management.automation.psmembertypes(v=vs.85).aspx
AliasProperty - A property that is an alias (new name) for another property of the object.
All - Any member of the PSObject object.
CodeMethod - A method that references a static method of the object.
CodeProperty - A property that references a static property of the object.
Dynamic - All dynamic members (where Windows PowerShell cannot determine the types of the members).
Event - An event of the PSObject object. This field is introduced in Windows PowerShell 2.0.
MemberSet - A predefined collection of properties and methods of the object, such as PSObject.
Method - A method of the underlying object.
Methods - All methods of the underlying object.
NoteProperty - A property of the object with a static value. Note that properties are defined as a name-value pair, such as "Color=Blue".
ParameterizedProperty - A property of the object that takes parameters and parameter values.
Properties - All properties of the object.
Property - A property of the underlying object.
PropertySet - A predefined collection of object properties.
ScriptMethod - A method whose value is the output of a script.
ScriptProperty - A property whose value is the output of a script.

It might also help you to review the full help for the Add-Member cmdlet.  With PS 3.0+ there is some alternative syntax for adding NoteProperties.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

724 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