简体   繁体   English

Set-Alias 中包含空格字符的值失败

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

I want to use ⍋ to sort ascending , and use ⍒ to sort descending .我想用 ⍋升序排序,用 ⍒降序排序。

I successfully set ⍋ as an alias for sort (default is ascending):我成功地将 ⍋ 设置为排序的别名(默认为升序):

Set-Alias -Name ⍋ -Value Sort-Object

I failed to set ⍒ to an alias for sort descending:未能将 ⍒ 设置为降序排序的别名:

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

Here is the error message I received:这是我收到的错误消息:

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

Any help would be appreciated.任何帮助,将不胜感激。

You can not set aliases that include parameters.您不能设置包含参数的别名。 You need to create a function that wraps the cmdlet.您需要创建一个包装 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 }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM