使用PowerShell第3部分中的排序对象进行排序

时间:2016-08-11 21:10:51

标签: powershell

从下面的第2部分示例稍微改变了我的代码,但是参考第2部分的例子,这非常感谢stackoverflow中的成员。

[string] $Source = 'e:\Temp\DataFromSkywardEdited.csv';
[string] $Destination = 'e:\Temp\DataFromSkywardRD.csv';
[object] $SortByColNames = 'LastName','FirstName';
[string] $ShowColsByNumber = "{0},{1},{2},{3},{4},{5},{6},{7},{8}";
[object] $ColumnValueFormat = '$_.EmployeeID.Trim()', '$_.FirstName.Trim()', '$_.LastName.Trim()', '$_.Location.Trim()', '$_.Department.Trim()', '$_.TelephoneNo.Trim()', '$_.Email.Trim()', '$_.EmpTypeCode.Trim()', '$_.EmployeeTypeDescription.Trim()';

Get-Content $Source |
    ConvertFrom-Csv -Delimiter $Delimiter | 
    Sort-Object -Property $SortByColNames -Unique |
    ForEach-Object {
        # Each of the values in $ColumnValueFormat must be executed to get the property from the loop variable ($_). 
        $values = foreach ($value in $ColumnValueFormat) {
            Invoke-Expression $value
        }
        # Then the values can be passed in as an argument for the format operator.
        $ShowColsByNumber -f $values
    } | 
    Add-Content $Destination

如何将 $ ColumnValueFormat 用数字而不是列名表示?

所以而不是:

[object] $ColumnValueFormat = '$_.EmployeeID.Trim()', '$_.FirstName.Trim()', '$_.LastName.Trim()', '$_.Location.Trim()', '$_.Department.Trim()', '$_.TelephoneNo.Trim()', '$_.Email.Trim()', '$_.EmpTypeCode.Trim()', '$_.EmployeeTypeDescription.Trim()';

类似于:

[object] $ColumnValueFormat = '$_.[0].Trim()', '$_.[1].Trim()', '$_.[2].Trim()', '$_.[3].Trim()', '$_.[4].Trim()', '$_.[5].Trim()', '$_.[6].Trim()', '$_.[7].Trim()', '$_.[8].Trim()';

2 个答案:

答案 0 :(得分:2)

这看起来好像你正在努力做到这一点,但要直接回答你的问题,你只需删除括号内的数字之前的句号。所以......

[object] $ColumnValueFormat = '$_[0].Trim()', '$_[1].Trim()', '$_[2].Trim()', '$_[3].Trim()', '$_[4].Trim()', '$_[5].Trim()', '$_[6].Trim()', '$_.[7]Trim()', '$_[8].Trim()';

答案 1 :(得分:2)

  

如何使$ColumnValueFormat由数字而不是列名表示?

你不会。只是......没有。

要修剪CSV的字段,您只需执行this之类的操作:

$csv = Import-Csv $Source

foreach ($record in $csv) {
  foreach ($property in $record.PSObject.Properties) {
    $property.Value = $property.Value.Trim()
  }
}

$csv | Sort-Object $SortByColNames -Unique | Export-Csv $Destination

如果您必须以不同的方式处理不同的字段,请使用switch语句来区分值:

$property.Value = switch ($property.Name) {
  'foo' { ... }
  'bar' { ... }
  ...
}

通过calculated properties修改字段:

Import-Csv $Source |
  Select-Object @{n='EmployeeID';e={$_.EmployeeID.Trim()},
    @{n='FirstName';e={...},
    ... |
  Sort-Object $SortByColNames -Unique |
  Export-Csv $Destination

或通过New-Object重新创建记录:

Import-Csv $Source |
  ForEach-Object {
    New-Object -Type PSObject -Property @{
      'EmployeeID' = $_.EmployeeID.Trim()
      'FirstName'  = ...
      ...
    }
  } |
  Sort-Object $SortByColNames -Unique |
  Export-Csv $Destination

在PowerShell v3及更新版本中,您甚至可以使用type accelerator [ordered]按特定顺序创建属性,如下所示:

$props = [ordered]@{
  'EmployeeID' = $_.EmployeeID.Trim()
  'FirstName'  = ...
  ...
}
New-Object -Type PSObject -Property $props

或者像这样:

[PSObject][ordered]@{
  'EmployeeID' = $_.EmployeeID.Trim()
  'FirstName'  = ...
  ...
}