I want to create a PowerShell function that enumerates some data, and fire a script block on all occurrences.
By now I have (this is not the actual code, but it illustrates my issue):
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 don't get any result when I call:
Invoke-TenTimes { $_ }
The output is blank (nothing is displayed).
If I call
Invoke-TenTimes { $_ -eq $null }
I get ten true
. In fact, I see that $_
is null.
What is the proper way to populate the $_
?
What is driving me crazy, is that if I put this function and the call in the same ps1 file, it works (but I want to pass script block on demand):
function Invoke-TenTimes
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=0)]
[ScriptBlock]$Action
)
process
{
$digits = 0..10
$digits | % {
$Action.Invoke($_);
}
}
}
Invoke-TenTimes { $_ }