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...
定义多个参数集时,DefaultParameterSetNameCmdletBinding属性的关键字 (keyword) 将指定默认参数集。 如果 PowerShell 无法根据提供给命令的信息确定要使用的参数集,则使用默认参数集。 有关CmdletBinding属性的详细信息,请参阅about_Functions_CmdletBindingAttribute。
而默认的 ParameterSet 是可以使用 CmdLetAttribute 的 DefaultParameterSetName 来做。
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″)] ...
Stop, "Proc", DefaultParameterSetName:="ProcessId", _ SupportsShouldProcess:=True)> _ Public Class StopProcCommand Inherits PSCmdlet 声明Cmdlet 的参数 此cmdlet 定义作为 cmdlet 输入所需的三个参数(这些参数还定义参数集),以及用于管理 cmdlet 执行的作的 Force 参数,以及一个 PassThru 参数,用于确定...
Finding hidden commands The ALL parameter of the Get-Command cmdlet gets all commands with the specified name, even if they're hidden or replaced. Beginning in PowerShell 3.0, by default, Get-Command gets only the commands that run when you type the command name. In the following examples,...
function Test { [CmdletBinding(DefaultParameterSetname = "SetB")] param([Parameter(Position = 0, ParameterSetname = "SetA")] [decimal]$dec, [Parameter(Position = 0, ParameterSetname = "SetB")] [int]$in ) $PsCmdlet.ParameterSetName } Test 42d # outputs "SetA" Test 42 # outputs "Se...
functionGet-User{ [CmdletBinding(DefaultParameterSetName="ID")] [OutputType("System.Int32", ParameterSetName="ID")] [OutputType([String], ParameterSetName="Name")]Param( [parameter(Mandatory=$true, ParameterSetName="ID")] [Int[]]$UserID, [parameter(Mandatory=$true, ParameterSet...
Parameter Set:Default Invoke-WebRequest[-Uri]<Uri>[-Body<Object>][-Certificate<X509Certificate>][-CertificateThumbprint<String>][-ContentType<String>][-Credential<PSCredential>][-DisableKeepAlive][-Headers<IDictionary>][-InFile<String>][-MaximumRedirection<Int32>][-Method<WebRequestMethod>{Default|...