在PowerShell中将元素从一个数组复制到另一个数组

时间:2014-06-05 16:01:50

标签: arrays powershell

我试图将一个元素从一个多维数组复制到另一个多维数组(每个数组的元素都是一个例子):

(数组1)

EmpID        Badge        WorkEmail
1234         ABCD         john@acme.com
2345         JKLM         mary@acme.com
4567         QWER         paul@acme.com

(数组2)

EmpName        UserID        Email
John           1234          john@acme.com
Mary           JKLM          mary@acme.com
Linda          POIU          linda@acme.com
Paul           QWER          paul@acme.com

我为 array2 中的元素电子邮件创建了一个名为 WorkEmail 的别名:

$array2 = $array2 | Add-Member -MemberType AliasProperty -Name WorkEmail -Value "Email" -PassThru

我正在使用 Compare-Object 来比较这两个基于 WorkEmail 元素/别名的数组,我希望保留 array1 当 arrayE array2 中的 WorkEmail 相同时:

$arrayNew = @(Compare-Object $array1 $array2 -Property WorkEmail -IncludeEqual -passthru | Where-Object {$_.SideIndicator -eq '=='}) 

这一切都很好。当 workEmail array1 array2 中相同时,我会将 array1 中的匹配值复制到中arrayNew 。但是也可以在该复制过程中包含来自 array2 的元素(基本上从 array2 中获取元素并在时将它们添加到 array1 WorkEmail 匹配)?我真的只需要 arrayNew 中包含 array2 UserID ,但如果我可以合并两个数组(当匹配时)也为我工作。可以在这里完成任何事情 - 我无法弄明白或找到解决方案吗?

除了上面的原始数组值,还有其他修改 ):

理想情况下,我想最终得到以下多维数组:

(arrayNew)

EmpID        Badge        WorkEmail          UserID
1234         ABCD         john@acme.com      1234
2345         JKLM         mary@acme.com      JKLM
4567         QWER         paul@acme.com      QWER

(跳过Linda,因为两个数组中的 WorkEmail 元素都不匹配)

下面的另一个编辑,如@ Cole9350请求 - 希望它有所帮助,而不是太多 ):

$ADobjects | Get-Member | fl

(主要是回归)

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : Contains
MemberType : Method
Definition : bool Contains(string propertyName)

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : GetEnumerator
MemberType : Method
Definition : System.Collections.IDictionaryEnumerator GetEnumerator()

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : GetType
MemberType : Method
Definition : type GetType()

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : ToString
MemberType : Method
Definition : string ToString()

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : SideIndicator
MemberType : NoteProperty
Definition : System.String SideIndicator===

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : Item
MemberType : ParameterizedProperty
Definition : Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string
         propertyName) {get;}

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : DistinguishedName
MemberType : Property
Definition : System.String DistinguishedName {get;set;}

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : Name
MemberType : Property
Definition : System.String Name {get;}

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : ObjectClass
MemberType : Property
Definition : System.String ObjectClass {get;set;}

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : ObjectGUID
MemberType : Property
Definition : System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral,
         PublicKeyToken=b77a5c561934e089]] ObjectGUID {get;set;}

TypeName   : Microsoft.ActiveDirectory.Management.ADObject
Name       : userPrincipalName
MemberType : Property
Definition : System.String userPrincipalName {get;set;}

,而

$CSVobjects | Get-Member | fl

(主要是回归)

TypeName   : System.Management.Automation.PSCustomObject
Name       : UserPrincipalName
MemberType : AliasProperty
Definition : UserPrincipalName = Work Email

TypeName   : System.Management.Automation.PSCustomObject
Name       : Equals
MemberType : Method
Definition : bool Equals(System.Object obj)

TypeName   : System.Management.Automation.PSCustomObject
Name       : GetHashCode
MemberType : Method
Definition : int GetHashCode()

TypeName   : System.Management.Automation.PSCustomObject
Name       : GetType
MemberType : Method
Definition : type GetType()

TypeName   : System.Management.Automation.PSCustomObject
Name       : ToString
MemberType : Method
Definition : string ToString()

TypeName   : System.Management.Automation.PSCustomObject
Name       : EmpID
MemberType : NoteProperty
Definition : System.String EmpID=1234

TypeName   : System.Management.Automation.PSCustomObject
Name       : Job Title
MemberType : NoteProperty
Definition : System.String Job Title =Genius

TypeName   : System.Management.Automation.PSCustomObject
Name       : Name
MemberType : NoteProperty
Definition : System.String Name=Coyote, Wile E

TypeName   : System.Management.Automation.PSCustomObject
Name       : SideIndicator
MemberType : NoteProperty
Definition : System.String SideIndicator==>

TypeName   : System.Management.Automation.PSCustomObject
Name       : Work Email
MemberType : NoteProperty
Definition : System.String Work Email=john.doe@acme.com

我希望它会很简单,所以我最初保持这个例子更简单。现在,减去几个元素,这是我真正使用的数组的核心。我的目标是,根据 UserPrincipalName 成员元素(一个是别名)比较这两个数组,它将复制等于第三个数组的项目(保持原始数组不变)。除了将第一个数组中的匹配项复制到新数组之外,我还需要从第二个数组中复制成员元素 EmpID ,并将其添加到新创建的数组中(基本上结合使用)阵列在一起)。似乎COmpare-Object本身没有这种能力,所以我尝试使用像Add-Member这样的东西来做到这一点。但我这样做:

$NewArray | Add-Member -membertype NoteProperty -Name 'EmpID' -Value 'tempdata'

我最终得到了错误:

Add-Member : Cannot add a member with the name "EmpID" because a member with that name already
exists. If you want to overwrite the member anyway, use the Force parameter to overwrite it.
(etc...)

由于我不能像我想要的那样直接组合数据,我的意思是在新数组中添加一个新成员(使用 Compare-Object 创建),然后 ForEach < / strong>通过原始数组复制新数组所需的其他数据。

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

我不确定我理解这个问题。什么阻止你简单地添加它们?

D:\> $arr1=@('a','b','c')
D:\> $arr2=@('d','e','c')
D:\> $arr1+$arr2
a
b
c
d
e
c
D:\> $arr1+$arr2 | select -unique
a
b
c
d
e

答案 1 :(得分:0)

我最不得不做的是首先使用自定义对象创建 $ NewArray

[array]$NewArray = New-Object PSObject
Add-Member -InputObject $NewArray -membertype NoteProperty -Name 'CSVuserID' -Value ""
[array]$ NewArray = @()

我最初不得不使用 -Force 选项,因为我在 ForEach 循环中创建了对象。现在我在 Get-ADobject 之前和 Import-CSV cmdlet运行之前创建它。所以现在当我比较 $ array1 $ array2 时, Compare-Object cmdlet将自动添加所需的任何新成员元素并(展开) $ arrayNew 数组。我只需要为我计划在数组之间复制的任何东西创建新对象。

$arrayNew = @(Compare-Object $array1 $array2 -Property WorkEmail -IncludeEqual -passthru | Where-Object {$_.SideIndicator -eq '=='})

然后一些 ForEach 循环,我最终在 $ NewArray 中得到了所需的输出:

If ($arrayNew.Count -ge 1)
    {
    $arrayNew | ForEach-Object {
        ForEach ($obj in $array2) {
            If ($obj.Email.ToLower() -eq $_.Email.ToLower() -eq $True)
                {
                $_.CSVuserID = $obj.UserID
                Break
                }
            Else
                {
                Continue
                }
        }
    }
    }

感谢所有帮助