1

我正在尝试获取两个用户的 CSV 文件并将一个附加到另一个。我首先导入 CSV 并仅从每个列中选择我想要的列,然后将那些已编辑的 CSV 移动到临时 CSV 文件(Temp1.csvTemp2 .​​csv )。然后我获取两个临时 CSV 文件并使用删除第一行(标题)Select-Object -Skip 1并将它们设置为两个新的临时 CSV(Temp3.csvTemp4.csv)。接下来,我将Temp3.csv作为变量导入,然后立即将该 CSV 作为我的最终文件导出。最后,我将Temp4.csv作为变量导入,然后将其导出为与我保存 Temp3 相同的文件名,但使用-Appendand-Force标志。但是, Temp4 没有像我预期的那样附加到 Temp3 。我觉得我遗漏了一些明显的东西,而这几乎绝对不是编写这个脚本的最佳方法。不过,直到最后一部分,一切都正常。任何帮助,将不胜感激。

我在这里遵循了 Export-CSV PowerShell 文档的示例 6:https ://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6

我也尝试过和的组合Set-ContentGet-Content的组合。以下是我的最新版本和相关输出:Import-CSVExport-CSV


# Get today's date to add to the end of the user upload file
$importDate = Get-Date -Format "yyyy.MM.dd"

# Import the record, but only select the employee's email address, preferred first name, and last name
# and place those three columns' worth of data in the temp csv file 
Import-Csv -Path "EmployeeList.csv" |
Select-Object "Email","FName","LName" |
Where-Object {$_."Email" -ne ""} |
Export-Csv -Path "Temp1.csv" -NoTypeInformation

# Import the record, but only select the employee's email address, preferred first name, and last name
# and place those three columns' worth of data in the temp csv file
Import-Csv -Path "ContractorList.csv" |
Select-Object "Email","FName","LName" |
Where-Object {$_.Email -like "*@company.com"} |
Export-Csv -Path "Temp2.csv" -NoTypeInformation

# Remove column headings
$tempCSVone = (Get-Content -Path "Temp1.csv")
$tempCSVone | Select-Object -Skip 1 | Set-Content -Path "Temp3.csv"

# Remove column heading
$tempCSVtwo = (Get-Content -Path "Temp2.csv")
$tempCSVtwo | Select-Object -Skip 1 | Set-Content -Path "Temp4.csv"

# Export to final csv
$tempCSVthree = (Import-Csv -Path "Temp3.csv")
$tempCSVthree | Export-Csv -Path "TempFinal_$importDate.csv"-NoTypeInformation

# Append to TempFInal
$tempCSVfour = (Import-Csv -Path "Temp4.csv")
$tempCSVfour | Export-CSv -Path "TempFinal_$importDate.csv" -Append -Force -NoTypeInformation


Results in CSV files: 

Temp1.csv
Email   FName   LName
a@company.com   A   AAA
b@company.com   B   BBB
c@company.com   C   CCC

Temp2.csv
Email   FName   LName
d@company.com   D   DDD
e@company.com   E   EEE
f@company.com   F   FFF

Temp3.csv
a@company.com   A   AAA
b@company.com   B   BBB
c@company.com   C   CCC

Temp4.csv
d@company.com   D   DDD
e@company.com   E   EEE
f@company.com   F   FFF

Expected result in TempFinal.csv:
a@company.com   A   AAA
b@company.com   B   BBB
c@company.com   C   CCC
d@company.com   D   DDD
e@company.com   E   EEE
f@company.com   F   FFF

Actual result in TempFinal.csv:
a@company.com   A   AAA
b@company.com   B   BBB
c@company.com   C   CCC

4

1 回答 1

2

拥有文件后,您使事情变得过于复杂

  • 进口,相同的属性和顺序
  • 只需 - 将第二个附加到同一文件:
## Q:\Test\2019\09\12\SO_57910524.ps1
Import-Csv -Path "EmployeeList.csv" |
  Select-Object Email,FName,LName |
    Where-Object Email -ne "" | 
      Export-Csv -Path "Temp1.csv" -NoTypeInformation

Import-Csv -Path "ContractorList.csv" |
  Select-Object Email,FName,LName |
    Where-Object Email -like "*@company.com" |
      Export-Csv -Path "Temp1.csv" -NoTypeInformation -Append

> Get-Content .\Temp1.csv
"Email","FName","LName"
"a@company.com","A","AAA"
"b@company.com","B","BBB"
"c@company.com","C","CCC"
"d@company.com","D","DDD"
"e@company.com","E","EEE"
"f@company.com","F","FFF"
于 2019-09-12T16:27:50.177 回答