title: Powershell: how get exception line number when using ScriptBlock::Invoke
body: <p>How can I get the line number where the exception is thrown in this example? The example below only gives me the line number where I invoke the script block (i.e. $sb.Invoke()). I want the line number where I throw "Error in FooBar".</p>
  
  <pre><code>function Foobar {
      throw "Error in FooBar"
  }
  
  function Bar {
      FooBar
  }
  
  function Foo {
      Bar
  }
  
  try {
          $sb = ${function:Foo}
          $sb.Invoke()
      }
  catch {
      $ErrorMessage = $_.Exception.Message
      $line = $_.InvocationInfo.ScriptLineNumber
      $script_name = $_.InvocationInfo.ScriptName
      write-host "&lt;--Error: Occurred on line $line in script $script_name." 
      Write-host "&lt;--Error: $ErrorMessage"
  }
  </code></pre>
  
  <p>UPDATE:</p>
  
  <p>If I change:</p>
  
  <pre><code>$sb = ${function:Foo}
  $sb.Invoke()
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>$func_info = get-command Foo
  Invoke-Command $func_info.ScriptBlock
  </code></pre>
  
  <p>it works.</p>