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
123 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_
  • 2
3 Comments
 
LVL 39

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 39

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

11 Experts available now in Live!

Get 1:1 Help Now