Created
September 26, 2021 04:09
-
-
Save nmbell/ddfdd12a9911661c60c56c9f2e3ecac1 to your computer and use it in GitHub Desktop.
Demo of a proxy function that sets the execution status instead of returning a Boolean
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 Set-ExecutionStatus | |
{ | |
[CmdletBinding()] | |
[Alias('?$')] | |
Param | |
( | |
[Parameter(ValueFromPipeline = $true)] | |
[Bool]$Boolean | |
) | |
# $? = $Boolean # pseudo-code | |
If (-not $Boolean) | |
{ | |
$ex = [System.Exception]::new() | |
$er = [System.Management.Automation.ErrorRecord]::new($ex,'error',0,$null) | |
$PSCmdlet.WriteError($er) | |
} | |
} | |
function Test-Path | |
{ | |
[CmdletBinding(DefaultParameterSetName='Path', HelpUri='https://go.microsoft.com/fwlink/?LinkID=2097057')] | |
param( | |
[Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] | |
[AllowEmptyCollection()] | |
[AllowNull()] | |
[AllowEmptyString()] | |
[string[]] | |
${Path}, | |
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)] | |
[Alias('PSPath','LP')] | |
[AllowNull()] | |
[AllowEmptyCollection()] | |
[AllowEmptyString()] | |
[string[]] | |
${LiteralPath}, | |
[string] | |
${Filter}, | |
[string[]] | |
${Include}, | |
[string[]] | |
${Exclude}, | |
[Alias('Type')] | |
[Microsoft.PowerShell.Commands.TestPathType] | |
${PathType}, | |
[switch] | |
${IsValid}, | |
[Parameter(ValueFromPipelineByPropertyName=$true)] | |
[pscredential] | |
[System.Management.Automation.CredentialAttribute()] | |
${Credential}) | |
dynamicparam | |
{ | |
try { | |
$targetCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Test-Path', [System.Management.Automation.CommandTypes]::Cmdlet, $PSBoundParameters) | |
$dynamicParams = @($targetCmd.Parameters.GetEnumerator() | Microsoft.PowerShell.Core\Where-Object { $_.Value.IsDynamic }) | |
if ($dynamicParams.Length -gt 0) | |
{ | |
$paramDictionary = [Management.Automation.RuntimeDefinedParameterDictionary]::new() | |
foreach ($param in $dynamicParams) | |
{ | |
$param = $param.Value | |
if(-not $MyInvocation.MyCommand.Parameters.ContainsKey($param.Name)) | |
{ | |
$dynParam = [Management.Automation.RuntimeDefinedParameter]::new($param.Name, $param.ParameterType, $param.Attributes) | |
$paramDictionary.Add($param.Name, $dynParam) | |
} | |
} | |
return $paramDictionary | |
} | |
} catch { | |
throw | |
} | |
} | |
begin | |
{ | |
try { | |
$outBuffer = $null | |
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) | |
{ | |
$PSBoundParameters['OutBuffer'] = 1 | |
} | |
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Test-Path', [System.Management.Automation.CommandTypes]::Cmdlet) | |
$scriptCmd = {& $wrappedCmd @PSBoundParameters | Set-ExecutionStatus } | |
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) | |
$steppablePipeline.Begin($PSCmdlet) | |
} catch { | |
throw | |
} | |
} | |
process | |
{ | |
try { | |
$steppablePipeline.Process($_) | |
} catch { | |
throw | |
} | |
} | |
end | |
{ | |
try { | |
$steppablePipeline.End() | |
} catch { | |
throw | |
} | |
} | |
<# | |
.ForwardHelpTargetName Microsoft.PowerShell.Management\Test-Path | |
.ForwardHelpCategory Cmdlet | |
#> | |
} |
Author
nmbell
commented
Sep 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment