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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Deleting folders and files using Powershell 4 39
share local drives of a different computer via powershell 1 25
Export AD group members. 1 28
Convert Variable to Array 4 16
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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…

726 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