0

I am trying to read a csv file and store in a hasmap. Below is the code I am using.

$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
}

Below is the output for $ht. I am getting 1 $null value for one of the field 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

Sample 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","","","","","","","",""
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
live2learn
  • 861
  • 2
  • 9
  • 16

0 Answers0