【问题标题】:Import data from one CSV to another CSV in a specific column将数据从一个 CSV 导入到特定列中的另一个 CSV
【发布时间】:2019-01-17 12:54:33
【问题描述】:

我在从 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 模板,这也可以满足我的需求,我只是不知道该怎么做。

【问题讨论】:

    标签: powershell csv import import-csv


    【解决方案1】:

    我认为,您的$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] 添加到哈希表中。你可能想自己测试一下。

    对于你问题的第二部分,对整个事情进行管道化,这样的事情可能就是你所追求的:

    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 列的顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      相关资源
      最近更新 更多