设置 - 阿里亚斯在其中有空格字符的值



我想用⍋排序 appending ,然后使用⍒排序 descing

我成功地将⍋作为排序的别名(默认正在上升):

Set-Alias -Name ⍋ -Value Sort-Object

i 失败将⍒设置为discending的别名:

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 }
    }
}