Created
January 17, 2024 08:59
-
-
Save amis92/91594d9e57c9a02b64facd953420d8bc to your computer and use it in GitHub Desktop.
This file contains 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
function Invoke-Native { | |
[CmdletBinding(SupportsShouldProcess)] | |
param( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | |
[scriptblock[]] | |
$Call | |
) | |
process { | |
foreach ($item in $Call) { | |
$invocation = $ExecutionContext.InvokeCommand.ExpandString($item).Trim() | |
if ($PSCmdlet.ShouldProcess($invocation)) { | |
$result = & $item | |
if ($LASTEXITCODE -ne 0) { | |
$invocation, $result | Join-String -Separator "`n" | Write-Error | |
throw "Failed to call native command - exit code $LASTEXITCODE." | |
} | |
$result | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment