Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
134 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 41

Accepted Solution

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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ā€¦
Screencast - Getting to Know the Pipeline

971 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