Skip to content

Instantly share code, notes, and snippets.

@amis92
Created January 17, 2024 08:59
Show Gist options
  • Save amis92/91594d9e57c9a02b64facd953420d8bc to your computer and use it in GitHub Desktop.
Save amis92/91594d9e57c9a02b64facd953420d8bc to your computer and use it in GitHub Desktop.
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