Windows Powershell 通过函数扩展别名
在Powershell中设置别名的确方便快捷,但是在设置别名的过程中并设置参数的相关信息。尽管别名会自动识别参数,但是如何把经常使用的参数默认设定在别名里面呢?例如Test-Connection-Count2-ComputerName,让-”-Count2″固化在别名中。
这时简单的别名无法完成上述需求,可以通过函数来完成它,并且一旦把函数拉过来,定义别名会变得更加灵活。
PSC:\PS>functiontest-conn{Test-Connection-Count2-ComputerName$args}
PSC:\PS>Set-Aliastctest-conn
PSC:\PS>tclocalhost
SourceDestinationIPV4AddressIPV6AddressBytesTime(ms)
----------------------------------------------------
test-me-01localhost127.0.0.1::1320
test-me-01localhost127.0.0.1::1320
有了函数牵线,别名可以完成更高级更强大的功能,其中$args为参数的占位符,经测试,发现这个占位符必须以$args命名,否则不能识别,会抛出异常:
Cannotvalidateargumentonparameter‘ComputerName'.Theargumentisnullorempty.Supplyanarg
ntthatisnotnulloremptyandthentrythecommandagain.
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下