title: Powershell script to read a file line by line, pass the lines which start with a particular string to a function
body: <p>I'm trying to get code coverage using Sonarqube. The coverage report is generated by karma. For some reason, the coverage file generated by Karma changes
  the case of 22 files inside the report. As a result, I'm unable to get coverage for those 22 files. I use a PowerShell script in my Jenkins to generate a canonical path. Below is the script. My script should perform the below steps:</p>
  
  <ol>
  <li>Access the coverage report (unit-tests-lcov.info)</li>
  <li>Read the report line by line</li>
  <li>Use every file inside unit-tests-lcov.info starting with 'SF' and pass it to the canonical function</li>
  <li>Save the file</li>
  </ol>
  
  <p>I'm unable to write a script for the 3rd step. Can anyone make necessary changes to my script below?</p>
  
  <pre><code>$getPathNameSignature = @'
  [DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
  public static extern uint GetLongPathName(
      string shortPath, 
      StringBuilder sb, 
      int bufferSize);
  
  [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
  public static extern uint GetShortPathName(
     string longPath,
     StringBuilder shortPath,
     uint bufferSize);
  '@
  $getPathNameType = Add-Type -MemberDefinition $getPathNameSignature -Name GetPathNameType -UsingNamespace System.Text -PassThru
  
  
  function Get-PathCanonicalCase
  {
      [CmdletBinding()]
      param(
          [Parameter(Mandatory=$true)]
          [string]
          # Gets the real case of a path
          $Path
      )
  
      if( -not (Test-Path $Path) )
      {
          Write-Error "Path '$Path' doesn't exist."
          return
      }
  
      $shortBuffer = New-Object Text.StringBuilder ($Path.Length * 2)
      [void] $getPathNameType::GetShortPathName( $Path, $shortBuffer, $shortBuffer.Capacity )
  
      $longBuffer = New-Object Text.StringBuilder ($Path.Length * 2)
      [void] $getPathNameType::GetLongPathName( $shortBuffer.ToString(), $longBuffer, $longBuffer.Capacity )
  
      return $longBuffer.ToString()
  }
  
  $file3 = "$env:WORKSPACE\UIArtifacts\unit-tests-lcov.info"           
  $text = (Get-Content -Path $file3 -ReadCount 0) -join "`n"  
  $ran = $text -Includes 'SF'                                  
  Get-PathCanonicalCase($text) | Set-Content -Path $file3    
  </code></pre>
  
  <p>A part of the input file looks like:
  I need to pass the file paths to the Get-Canonical function. PS. Part of the file paths is the environment variable.</p>
  
  <pre><code>TN:
  c:\sysapps\hudson\.jenkins\jobs\CropObsUi-Metrics\workspace\encirca\encConf.js
  FNF:0
  FNH:0
  DA:10,1
  DA:14,1
  DA:30,1
  DA:31,1
  DA:32,1
  DA:33,1
  DA:34,1
  DA:35,1
  DA:36,1
  DA:37,1
  DA:39,1
  LF:11
  LH:11
  BRF:0
  BRH:0
  end_of_record
  TN:
  c:\sysapps\hudson\.jenkins\jobs\CropObsUi-Metrics\workspace\encirca\common\util\data.js
  FN:25,(anonymous_1)
  FN:57,(anonymous_2)
  FN:87,(anonymous_3)
  FN:149,(anonymous_4)
  FNF:4
  FNH:0
  FNDA:0,(anonymous_1)
  FNDA:0,(anonymous_2)
  FNDA:0,(anonymous_3)
  FNDA:0,(anonymous_4)
  </code></pre>