powershell cmdlet 参数的较短版本

发布于 2024-12-02 03:35:52 字数 524 浏览 1 评论 0原文

根据我的研究,我认为以下任务即使有的话也不容易完成。然而,作为最后的手段,我想我应该检查一下这里。

在 Powershell 2.0 中,我想要一种方法来减少各种 cmdlet 参数的(令人烦恼的)长名称。我想绝对控制速记版本的外观。 (而不是成为 PS 使用的任何参数缩写方案的奴隶。)

因此,例如,我希望能够做这样的事情:

# Command goes on this first line to alias "-ForegroundColor" to "-fg"
# Command goes on this second line to alias "-BackgroundColor" to "-bg"
Wr-te-Host -fg yellow -bg black "Parameter aliases now work just like I want."

我能得到的最接近此功能的是什么,以及如何实现?我无法使用“get-help about_parameters”找到任何有关参数缩写的信息。

谢谢!

Given my research, I don't believe the following is easily accomplished, if at all. As a last resort, however, I figured I'd check here.

In Powershell 2.0, I'd like a way to reduce the (annoyingly) long names of parameters to various cmdlets. I would like absolute control over what the shorthand version looks like. (As opposed to being a slave to whatever parameter abbreviation scheme PS uses.)

So, for example, I'd like to be able to do something like this:

# Command goes on this first line to alias "-ForegroundColor" to "-fg"
# Command goes on this second line to alias "-BackgroundColor" to "-bg"
Wr-te-Host -fg yellow -bg black "Parameter aliases now work just like I want."

What's the closest I can get to this functionality, and how? I was not able to find anything regarding parameter abbreviation using 'get-help about_parameters'.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海 2024-12-09 03:35:52

您可以为自己的函数创建参数别名,如下所示:

function ParamAlias {
    param(
        [Alias('fg','fColor')]
        $ForegroundColor
    )

    Write-Host "$ForegroundColor" -ForegroundColor $ForegroundColor
}

ParamAlias -fg Green
ParamAlias -fColor Green

然后您可以将此技术与 代理 CmdLets 将您自己的别名添加到现有 CmdLets。但是,我发现在控制台中使用现有参数别名/缩短的参数名称就足够了,并且您不应该在脚本中使用别名,所以我不确定这是否值得。我会同意@Shay的回答

You can create parameter aliases for your own functions like so:

function ParamAlias {
    param(
        [Alias('fg','fColor')]
        $ForegroundColor
    )

    Write-Host "$ForegroundColor" -ForegroundColor $ForegroundColor
}

ParamAlias -fg Green
ParamAlias -fColor Green

You could then use this technique with Proxy CmdLets to add your own aliases to existing CmdLets. However, I find it sufficient to use existing parameter aliases/shortened parameter names in the console, and you shouldn't use aliases in scripts, so I'm not sure this would be worth the effort. I would go with @Shay's answer

时光匆匆的小流年 2024-12-09 03:35:52

检查此脚本: Get-Parameter.ps1

点源它并执行以下命令,它提供了有关命令参数的大量信息。看一下别名列,它将显示所有内置参数别名并计算可用于参数的最短名称:

PS > Get-Parameter Write-Host


    Command: Microsoft.PowerShell.Utility/Write-Host
    Set:     Default


Name                   Aliases      Position Mandatory Pipeline ByName Provider        Type
----                   -------      -------- --------- -------- ------ --------        ----
BackgroundColor        {b}          Named    False     False    False  All             ConsoleColor
ForegroundColor        {f}          Named    False     False    False  All             ConsoleColor
NoNewline              {n}          Named    False     False    False  All             SwitchParameter
Object                 {obj}        0        False     True     False  All             Object
Separator              {s}          Named    False     False    False  All             Object

Check this script: Get-Parameter.ps1

dot-source it and execute the following, it gives a wealth of information about a command parameters. Take a look at the aliases column, it will show all built-in parameter aliases as well as calculates the shortest name you can use for a parameter:

PS > Get-Parameter Write-Host


    Command: Microsoft.PowerShell.Utility/Write-Host
    Set:     Default


Name                   Aliases      Position Mandatory Pipeline ByName Provider        Type
----                   -------      -------- --------- -------- ------ --------        ----
BackgroundColor        {b}          Named    False     False    False  All             ConsoleColor
ForegroundColor        {f}          Named    False     False    False  All             ConsoleColor
NoNewline              {n}          Named    False     False    False  All             SwitchParameter
Object                 {obj}        0        False     True     False  All             Object
Separator              {s}          Named    False     False    False  All             Object
尘曦 2024-12-09 03:35:52

像这样的东西将为您提供 cmdlet 参数的现有别名:

Get-Command write-host |
    ForEach-Object {$_.parameters |
        ForEach-Object { $_.Values |
            Where-Object {
                $_.Aliases.Count -gt 0 } |
                Select-Object Name, Aliases
            }
    }

不过,我确实没有找到“控制”别名的方法。

Something like this would give you the existing aliases for a cmdlet's parameters:

Get-Command write-host |
    ForEach-Object {$_.parameters |
        ForEach-Object { $_.Values |
            Where-Object {
                $_.Aliases.Count -gt 0 } |
                Select-Object Name, Aliases
            }
    }

I don't really see a way for "controlling" the aliases though.

猫卆 2024-12-09 03:35:52

给定 cmdlet 的参数只需具有足够的参数即可使其在该 cmdlet 中与众不同。诸如 Get-Member -m Property(-m 代表 MemberType,这是此 cmdlet 的唯一“M”参数)之类的内容。

如果我要快速输入一行内容,我倾向于仅使用参数的前 3 个字符。这在大多数情况下都有效,并且与 Cisco 的 CLI(如果您曾经使用过的话)类似。当我调试时,我会时不时地标记出参数,以确保我引用的是正确的参数。

IMO,我尽量不在脚本中做太多事情。我尝试使我的脚本尽可能便于其他可能不知道每个 cmdlet 别名的人阅读。它有助于将脚本传递给其他人。如果你读过唐·琼斯的博客/文章,他也会谈到这一点。然而,如果脚本只适合我,我会尽可能使其简短和快速。

Parameters of a given cmdlet only need to have enough of it to make it distinct within that cmdlet. Things like Get-Member -m Property (-m stands for MemberType, which is the only "M" parameter for this cmdlet).

If I am typing out a quick one-liner I tend to use only the first 3 characters of the parameter. This works most of the time and is similar to Cisco's CLI if you ever worked with that. Every now and then I will tab the parameter out when I am debugging, to make sure I am referencing the right one.

IMO, I try not to do it to much in scripts. I try to make my scripts as readable as I can for other folks that may not know the aliases of every cmdlet. It helps in passing on scripts to other people. If you read Don Jones blog/articles he talks about this some too. However, if the script is just for me I make it as short and quick as possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文