读取CSV时获取空白

时间:2016-04-28 12:34:03

标签: csv powershell powershell-v5.0

我正在尝试读取csv文件并存储在hasmap中。以下是我正在使用的代码。

$data | ForEach-Object {
  $ht = @{}
  $_.psobject.Properties |
    #Get only the grouped properties (that have a number at the end)
    Where-Object { $_.Name -match '\d+$' } |
    #Group properties by param/group number
    Group-Object {$_.Name -replace '\w+(\d+)$', '$1' } | ForEach-Object {
      $param = $_.Group | Where-Object { $_.Name -match 'param' }
      $value = $_.Group | Where-Object { $_.Name -match 'value' }

      #If property has value
      if($value.value -ne ""){
        #Add to hashtable
        $ht.add($param.Value,$value.Value)
      }
    }

  $ht
}

以下是$ht的输出。我为其中一个字段$null获得了1 OrgId个值。

Name                           Value
----                           -----
{orgId, }                      {1000002, $null}
type                           CSVFile
codepage                       MS1252
agentId                        00000208000000000002
name                           infa_param_file_Pravakar
dateFormat                     MM/dd/yyyy HH:mm:ss
database                       C:\\Program Files\\Informatica Cloud Secure A

示例CSV:

"param1","value1","param2","value2","param3","value3","param4","value4","param5","value5","param6","value6","param7","value7","param8","value8","param9","value9","param10","value10","param11","value11"
"orgId","000002","name","infa_param_file_Pravakar","agentId","00000208000000000002","dateFormat","MM/dd/yyyy HH:mm:ss","database","C:\\Program Files\\Informatica Cloud Secure Agent\\main\\rdtmDir\\userparameters","codepage","MS1252","type","CSVFile","","","","","","","",""

0 个答案:

没有答案