How to tell if function/cmdlet is last in Pipeline


i writing logging wrapper outputs file , output object.  since output matters if object last in pipeline want run test @ command invocation see if command last in pipeline.  if is, let code run is.  if it's not, want kill write-output line.  any suggestions?  here snippet:

  function utility.writeto-outputandhost {    	param(  		[parameter(mandatory=$true,valuefrompipeline=$true)]  		[string]  		$inputobject  	)    	write-host $inputobject;  	write-output $inputobject  }  


hi will,

$myinvocation has pipelineposition and pipelinelength properties; compare them in function's begin block determine whether or not function's position in pipeline last.

function x {

 param([parameter(valuefrompipeline = $true)]$x)

 begin {

  if ($myinvocation.pipelineposition -ne $myinvocation.pipelinelength) {

   break

  }

 }

 process {

  $x

 }

}

 

# in both case function x last

1..3 | x

x 4

 

# breaks execution

5 | x | write-output


  robert robelo  


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

DCOM received error "2147746132" from...

DFSR RPC replication errors 5014 1726 with large files over VPN

ADFS 3.0 Event ID 4625 | An Error occurred During Logon | Status: 0xC000035B