2

I am using a script to create new objects with certain values given as below. This works in cases where $Names returns a single string value, however get’s the following error when there is an array and before printing $newobj. Trying to find what is causing the error as I added a foreach statement for $Names. Also note that, the value of $x is same as "London" for both values John, Jake in the $names array.

In the current output, StudentName is printed as “John” both times, and when I use parameter –Force, the value of StudentName is “Jake" both times. I would need to separate these values different in the output. Could someone help me please?

Add-Member : Cannot add a member with the name "StudentName" because a member with that name already exists. To overwrite the
member anyway, add the Force parameter to your command.
At E:\test.ps1:93 char:14
+             $newobj | Add-Member -MemberType NoteProperty -Name StudentName -Value $_

Script:

# $DCNames is an array

Foreach($x in $DCNames){
        $newobj = New-Object psobject
        $newobj | Add-Member -MemberType NoteProperty -Name DC -Value $x

        ######  $Names in most cases is a single string or it could be also an array. For example: it could be a value Adam or {John Jake….} and so I would need to Add each member in $Names as StudentName object below.
        $Names =@()
        $Names = $List | Where-Object{$_.Name -eq $x} | Select -ExpandProperty Name

        $Names | foreach {
            $newobj | Add-Member -MemberType NoteProperty -Name StudentName -Value $_   
            # $ClassName is a single string
            $newobj | Add-Member -MemberType NoteProperty -Name Class -Value $ClassName
            $newobj
            }

Current output:

Name: Vegas
StudentName: Adam
Class: 10

Name: London
StudentName: John
Class: 12

Name: London
StudentName: John (the script should take the second value “Jake” here from $Names array, but is not happening for some reason)
Class: 11
3
  • Just add it as a whole, be it an array or single value, don't enumerate it. I don't see any other options. Commented Aug 31, 2016 at 8:21
  • when i export $newobj to a CSV, i see System.Object[] for the same and can't read the actual values. How should i convert that to a readable array object? Commented Aug 31, 2016 at 8:50
  • Ah! CSV changes the deal. So you want to keep all names in one column cell which means you have to -join them in one string separated by a semicolon for example. Commented Aug 31, 2016 at 9:03

1 Answer 1

1

You create only 1 object, and attempt to overwrite the StudentName on that same object. Do it like this instead:

Foreach($x in $DCNames){
    ######  $Names in most cases is a single string or it could be also an array. For example: it could be a value Adam or {John Jake….} and so I would need to Add each member in $Names as StudentName object below.
    $Names = @()
    $Names = $List | Where-Object{$_.Name -eq $x} | Select -ExpandProperty Name

    $Names | foreach {
        $newobj = New-Object psobject
        $newobj | Add-Member -MemberType NoteProperty -Name DC -Value $x
        $newobj | Add-Member -MemberType NoteProperty -Name StudentName -Value $_ 
        $newobj | Add-Member -MemberType NoteProperty -Name Class -Value $ClassName
        $newobj
    }
}

Piping to Add-Member can be quite slow, you may want to use New-Object -Property @{} instead:

Foreach($x in $DCNames){
    ######  $Names in most cases is a single string or it could be also an array. For example: it could be a value Adam or {John Jake….} and so I would need to Add each member in $Names as StudentName object below.
    $Names = @()
    $Names = $List | Where-Object{$_.Name -eq $x} | Select -ExpandProperty Name

    $Names | foreach {
        $newobj = New-Object psobject -Property @{
            DC = $x
            StudentName = $_ 
            Class = $ClassName
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks much Mathias. The first example worked.. Regarding New-Object example, I would need to run few cmdlets and get other values based on $x, $Names , $_ and after the New-Object step. In this case, how should we add those new ones into $newobj?
@user3331975 create a bunch of hashtables to hold the data, then at the very end, create the objects based on these hashtables :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.