我正在尝试比较两个 csv 文件,第一个称为 ticket.csv 并包含列标题:“tcn”、“two”、“twod”。第二个文件名为 tracker.csv,包含标题:“cn”、“wo”和“wod”。我想输出tracker.csv的内容,如果有“tcn”(tickets.csv)匹配“cn”(tracker.csv),则将其关联的“二”(tickets.csv)写入“wo”( tracker.csv) 和 "twod" (tickets.csv) 到 "wod" (tracker.csv)。
我遇到了我的代码没有移动“两个”或“wod”的问题,并且它还编写了每个对象 4 次。
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"