灵活运用ParameterSetName于多种使用场景
来源:互联网 发布:编程教师培训 编辑:程序博客网 时间:2024/06/06 01:51
在PowerShell中许多cmdlet都支持大量的参数集。我们用Get-Help去查看相应的cmdlet时就能看到参数集。
例如,我们使用Get-Help Get-Process在查看使用语句用法,显示结果如下:
Get-Process SYNTAX Get-Process [[-Name] <string[]>] [-ComputerName <string[]>] [-Module] [-FileVersionInfo] [<CommonParameters>] Get-Process [[-Name] <string[]>] -IncludeUserName [<CommonParameters>] Get-Process -Id <int[]> [-ComputerName <string[]>] [-Module] [-FileVersionInfo] [<CommonParameters>] Get-Process -Id <int[]> -IncludeUserName [<CommonParameters>] Get-Process -InputObject <Process[]> [-ComputerName <string[]>] [-Module] [-FileVersionInfo] [<CommonParameters>] Get-Process -InputObject <Process[]> -IncludeUserName [<CommonParameters>]
我们可以看到Get-Process有多种的使用方法正是因为使用到了多种的参数集。
对于熟悉PowerShell的朋友来说,在编写高级函数时,我们经常会设置参数集,我们今天正是要来再次探讨下关于参数集的使用。
我们先从下面这个列子说起吧。
Function Get-Somthing { [CmdletBinding()] Param ( [Parameter(ParameterSetName='List1')][String]$apple, [Parameter(ParameterSetName='List1')][String]$orange, [Parameter(ParameterSetName='List1')][String]$mango, [Parameter(ParameterSetName='List2')][String]$water, [Parameter(ParameterSetName='List2')][String]$milk, [Parameter(ParameterSetName='List2')][String]$juice, [Parameter(ParameterSetName='List3')][String]$phone, [Parameter(ParameterSetName='List3')][String]$computer, [Parameter(ParameterSetName='List3')][String]$camera, [Parameter()][String]$boy, [Parameter()][String]$girl ) }在编写好如上列子后,我们一般会使用下面的方式去调用方法。
Get-Somthing -apple "one" -orange "one" -boy "eat" Get-Somthing -water "one" -juice "one" -girl "eat" Get-Somthing -computer "one" -camera "one" -boy "eat"
由此可以看出,采用了参数集后,我们调用的参数必须是归纳在相同的参数集名称范围里的参数,而没有设定参数集的参数则可以和任何参数及的成员搭配。
我们再来看看下面没有正确使用参数集的例子:
Get-Somthing -apple "one" -phone "two" Get-Somthing -boy "23" -girl "21"
第一个例子,很明显两个参数,apple与phone存在于不同的参数集,自然是不正确的。但是奇怪的是第二个例子也依然是不正确的用法。因为boy与girl参数不隶属于任何参数集,也代表他们可以和任何参数集的成员一起使用,而PowerShell是不知道你到底想使用哪一个参数成员。
另外,在定义参数集时,我们不能重复把一个参数成员定义在不同的参数集下面。如下面的例子,同一个变量不能重复定义在超过一个以上的参数集。
Function Get-Somthing { [CmdletBinding()] Param ( [Parameter(ParameterSetName='List1')][String]$apple, [Parameter(ParameterSetName='List1')][String]$orange, [Parameter(ParameterSetName='List1')][String]$mango, [Parameter(ParameterSetName='List2')][String]$apple, [Parameter(ParameterSetName='List2')][String]$milk, [Parameter(ParameterSetName='List2')][String]$juice ) }但是我们可以让一个变量成员隶属于多个参数集,就像下面这个样子:
Function Get-Somthing { [CmdletBinding()] Param ( [Parameter(ParameterSetName='List1')][String]$apple, [Parameter(ParameterSetName='List1')][String]$orange, [Parameter(ParameterSetName='List1')][String]$mango, [Parameter(ParameterSetName='List2')][String]$water, [Parameter(ParameterSetName='List2')][String]$milk, [Parameter(ParameterSetName='List2')][String]$juice, [Parameter(ParameterSetName='List3')][String]$phone, [Parameter(ParameterSetName='List3')][String]$computer, [Parameter(ParameterSetName='List3')][String]$camera, [Parameter(ParameterSetName='List1')] [Parameter(ParameterSetName='List2')] [String]$car, [Parameter()][String]$boy, [Parameter()][String]$girl ) }使用的时候就可以是:
Get-Somthing -apple "one" -car "20" Get-Somthing -milk "one" -car "20"让我们回过头再次使用Get-Help Get-Somthing命令时会得到如下:
NAME Get-Somthing SYNTAX Get-Somthing [-apple <string>] [-orange <string>] [-mango <string>] [-car <string>] [-boy <string>] [-girl <string >] [<CommonParameters>] Get-Somthing [-water <string>] [-milk <string>] [-juice <string>] [-car <string>] [-boy <string>] [-girl <string>] [<CommonParameters>] Get-Somthing [-phone <string>] [-computer <string>] [-camera <string>] [-boy <string>] [-girl <string>] [<CommonP arameters>]
就像开场白所说的那样,你在帮助里看到的使用语法正是我们使用了参数集的作用。
最后在提一点,你甚至可以设置默认参数集,像这样[CmdletBinding(DefaultParameterSetName='List3')],当你再次使用Get-Help Get-Somthing命令时,默认第一条使用的语句方法,正是你设置的默认参数集。
NAME Get-Somthing SYNTAX Get-Somthing [-phone <string>] [-computer <string>] [-camera <string>] [-boy <string>] [-girl <string>] [<CommonParameters>] Get-Somthing [-apple <string>] [-orange <string>] [-mango <string>] [-car <string>] [-boy <string>] [-girl <string>] [<CommonParameters>] Get-Somthing [-water <string>] [-milk <string>] [-juice <string>] [-car <string>] [-boy <string>] [-girl <string>] [<CommonParameters>]
0 0
- 灵活运用ParameterSetName于多种使用场景
- 灵活运用政务信息系统
- 灵活运用__doPostBack函数
- 灵活运用__doPostBack函数
- 灵活运用__doPostBack函数
- 灵活运用__doPostBack函数
- 灵活运用__doPostBack函数
- MathTypy的灵活运用
- 灵活运用枚举
- onMouseover的灵活运用
- 灵活运用数据库主外键
- javascript “||”、“&&”的灵活运用
- 灵活运用递增递减
- MathType的灵活运用
- javascript “||”、“&&”的灵活运用
- javascript “||”、“&&”的灵活运用
- break语句灵活运用
- 灵活运用PHP stdClass
- 有关oracle11g中scott用户被锁的解锁
- include 布局android:layout_below属性无法使用
- 正则表达式【项目中碰到使用的】
- 笔试题66. LeetCode OJ (53)
- ASE :Space available in segment 'logsegment' has fallen critically low in database
- 灵活运用ParameterSetName于多种使用场景
- python(十三)正则表达式
- 平台搭建1:Virtualbox虚拟机下安装ubuntu12.04以及共享文件夹的设置
- sql in '1,2' 转换成int
- Spring3.2.11与Quartz2.2.1整合时内存泄漏问题解决
- c++ byte与int
- Ubuntu 14.04+cuda7.5+torch+tensorflow+remastersys
- [1.3]Spark core编程(二)之RDD执行流程图与RDD的基本操作
- 查看NAND Flash空间大小