Skip to content

Instantly share code, notes, and snippets.

@santisq
Created December 30, 2024 17:54
Show Gist options
  • Save santisq/1c8809a80947cb82837142d23d8fa0a9 to your computer and use it in GitHub Desktop.
Save santisq/1c8809a80947cb82837142d23d8fa0a9 to your computer and use it in GitHub Desktop.
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