Created
December 30, 2024 17:54
-
-
Save santisq/1c8809a80947cb82837142d23d8fa0a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
New-Module TestModule { | |
function Test-InvokeUsingCmdlet { | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline)] | |
[psobject] $InputObject, | |
[Parameter(Mandatory, Position = 0)] | |
[scriptblock] $Scriptblock | |
) | |
begin { | |
$method = [System.Management.Automation.ScriptBlock].GetMethod( | |
'InvokeUsingCmdlet', | |
[System.Reflection.BindingFlags] 'NonPublic, Instance') | |
$autoNull = [System.Management.Automation.Internal.AutomationNull]::Value | |
} | |
process { | |
$params = | |
$PSCmdlet, # contextCmdlet: this | |
$false, # useLocalScope: false | |
1, # errorHandlingBehavior: ErrorHandlingBehavior.WriteToCurrentErrorPipe = 1 | |
$InputObject, # dollarUnder: InputObject | |
$InputObject, # input: new object[] { InputObject } | |
$autoNull, # scriptThis: AutomationNull.Value | |
$null # args: Array.Empty<object>() | |
$method.Invoke($Scriptblock, $params) | |
} | |
} | |
} | Import-Module | |
0..10 | Test-InvokeUsingCmdlet { $_ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment