对于其中包含 space 字符的值,Set-Alias 失败

Set-Alias fails for value with a space character in it

我想用⍋排序升序,用⍒排序降序.

我成功设置了⍋作为排序的别名(默认是升序):

Set-Alias -Name ⍋ -Value Sort-Object

未能将 ⍒ 设置为降序排序的别名:

Set-Alias -Name ⍒ -Value Sort-Object -Descending

这是我收到的错误消息:

Set-Alias : A parameter cannot be found that matches parameter name 'Descending'.
    At line:1 char:38
    + Set-Alias -Name ⍒ -Value Sort-Object -Descending
    +                                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Alias], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

如有任何帮助,我们将不胜感激。

您不能设置包含参数的别名。 您需要创建一个包装 cmdlet 的函数。

function Sort-ObjectDescending
{
    [Alias("⍒")]

    param(
        [Parameter(Position = 0)]
        [Object[]]
        $Property,

        [Switch]
        $CaseSensitive,

        [String]
        $Culture,

        [Switch]
        $Unique,

        [Parameter(ValueFromPipeline)]
        [PSObject]
        $InputObject
    )

    begin {
        try {
            $scriptCmd = { Sort-Object -Descending @PSBoundParameters }
            $steppablePipeline = $scriptCmd.GetSteppablePipeline()
            $steppablePipeline.Begin($PSCmdlet)
        } catch {
            throw
        }
    }

    process {
        try { $steppablePipeline.Process($_) } catch { throw }
    }

    end {
        try { $steppablePipeline.End() } catch { throw }
    }
}