I want to create a PowerShell function that enumerates some data and runs a script block for all occurrences.
Now I have (this is not real code, but it illustrates my problem):
function Invoke-TenTimes { [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [ScriptBlock]$Action ) process { $digits = 0..10 $digits | % { $Action.Invoke($_); } } }
I put this function in my module. However, I do not get any result when I call:
Invoke-TenTimes { $_ }
The output is empty (nothing is displayed).
If i call
Invoke-TenTimes { $_ -eq $null }
I get ten true . In fact, I see that $_ is null.
What is the correct way to populate $_ ?
What drives me crazy is that if I put this function and the call in the same ps1 file, it works (but I want to pass the script block on request):
function Invoke-TenTimes { [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [ScriptBlock]$Action ) process { $digits = 0..10 $digits | % { $Action.Invoke($_); } } } Invoke-TenTimes { $_ }
Steve b
source share