Invoke-External.ps1 997 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function Invoke-External {
  2. <#
  3. .SYNOPSIS
  4. Invokes a non-PowerShell command.
  5. .DESCRIPTION
  6. Runs a non-PowerShell command, and captures its return code.
  7. Throws an exception if the command returns non-zero.
  8. .EXAMPLE
  9. Invoke-External 7z x $MyArchive
  10. #>
  11. if ( $args.Count -eq 0 ) {
  12. throw 'Invoke-External called without arguments.'
  13. }
  14. if ( ! ( Test-Path function:Log-Information ) ) {
  15. . $PSScriptRoot/Logger.ps1
  16. }
  17. $Command = $args[0]
  18. $CommandArgs = @()
  19. if ( $args.Count -gt 1) {
  20. $CommandArgs = $args[1..($args.Count - 1)]
  21. }
  22. $_EAP = $ErrorActionPreference
  23. $ErrorActionPreference = "Continue"
  24. Log-Debug "Invoke-External: ${Command} ${CommandArgs}"
  25. & $command $commandArgs
  26. $Result = $LASTEXITCODE
  27. $ErrorActionPreference = $_EAP
  28. if ( $Result -ne 0 ) {
  29. throw "${Command} ${CommandArgs} exited with non-zero code ${Result}."
  30. }
  31. }