user3331975 user3331975 - 3 months ago 21
PowerShell Question

using Add-Member to create object from an array

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

Answer

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