比较多个电子表格并生成 CSV 输出

Compare Multiple Spreadsheets and Produce CSV output

我正在尝试比较两个 csv 文件,第一个名为 tickets.csv 并包含列 headers:"tcn"、"two"、"twod" .第二个文件名为 tracker.csv,包含 headers:"cn"、"wo" 和 "wod"。我想输出 tracker.csv 的内容,如果有 "tcn" (tickets.csv) 匹配 "cn" (tracker.csv),则写入其关联的 "two" (tickets.csv) 到 "wo" (tracker.csv),以及 "twod" (tickets.csv) 到 "wod" (tracker.csv)。

我 运行 遇到了我的代码没有移动 "two" 或 "wod" 的问题,并且每个 object 都写了 4 次。

Input CSVs

Import-Csv "$home\Desktop\nestTest\tracker.csv" |
ForEach-Object {
    $machine = $_.machine
    $wo = ""
    $wod = ""
    $tickets = import-csv "$home\Desktop\nestTest\tickets.csv"
    foreach ($ticket in $tickets) {
        try {
        $tcn = ""
        $two = ""
        $twod = ""
        $two = $_.wo
        $twod = $_.wod
        $tcn = $_.cn
        if ($tcn -eq $machine) {
            $wo = $two
            $wod = $twod
        }
        else {
        $wo = ""
        $wod = ""
        }
        }
        catch {
        }
        [pscustomobject]@{
        "Machine Name"=$machine
        "Work Order #"=$wo
        "Work Order Date"=$wod
        }
     }
} |
        select "Machine Name","Work Order #","Work Order Date" |
        Export-Csv "$home\Desktop\nestTest\output.csv"

我之前看错方向了。但我想通了。

$tracker = Import-Csv "$home\Desktop\nestTest\tracker.csv" |
ForEach-Object {
    $machine = ""
    $wo = ""
    $wod = ""
    $machine = $_.machine
    $tickets = import-csv "$home\Desktop\nestTest\tickets.csv" |
    ForEach-Object {
        $two = ""
        $twod = ""
        $two = $_."wo"
        $twod = $_."wod"
        $tcn = $_."cn"
        if ($tcn -eq $machine) {
            $wo = $two
            $wod = $twod
        }
        else {
        }
    }
        [pscustomobject]@{
        "Machine Name"=$machine
        "Work Order #"=$wo
        "Work Order Date"=$wod
        }

} |
select "Machine Name","Work Order #","Work Order Date" |
Export-Csv "$home\Desktop\nestTest\output.csv" -NoTypeInformation