powershell ParameterSet解析 自定义PowerShell函数,在设置参数的时候中,可以将参数设置为某些情况下可选,某些条件下又设置为必选。 示例代码从网站复制的。 1functionConnect-Somewhere2{3[CmdletBinding(DefaultParameterSetName='A')]4param5(6[Parameter(ParameterSetName='A',Mandatory=$false)]7[Parameter(Paramete...
下面演示当可选参数出现,也必须使用这个强制参数。 functionConnect-Somewhere { [CmdletBinding(DefaultParameterSetName='A')]param( [Parameter(ParameterSetName='A',Mandatory=$false)] [Parameter(ParameterSetName='B',Mandatory=$true)]$ComputerName, [Parameter(ParameterSetName='B',Mandatory=$false)]$Crede...
自动变量$PSCmdlet提供ParameterSetName属性。 此属性包含正在使用的参数集的名称。 可以在函数中使用此属性来确定哪个参数集用于选择特定于参数集的行为。 PowerShell functionGet-ParameterSetName{ [CmdletBinding(DefaultParameterSetName ='Set1')]param( [Parameter(ParameterSetName ='Set1', Position =0)]$Var1...
TypeName: System.Management.Automation.FunctionInfo Name MemberType Definition --- --- --- CmdletBinding Property System.Boolean CmdletBinding {get;} CommandType Property System.Management.Automation. CommandTypes CommandType {get;} DefaultParameterSet Property System.String DefaultParameterSet {get;} Defini...
function Get-User { [CmdletBinding(DefaultParameterSetName="ID")] [OutputType("System.Int32", ParameterSetName="ID")] [OutputType([String], ParameterSetName="Name")] Param ( [parameter(Mandatory=$true, ParameterSetName="ID")] [Int[]] $UserID, [parameter(Mandatory=$...
Wouldn’t it be great if the PowerShell team thought about such a circumstance and give you a mechanism to specify which parameterset to pick if things were ambiguous? OH WAIT – they did! function test-param { [CmdletBinding(DefaultParametersetName=”p2″)] ...
functionTest-MrParameter{param($ComputerName)Write-Output$ComputerName} 以下函数可查询系统中的所有命令,并返回带有特定参数名称的命令编号。 PowerShell functionGet-MrParameterCount{param( [string[]]$ParameterName)foreach($Parameterin$ParameterName) {$Results=Get-Command-ParameterName$Parameter-ErrorActionSil...
可以通过在命令提示符处输入$PSDefaultParameterValues来查看定义的值。 PowerShell PS>$PSDefaultParameterValuesName Value --- ---Get-WinEvent:LogNameMicrosoft-Windows-PrintService/Operational Get-*:Verbose TrueSend-MailMessage:SmtpServer Server123 可以使用...
这里的 ValidateRange 是可以规定一些验证的。这个其实我们可以通过 advanced function parameter 的帮助文件来进一步学习。不在这里展开了。这里需要注意的是,我们是可以规定 parameterset 的。而默认的 ParameterSet 是可以使用 CmdLetAttribute 的 DefaultParameterSetName 来做。
To define a default value for a parameter, type an equal sign and the value after the parameter name, as shown in the following variation of theGet-SmallFilesexample: PowerShell functionGet-SmallFiles($Size=100) {Get-ChildItem$HOME|Where-Object{$_.Length-lt$Size-and!$_.PSIsContainer } } ...