title: Save stop-Process error in a variable and write it in eventlog
body: <p>This seemed like easy one, but it wasn't.
  I have the following function that stops processes and write in eventlog:</p>
  <pre><code>$PiEventlog = &quot;Company Name&quot;
  $PiEventlogSource = &quot;Source Name&quot;
  #   For Testing purpose, i use this PIDs.
  $gpid = 45852,5264
          
  function Terminate-Processs {
  #   if $pid is empty, give this msg.
      if(!$gpid){
          Write-Host &quot;Nth here&quot;
      }
      else{
          for ($i = 0; $i -lt ($gpid).Count; $i++) {     
              $PIDForEventLog = $gpid        
              try{
                  Stop-Process -Id $gpid[$i] -force -ErrorAction SilentlyContinue -ErrorVariable $ResultStopProcess
                  Write-EventLog -logname $PiEventlog -source $PiEventlogSource -eventID 1021 -entrytype Information -message &quot;$env:USERNAME killed the following PID $PIDForEventLog from the instance  $Global:SelectedInstance&quot;
              }
              catch{
                  Write-EventLog -logname $PiEventlog -source $PiEventlogSource -eventID 1021 -entrytype Error -message &quot;$env:USERNAME could not kill the following PID $PIDForEventLog from the instance  $Global:SelectedInstance `n$ResultStopProcess `nPlease check manually whether the process is still available.&quot;
              }     
          }
      }
  }
  </code></pre>
  <p>in the Catch loop, i am trying to give the errorvariable $resultstopprcess to the eventlog entry. That means, if an error occur while stoping an process, the error should be saved into that variable and it should be given to eventlog. the variable should contain sth like this, if error occurs:</p>
  <blockquote>
  <p>Stop-Process : Es kann kein Prozess mit der Prozess-ID 34234 gefunden
  werden. In Zeile:1 Zeichen:1</p>
  <ul>
  <li>Stop-Process -id 34234</li>
  <li>
  <pre><code>  + CategoryInfo          : ObjectNotFound: (34234:Int32) [Stop-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.StopProcessCommand
  </code></pre>
  </li>
  </ul>
  </blockquote>
  <p>But the variable is somehow always empty.
  How can i do this? How can i save the error to a variable. so that i can use it later?</p>