PowerShell中的代理功能不接受管道输入

时间:2017-04-17 17:12:52

标签: powershell

我为Remove-Item创建了proxy function,它会删除到回收站而不是永久删除(使用代理,以便我可以无缝地替换rm别名,而不会破坏第3个派对剧本)。

但是,当文件通过管道输入函数时,它不起作用。代理功能的核心是:

if ($PSBoundParameters['DeletePermanently'] -or $PSBoundParameters['LiteralPath'] -or $PSBoundParameters['Filter'] -or $PSBoundParameters['Include'] -or $PSBoundParameters['Exclude'] -or $PSBoundParameters['Recurse'] -or $PSBoundParameters['Force'] -or $PSBoundParameters['Credential']) {
    if ($PSBoundParameters['DeletePermanently']) { $PSBoundParameters.Remove('DeletePermanently') | Out-Null }
    $scriptCmd = {& $wrappedCmd @PSBoundParameters }
} else {
    $scriptCmd = {& Recycle-Item -Path $PSBoundParameters['Path'] }
}

因此,仅当Recycle-Item是唯一参数时,才会调用我的自定义Path函数。因此,Get-ChildItem .\temp\ | rm -DeletePermanently之类的工作正常,但Get-ChildItem .\temp\ | rm有错误,因为传递给Path的{​​{1}}为Recycle-Item

我已尝试传递$null而不是$Path,并试图像上面$PSBoundParameters['Path']的调用那样展开@PSBoundParameters,但似乎没有任何好处。我已将$wrappedCmd从此函数复制到params,以确保它期望来自管道的输入,但这似乎也没有帮助。其中一些更改似乎传递了文件名,但不是完整路径,所以我不知道Recycle-Item中是否有一些魔法需要复制以处理来自管道的文件对象。

Remove-Item只是一个基本功能:

Recycle-Item

1 个答案:

答案 0 :(得分:2)

正如评论中所提到的,当您在它们之间管道对象时,提供程序cmdlet通常会绑定在LiteralPath上。这种方式允许Path支持通配符通配,而不会在cmdlet之间传递不明确的项目路径。

Remove-Item只有两个参数集,它们以强制参数PathLiteralPath

命名

要解决您的问题,只需检查这两个中的一个的所有已定义参数,然后根据Remove-Item值将相应的值传递给$PSCmdlet.ParameterSetName

if(@($PSBoundParameters.Keys |Where-Object {@('DeletePermanently','Filter','Include','Exclude','Recurse','Force','Credential') -contains $_}).Count -ge 1){
    # a parameter other than the Path/LiteralPath or the common parameters was specified, default to Remove-Item
    if ($PSBoundParameters['DeletePermanently']) { 
        $PSBoundParameters.Remove('DeletePermanently') | Out-Null 
    }
    $scriptCmd = {& $wrappedCmd @PSBoundParameters }
} else {
    # apart from common parameters, only Path/LiteralPath was specified, go for Recycle-Item
    $scriptCmd = {& Recycle-Item -Path $PSBoundParameters[$PSCmdlet.ParameterSetName] }
}