简体   繁体   English

使用 Powershell 将 2 个 CSV 文件中的数据合并为 1 个新的 CSV

[英]Combine data from 2 CSV files into 1 new CSV using Powershell

***EDITED FOR CLARITY: *** 编辑清晰:

My ADP CSV contains these headers exactly as listed: Action Date, Job Information Effective Date,Location Name, Legal Name, Employee Status Code, First Name, Last Name, Employee ID, Job Title, Location State, Location City, Employee Class, Hire Date, Job Termination Date, FLSA Over-Time Indicator, FLSA Status.我的 ADP CSV 包含与所列完全相同的这些标题:行动日期、工作信息生效日期、地点名称、法定姓名、员工状态代码、名字、姓氏、员工 ID、职位名称、地点状态、地点城市、员工类别、雇用日期、工作终止日期、FLSA 加班指示、FLSA 状态。

My Office 365 CSV contains these headers exactly as listed: DISPLAY NAME, EMAIL ADDRESS我的 Office 365 CSV 包含与所列完全相同的这些标题:显示名称、电子邮件地址

I need a CSV that lists: Display Name, Email Address, City, State, Location Name, Job Title.我需要一个 CSV 列出:显示名称、电子邮件地址、城市、州、位置名称、职位。

I need a Powershell Script that combines the two files together to leave me with only the Office 365 Users and the data that their profile is missing such as City, State, Location Name, Job Title.我需要一个将这两个文件组合在一起的 Powershell 脚本,只留下 Office 365 用户和他们的个人资料缺失的数据,例如城市、州、位置名称、职位。

I understand creating the Hash Table for the Office 365 List, but every time I create one it returns "The array index evaluated to Null" which in my understanding means I am not using the correct header names in the hash table.我了解为 Office 365 列表创建哈希表,但每次创建时它都会返回“数组索引评估为 Null”,这在我的理解中意味着我没有在哈希表中使用正确的标题名称。

****Edited AGAIN! ****再次编辑! I am attaching my code.我附上了我的代码。 It does everything I need it to except that the Email column it creates is blank.除了它创建的电子邮件列是空白的之外,它可以完成我需要的所有操作。 I no longer get hast table errors.我不再收到 hast 表错误。

    $ItemsToKeep = 'First Name','Last Name','Employee Status Code','Location Name','Job Title','Location State'
    $ItemsToKeep += @{Name = "Display Name"; Expression = {$_.'First Name' + ' ' + $_.'Last Name'}}
    $ItemsToKeep += @{Name = "Email"; Expression = { $EmailLookupTable[$_.'Display Name'] }}

    $EmailLookupTable = @{}
    Import-Csv C:\O365LIST.csv |ForEach-Object {
    $EmailLookupTable[$_.'Display Name'] = $_.'Email Address'
    }



    Import-Csv C:\ADPLIST.csv |Where-Object {$_.'Employee Status Code' -eq 'Active'} | Select $ItemsToKeep | export-csv C:\Testing.csv -notypeinformation

First, load the second CSV into a hashtable, this way you can easily map the display name from the first CSV to an email address:首先,将第二个 CSV 加载到哈希表中,这样您就可以轻松地将显示名称从第一个 CSV 映射到电子邮件地址:

#Import the second CSV and use a hashtable to store the email address based on the display name
$EmailLookupTable = @{}
Import-Csv C:\employee_emails.csv |ForEach-Object {
    $EmailLookupTable[$_.'Display Name'] = $_.'Email Address'
}

With your existing script, instead of exporting to a new file in between each operation, chain them all together in a single pipeline - most of the code can be automated away with a tiny bit of metaprogramming!使用您现有的脚本,而不是在每个操作之间导出到一个新文件,而是将它们全部链接在一个管道中 - 大多数代码可以通过一点元编程实现自动化!

# Automate away the task of replacing spaces with underscores
$ColumnsToKeep = "Location Name","Employee Status Code","Job Title","Location State","Location City","First Name","Last Name"
$PropertiesToSelect = $ColumnsToKeep |ForEach-Object {
    @{Name = $_ -replace ' ','_'; Expression = [scriptblock]::Create('$_."{0}"' -f $_)}
}

# Add the display name property
$PropertiesToAdd = @(@{Name = "Display_Name"; Expression = {$_.First_Name + ' ' + $_.Last_Name}})

# Add the Email address from the lookup table
$PropertiesToAdd += @{Name = "Email"; Expression = { $EmailLookupTable[$_.First_Name + ' ' + $_.Last_Name] }}

# 1. Import CSV
# 2. Filter on status
# 3. Select the properties to keep/rename
# 4. Select the properties from the previous step + the new ones
# 5. Export CSV
Import-Csv C:\ADPLIST.csv |Where-Object {$_.'Employee Status Code' -eq 'Active'} |Select-Object $PropertiesToSelect |Select-Object *,$PropertiesToAdd |Export-Csv C:\final.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Powershell合并所有csv文件 - Unable to combine all csv files using powershell 使用来自 csv 文件的数据在 PowerShell 中组合数组和哈希表 - Combine Arrays and Hash Tables in PowerShell using data from csv file 使用 powershell 排序和分组从 csv 文件中提取数据 - Extracting data from csv files using powershell sort and group 使用 Windows Z3D265B4E03EEEF0DDFZBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA - Parsing date from .csv filename into a new column for multiple .csv files using Windows PowerShell PowerShell - 尝试根据列值将 2 个 CSV 文件中的数据合并为一个 - PowerShell - Trying to combine data from 2 CSV files into one based on column value 使用Powershell处理多个csv文件并将数据存储在单独的文本文件中(删除空格和换行) - Process multiple csv files and store data in separate text files (removing spaces and new lines) using Powershell 合并2个CSV文件中的列 - Combine columns from 2 CSV files 使用PowerShell比较2个csv文件 - Comparing 2 csv files using powershell PowerShell 使用 CSV 删除文件 - PowerShell using CSV to remove files 使用 powershell 比较 2 个 csv 文件 - comparision of 2 csv files using powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM