powershell - 较短版本的 powershell cmdlet 参数

标签 powershell parameters alias cmdlet

鉴于我的研究,我认为以下内容不容易完成,如果有的话。然而,作为最后的手段,我想我会检查这里。

在 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”找到任何有关参数缩写的信息。

谢谢!

最佳答案

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

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

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

ParamAlias -fg Green
ParamAlias -fColor Green

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

关于powershell - 较短版本的 powershell cmdlet 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7262802/

相关文章:

ruby - 为什么从代码块中的双拼参数调用 `to_ary`?

asp.net - http post请求在ASP NET MVC中将参数转换为 'Null'

tcl - TCL 8.3 : syntax error in expression “a eq b”

mysql - PowerShell 脚本需要几个小时才能完成

azure - 如何使用 powershell 设置 azure 逻辑应用定义

Powershell 报告列出修补程序

java - 如何按参数对数组列表进行排序?

azure - 实现重试逻辑后显示角色分配已存在错误

google-app-engine - 如何在 Google Apps 中添加支持 SSL 的别名子域

Linux 外壳 : How to give parameter to alias of find command?