将特定列中的数据从一个 CSV 导入到另一个 CSV

Import data from one CSV to another CSV in a specific column

我在从 1 个 CSV 复制数据并将其粘贴到另一个 CSV 的模板时遇到了一些问题。 该模板具有特定的列名称。 以及我拥有的包含数据的 csv 文件,我能够获取每一列,但我无法将其粘贴到模板中。

我正在尝试将以下数据从 1 个 csv 复制到模板,这里是列

email --> internal_customer_ID  
givenname --> first_name
surname --> last_name
mail --> email_address
mobilephone --> mobile_phone_1
officePhone --> landline_phone_1

这是我当前的代码。

#Clear Screen
CLS

#The path we are working with (use the path where we execute this script from)
$global:path = Split-Path $script:MyInvocation.MyCommand.Path

$DataFile = $path + "\dataFile.csv"
$ExportedFileCSV = $path + "\PopulatedTemplate.csv"

#Export the data file with our user info
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone | Export-Csv -path $DataFile -NoTypeInformation -Force

$dataInput = Import-Csv $DataFile
$dataOutput = Import-Csv $ExportedFileCSV

$dataInput | ForEach-Object {

    $newData = $_

    $dataOutput | 
    Add-Member -MemberType NoteProperty -Name "internal_customer_ID" -Value $newData.Mail -PassThru -Force|
    Add-Member -MemberType NoteProperty -Name "landline_phone_1" -Value $newData.OfficePhone -PassThru -Force|
    Add-Member -MemberType NoteProperty -Name "email_address" -Value $newData.Mail -PassThru -Force|
    Add-Member -MemberType NoteProperty -Name "mobile_phone_1" -Value $newData.MobilePhone -PassThru -Force|
    Add-Member -MemberType NoteProperty -Name "last_name" -Value $newData.SurName -PassThru -Force|
    Add-Member -MemberType NoteProperty -Name "first_name" -Value $newData.GivenName -PassThru -force

} | Export-CSV $ExportedFileCSV

如果我能避免首先导出数据文件并仅附加

的结果
Get-ADGroupMember -Identity SomeGroup | Get-ADUser -Properties * | Select-Object -Property GivenName, SurName, Mail, MobilePhone, OfficePhone

直接使用 csv 模板,这也能满足我的需要,我只是不确定该怎么做。

我认为你的行阅读 $dataOutput | Add-Member ... 是问题所在。 Add-Member是为单个对象添加属性,而$dataOutput此时是对象的集合。我认为解释器认为您正在尝试将成员属性添加到对象数组。

尝试为每个输出记录创建一个新对象,然后对输出 CSV 文件执行 Export-CSV -append

我认为这样的事情应该可行:

$dataInput | ForEach-Object {

    $newData = $_
    $newRecordProperties = [ordered]@{
                    "internal_customer_ID"=$newData.Mail
                    "landline_phone_1" = $newData.OfficePhone 
                    "email_address" = $newData.Mail
                    "mobile_phone_1" = $newData.MobilePhone 
                    "last_name" = $newData.SurName
                    "first_name" = $newData.GivenName
                    }
    $newRecord = new-object psobject -Property $newRecordProperties
    Write-Output $newRecord

} | Export-CSV $ExportedFileCSV -Append

只要输出 CSV 中的列名与您的新记录对象相同,我认为就可以了。我不确定如果 $ExportedFileCSV 中的列与导出的 $newRecord 中的列顺序不同会发生什么,所以我将 [ordered] 添加到散列 table 中。您可能想自己测试一下。

对于你问题的第二部分,对整个事情进行流水线处理,这样的事情可能就是你想要的:

Get-ADGroupMember -Identity SomeGroup | 
    Get-ADUser -Properties * | 
    Select-Object -Property @(
        @{label="internal_customer_ID"; expression={$_.Mail}}
        @{label="email_address";        expression={$_.Mail}}
        @{label="landline_phone_1";     expression={$_.OfficePhone}}
        @{label="first_name";           expression={$_.GivenName}}
        @{label="last_name";            expression={$_.SurName}}
        @{label="mobile_phone_1";       expression={$_.MobilePhone}}
     ) |
    Export-Csv $ExportedFileCSV -Append
上面的

Select-Object 创建了一个自定义对象,其属性名称和属性值匹配 label 以及 expression 的结果。同样,重新排序以匹配 CSV 列的顺序。