Retry-Command.ps1 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright (C) 1994-2018 Altair Engineering, Inc.
  2. # For more information, contact Altair at www.altair.com.
  3. #
  4. # This file is part of the PBS Professional ("PBS Pro") software.
  5. #
  6. # Open Source License Information:
  7. #
  8. # PBS Pro is free software. You can redistribute it and/or modify it under the
  9. # terms of the GNU Affero General Public License as published by the Free
  10. # Software Foundation, either version 3 of the License, or (at your option) any
  11. # later version.
  12. #
  13. # PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
  14. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. # FOR A PARTICULAR PURPOSE.
  16. # See the GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # Commercial License Information:
  22. #
  23. # For a copy of the commercial license terms and conditions,
  24. # go to: (http://www.pbspro.com/UserArea/agreement.html)
  25. # or contact the Altair Legal Department.
  26. #
  27. # Altair’s dual-license business model allows companies, individuals, and
  28. # organizations to create proprietary derivative works of PBS Pro and
  29. # distribute them - whether embedded or bundled with other software -
  30. # under a commercial license agreement.
  31. #
  32. # Use of Altair’s trademarks, including but not limited to "PBS™",
  33. # "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
  34. # trademark licensing policies.
  35. Function Retry-Command {
  36. [CmdletBinding()]
  37. Param
  38. (
  39. [Parameter(Mandatory = $true, Position = 1, ValueFromPipeline = $true)]
  40. [ScriptBlock]$Command,
  41. [Parameter(Mandatory = $false, Position = 2)]
  42. [ValidateRange(0, [UInt32]::MaxValue)]
  43. [UInt32]$Retry = 120,
  44. [Parameter(Mandatory = $false, Position = 3)]
  45. [ValidateRange(0, [UInt32]::MaxValue)]
  46. [UInt32]$Timeout = 1000
  47. )
  48. Begin {
  49. $StrCommand = $Command.ToString().Trim()
  50. }
  51. Process {
  52. $lasterr = $false
  53. for ($i = 1; $i -le $Retry; $i++) {
  54. Write-Host -ForegroundColor DarkCyan -NoNewLine ("{0} ... attempt = {1}" -f $StrCommand, $i)
  55. try {
  56. $cmdOutput = & $Command
  57. if ($LastExitCode -ne 0) {
  58. if ($cmdOutput) {
  59. throw $cmdOutput
  60. } else {
  61. throw "{0} exited with {1}" -f $StrCommand,$LastExitCode
  62. }
  63. }
  64. Write-Host -ForegroundColor Green ' ... Ok'
  65. $cmdOutput
  66. $lasterr = $false
  67. Break
  68. } catch {
  69. Write-Host -ForegroundColor Red ' ... Failed'
  70. $lasterr = $_.Exception
  71. Start-Sleep -Milliseconds $Timeout
  72. }
  73. }
  74. if ($lasterr) {
  75. throw $lasterr
  76. }
  77. }
  78. }