Skip to content

Instantly share code, notes, and snippets.

@nmbell
Created September 26, 2021 04:09
Show Gist options
  • Save nmbell/ddfdd12a9911661c60c56c9f2e3ecac1 to your computer and use it in GitHub Desktop.
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
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
#>
}
@nmbell
Copy link
Author

nmbell commented Sep 26, 2021

PS C:\> $path = Get-Process -Id $PID | Select-Object -ExpandProperty Path
PS C:\> Test-Path -Path $path -ErrorAction Ignore && Write-Host "$path found"
C:\Program Files\PowerShell\7\pwsh.exe found
PS C:\> $path = 'C:\NoSuchFile.txt'
PS C:\> Test-Path -Path $path -ErrorAction Ignore || Write-Host "$path not found"
C:\NoSuchFile.txt not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment