title: How to catch exceptions in PowerShell and send them to a method to deal with the properties?
body: <p>I have a catch clock that works like this:</p>
  
  <pre><code>catch  [System.InvalidOperationException]
  {
       Process-Exception($_.Exception,1111) 
  }
  </code></pre>
  
  <p>And then I want to process the exception like this:</p>
  
  <pre><code># Write exceptions to eventlog and reset socket
  function Process-Exception { param ($exception, $eventID)
  
      $dateTime = Get-Date
      #Write-EventLog -ComputerName $env:COMPUTERNAME -Source PowerShell -LogName Application -EventId $eventID `
  
      #for now: write-host [string] instead of -Message [string]
      write-host "Date and time:`t" + $dateTime + "`r`n`" 
      write-host "Exception Source:`t" + $exception.Source   
      write-host "Error Code:`t"+ $exception.NativeErrorCode  
      write-host "Exception Message:" + $exception.Message  
      write-host "Stack Trace:`t" + $exception.StackTrace + "`r`n`r`n`
  
      $socket = $null
  }
  </code></pre>
  
  <p>Where <code>`t</code> is a tab and the <code>$exception</code> properties needs to be accessed. Unfortunately when I do this the exception is not send to the method, only the error code (this case 1111) is send. THe message is:</p>
  
  <pre><code> write-host   Stack Trace:   + Cannot find type [[System.InvalidOperationException]]: 
  make sure the assembly containing this type is loaded. 9999.StackTrace + 
  </code></pre>
  
  <p>How can I send the full exception to the method? And if you have improvements to the code let me know!</p>