I want to write a cmdlet that invokes a call back method. When invoking the callback method, I want to be able to use the automatic variable $_. This is so that I don't have to
For example, let's say my function is an implementation similar to ForEach-Object:
(Note - I know I could use ForEach-Object
to this exact same thing. I'm just presenting this as a simplified example for something else that i need to do)
function Invoke-MyCustomPsCmdlet {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[int[]] $input,
[scriptblock] $callback
)
# Do somethign meaningful here.
Write-Output "Cmdlet: $input"
foreach($i in $input){
# How to invoke the $callback so that $i will be passed on to the $_ of the callback scriptblock?
#$i | $callback
}
}
This is how i want to invoke it (with $_)
1,2,3,4,5 | Invoke-MyCustomPsCmdlet -callback { Write-Output "Callback: $_" }
And my expectation is to have this output
Cmdlet: 1 2 3 4 5
Callback: 1
Callback: 2
Callback: 3
Callback: 4
Callback: 5