Windows Powershell 通过函数扩展别名 Windows Powershell 通过函数扩展别名

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Windows Powershell 通过函数扩展别名 Windows Powershell 通过函数扩展别名

  2021-03-19 我要评论
想了解Windows Powershell 通过函数扩展别名的相关内容吗,在本文为您仔细讲解Windows Powershell 通过函数扩展别名的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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.

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们