start-process-and-test-exitcode.psm1 848 B

1234567891011121314151617181920212223242526
  1. Function Start_Process_and_test_exitcode {
  2. # This function is a wrapper for Start-Process that checks the exitcode
  3. # It receives 3 parameters:
  4. # $fun - the process that shall be started
  5. # $args - the the arguments of $fun
  6. # $descr - the short description shown in the case of an error
  7. Param(
  8. [Parameter(Mandatory=$true)] [String] $fun,
  9. [Parameter(Mandatory=$true)] [String] $args,
  10. [Parameter(Mandatory=$true)] [String] $descr
  11. )
  12. Begin { Write-Host "Executing Command: $fun $args" }
  13. Process {
  14. $p = Start-Process "$fun" -ArgumentList "$args" -Wait -PassThru
  15. If ($p.ExitCode -ne 0) {
  16. Write-Error "$descr returned exitcode $p.ExitCode."
  17. exit $p.ExitCode
  18. }
  19. }
  20. End { Write-Host "Finished Executing Command: $fun $args" }
  21. }