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
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
126 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 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A brief introduction to what I consider to be the best editor for PowerShell.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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