Windows Powershell 通过函数扩展别名

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Windows Powershell 通过函数扩展别名脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

在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.

脚本宝典总结

以上是脚本宝典为你收集整理的Windows Powershell 通过函数扩展别名全部内容,希望文章能够帮你解决Windows Powershell 通过函数扩展别名所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。