I am trying to compare two csv files, the first is called tickets.csv and contains the column headers: "tcn", "two", "twod". The second file is called tracker.csv and contains the headers: "cn", "wo", and "wod". I would like to output the contents of tracker.csv, and if any "tcn" (tickets.csv) match "cn" (tracker.csv), then write its associated "two" (tickets.csv) to "wo" (tracker.csv), and "twod" (tickets.csv) to "wod" (tracker.csv).
I'm running into the issue where my code isn't moving the "two" or "wod", and its also writing each object 4 times.
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"
I was looking in the wrong direction before. But I figured it out.
$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