使用Add-Member从数组

时间:2016-08-31 06:07:42

标签: powershell

我正在使用脚本创建具有如下给定的某些值的新对象。这适用于$ Names返回单个字符串值的情况,但是当存在数组并且在打印$ newobj之前得到以下错误。当我为$ Names添加一个foreach语句时,试图找到导致错误的原因。另请注意,对于$ names数组中的John,Jake值,$ x的值与“London”相同。

在当前输出中,StudentName两次都打印为“John”,当我使用参数-Force时,StudentName的值都是“Jake”两次。我需要在输出中将这些值分开。有人帮我吗?

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 $_

脚本:

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

当前输出:

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

1 个答案:

答案 0 :(得分:0)

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