invoke command - Powershell seems to treat sub pipeline code as part of execution body of first pipeline script block -


i creating retry command in powershell invoke script block retry logic:

function invoke-commandwithretry {     [cmdletbinding()]     param     (         [parameter(mandatory = $true, valuefrompipeline = $true)]         [scriptblock]$command,          [parameter(mandatory = $false)]         [validaterange(0, [uint32]::maxvalue)]         [uint32]$retry = 3,          [parameter(mandatory = $false, position = 3)]         [validaterange(0, [uint32]::maxvalue)]         [uint32]$delayinms = 1000     )      process {          $retrycount = 0         while( $true )         {             try              {                 return (invoke-command -scriptblock $command)             }             catch             {                 if( $retrycount -ge $retry )                 {                     throw                 }                 else                 {                     write-warning $_.exception                     start-sleep -milliseconds $delayinms                     $retrycount++                     write-warning "retry command $retrycount time: $command"                 }             }         }     } } 

the command should straightforward, try run script block , if exception found, re-run it.

but found issue function when sub pipeline throw exception, here test code:

{     write-host "start";     1,2,3  } | invoke-commandwithretry | foreach-object {         $_         throw "error" } write-host "end" 

i expect that, because of exception, 2,3 object skipped. result expected, retry working not first pipeline script block, seems involve following pipeline body of first pipeline, weird me, here result:

start 1 warning: system.management.automation.runtimeexception: error warning: retry command 1 time:      write-host "start";     1,2,3   start 1 warning: system.management.automation.runtimeexception: error warning: retry command 2 time:      write-host "start";     1,2,3   start 1 warning: system.management.automation.runtimeexception: error warning: retry command 3 time:      write-host "start";     1,2,3   start 1 error @ line:7 char:9 +         throw "error" +         ~~~~~~~~~~~~~     + categoryinfo          : operationstopped: (error:string) [], runtimeexception     + fullyqualifiederrorid : error 

anyone can explain why happened? , how avoid first script block retry when sub pipeline throw exception?


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -