当前位置: 移动技术网 > IT编程>脚本编程>Shell > Windows Powershell 通过函数扩展别名

Windows Powershell 通过函数扩展别名

2017年12月08日  | 移动技术网IT编程  | 我要评论

在powershell中设置别名的确方便快捷,但是在设置别名的过程中并设置参数的相关信息。尽管别名会自动识别参数,但是如何把经常使用的参数默认设定在别名里面呢?例如test-connection -count 2 -computername,让-”-count 2″ 固化在别名中。

这时简单的别名无法完成上述需求,可以通过函数来完成它,并且一旦把函数拉过来,定义别名会变得更加灵活。

ps c:\ps> function test-conn { test-connection -count 2 -computername $args}
ps c:\ps> set-alias tc test-conn
ps c:\ps> tc localhost

source    destination   ipv4address   ipv6address               bytes  time(ms)
------    -----------   -----------   -----------               -----  --------
test-me-01  localhost    127.0.0.1    ::1                   32    0
test-me-01  localhost    127.0.0.1    ::1                   32    0

有了函数牵线,别名可以完成更高级更强大的功能,其中$args为参数的占位符,经测试,发现这个占位符必须以$args命名,否则不能识别,会抛出异常:
cannot validate argument on parameter ‘computername'. the argument is null or empty. supply an arg
nt that is not null or empty and then try the command again.

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网