我将如何使用 powershell 从两个不同的 csv 文件中提取数据,然后使用该数据用我提取的数据创建一个新的 csv 文件?

How would I use powershell to pull data from two different csv files and then use that data to create a new csv file with the data I've pulled?

我不确定我是否走在正确的轨道上。所以我得到了一项任务,我有两份关于两个不同 cvs 文件的报告,这些文件包含以下信息:

一个csv保存了这些headers下的支付信息:

Payments.csv:

id,employee_id,payment,code 

另一个使用以下headers保存员工信息:

Employee.csv:

id,first_name,last_name,email,city,ip_address

这里的主键是employee.csv文件中的id,而外键是中的employee_id payments.csv 文件。这意味着 employee.csv 文件中的 id 应该与 payment.csv 中的 employee_id 匹配。

根据这些信息,我应该创建 2 个 classes。其中一个是 class 员工,他使用 employee.csv 文件中的信息创建了 objects。另一个是使用 payments.csv.

创建 objects 的付款 class

然后我需要比较两组 objects,其中 employee.csv 上的 id 等于 payments.csv 上的 employee_id。然后我想使用这些数据创建一个新的 csv,将两个 csv 文件上的数据合并到一个文件中,其中 employee.csv 的员工链接到他们在 payments.csv.

的付款

感谢任何帮助或指导!这就是我到目前为止所做的。我可能离题很远,所以请不要评判。只是想学习。在能够创建员工和付款后,我在具体做什么方面遇到了障碍 objects。

#Class that creates employee object

class Employee {

    [Int]$id
    [String]$first_name
    [String]$last_name
    [String]$email
    [String]$city
    [String]$ip_address

    Employee ([Int]$id,[String]$first_name,[String]$last_name,[String]$email,[String]$city,[String]$ip_address){

        $This.id = $id
        $This.first_name = $first_name
        $This.last_name = $last_name
        $This.email = $email
        $This.city = $city
        $This.ip_address = $ip_address
    }
}

#Class that creates payment object

class Payment{

    [Int]$id
    [Int]$employee_id
    [String]$payment
    [String]$code

    Payment ([Int]$id,[Int]$employee_id,[String]$payment,[String]$code){

        $This.id = $id 
        $This.employee_id = $employee_id
        $This.payment = $payment
        $This.code = $code
    }
}

#Importing spreadsheets w/ data being used

$ImportedEmployees = Import-Csv ".\Employee.csv" 
$ImportedPayments = Import-Csv ".\Payment.csv"
$FinalEmployeeReport = @{}

#Calling [Employee] to create new objects using the employee.csv

Foreach ($Employee in $ImportedEmployees){

    $NewEmployeeEntry = [Employee]::new([Int]$Employee.id,[String]$Employee.first_name,[String]$Employee.last_name,[String]$Employee.email,[String]$Employee.city,[String]$Employee.ip_address)

    #Adding object to $FinalEmployeeReport

    $FinalEmployeeReport.Add([String]$NewEmployeeEntry.last_name,[Int]$NewEmployeeEntry.id)
}

Foreach ($Payment in $ImportedPayments)
{
    $NewPayment = [Payment]::new([Int]$Payment.id,[Int]$Payment.employee_id,[String]$Payment.payment,[String]$Payment.code)

    $FinalEmployeeReport.Add[Int]$Payment.employee_id,[String]$Payment.payment))
}


Foreach($Payment in $ImportedPayments){

    $NewPayment = [Payment]::new([Int]$Payment.id,[Int]$Payment.employee_id,[String]$Payment.payment,[String]$Payment.code)

    Foreach($NewEmployeeEntry in $Payment){

        if($NewPayment.employee_id -eq $NewEmployeeEntry.id ){

            $NewEmployeeEntry.Add($NewPayment)  
        }
    } 
} 

$FinalEmployeeReport.Add($NewEmployeeEntry)

实现此目的的一种方法是使用一个简单的循环并在内部根据员工 ID 查找匹配记录。

如果这些是您的输入文件

payments.csv

id,employee_id,payment,code
123,8765,1500,abc123
456,9007,100,xyz456
999,9007,200,def666

employee.csv

id,first_name,last_name,email,city,ip_address
9007,John,Doe,jdoe@yourcompany.com,Miami,10.10.10.10
8765,Joe,Bloggs,jbloggs@somewhere.org,Salem,10.11.12.13

那就试试

# load both csv files
$payments  = Import-Csv -Path 'D:\Test\payments.csv'
$employees = Import-Csv -Path 'D:\Test\employee.csv'
# loop through the employees records
$result = foreach ($emp in $employees){
    # find a record in the payments.csv where the .employee_id is equal to the .id in the employee.csv
    $payments | Where-Object { $_.employee_id -eq $emp.id } | ForEach-Object {
        # create an object with properties from both csv files combined
        $obj = $emp | Select-Object @{Name = 'employee_id'; Expression = {$_.id}}, * -ExcludeProperty id
        # add the details from $payments to this
        $obj | Add-Member -MemberType NoteProperty -Name 'payment_id' -Value $_.id
        $obj | Add-Member -MemberType NoteProperty -Name 'payment' -Value $_.payment
        $obj | Add-Member -MemberType NoteProperty -Name 'payment_code' -Value $_.code
        # output the combined object to be collected in variable $result
        $obj
    }
}
# now you can show the results in the console
$result | Format-Table -AutoSize

# and save as new csv file
$result | Export-Csv -Path 'D:\Test\EmployeePayments.csv' -NoTypeInformation

屏幕输出:

employee_id first_name last_name email                 city  ip_address  payment_id payment payment_code
----------- ---------- --------- -----                 ----  ----------  ---------- ------- ------------
9007        John       Doe       jdoe@yourcompany.com  Miami 10.10.10.10 456        100     xyz456      
9007        John       Doe       jdoe@yourcompany.com  Miami 10.10.10.10 999        200     def666      
8765        Joe        Bloggs    jbloggs@somewhere.org Salem 10.11.12.13 123        1500    abc123