尝试将包含某些数据的CSV文件(以逗号分隔)中的重复项排序并删除到新的CSV文件中。如果我对-Property参数的列进行硬编码,我会完美地工作。如果我尝试动态添加列,那么我可以将它用于不同的项目。它不起作用。
下面是有效的代码,之后的代码是:
作品
$Source = 'e:\Temp\DataFromSkywardEdited.csv';
$Destination = 'e:\Temp\DataFromSkywardRD.csv';
$SortByColNames = 'LastName,FirstName';
Get-Content $Source |
ConvertFrom-Csv |
Sort -Property LastName,FirstName -Unique |
ForEach {"{0},{1},{2},{3},{4},{5},{6},{7},{8}" -f $_.EmployeeID.Trim(), $_.FirstName.Trim(), $_.LastName.Trim(), $_.Location.Trim(), $_.Department.Trim(), $_.TelephoneNo.Trim(), $_.Email.Trim(), $_.EmpTypeCode.Trim(), $_.EmployeeTypeDescription.Trim()} |
Add-Content $Destination
不起作用
$Source = 'e:\Temp\DataFromSkywardEdited.csv';
$Destination = 'e:\Temp\DataFromSkywardRD.csv';
$SortByColNames = 'LastName,FirstName';
Get-Content $Source |
ConvertFrom-Csv |
Sort -Property $SortByColNames -Unique |
ForEach {"{0},{1},{2},{3},{4},{5},{6},{7},{8}" -f $_.EmployeeID.Trim(), $_.FirstName.Trim(), $_.LastName.Trim(), $_.Location.Trim(), $_.Department.Trim(), $_.TelephoneNo.Trim(), $_.Email.Trim(), $_.EmpTypeCode.Trim(), $_.EmployeeTypeDescription.Trim()} |
Add-Content $Destination