自定义PowerShell函数,在设置参数的时候中,可以将参数设置为某些情况下可选,某些条件下又设置为必选。 示例代码从网站复制的。 1functionConnect-Somewhere2{3[CmdletBinding(DefaultParameterSetName='A')]4param5(6[Parameter(ParameterSetName='A',Mandatory=$false)]7[Parameter(ParameterSetName='B',Mandatory=$...
定义多个参数集时,DefaultParameterSetNameCmdletBinding属性的关键字 (keyword) 将指定默认参数集。 如果 PowerShell 无法根据提供给命令的信息确定要使用的参数集,则使用默认参数集。 有关CmdletBinding属性的详细信息,请参阅about_Functions_CmdletBindingAttribute。
下面演示当可选参数出现,也必须使用这个强制参数。 functionConnect-Somewhere { [CmdletBinding(DefaultParameterSetName='A')]param( [Parameter(ParameterSetName='A',Mandatory=$false)] [Parameter(ParameterSetName='B',Mandatory=$true)]$ComputerName, [Parameter(ParameterSetName='B',Mandatory=$false)]$Crede...
PowerShell uses the default parameter set if it can't determine the parameter set to use based on the information provided by the command. For more information about the Cmdlet attribute, see Cmdlet Attribute Declaration. Declaring parameter sets To create a parameter set, you must specify the ...
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″)] ...
functionGet-User{ [CmdletBinding(DefaultParameterSetName="ID")] [OutputType("System.Int32", ParameterSetName="ID")] [OutputType([String], ParameterSetName="Name")]Param( [parameter(Mandatory=$true, ParameterSetName="ID")] [Int[]]$UserID, [parameter(Mandatory=...
SetEnvironmentVariable()使用 方法 使用系统控制面板 在配置文件中设置环境变量 在PowerShell 配置文件中添加或更改的任何环境变量在加载配置文件的任何会话中都可用。 此方法适用于任何受支持的平台上的任何 PowerShell 版本。 例如,若要创建CompanyUri环境变量并更新Path环境变量以包含 文件夹C:\Tools,请将以下行添...
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|...
Sort-Objectcmdlet 根据命令中指定的属性或对象类型的默认排序属性对对象进行排序。 默认排序属性是使用PropertySet文件中名为DefaultKeyPropertySet的types.ps1xml定义的。 有关详细信息,请参阅about_Types.ps1xml。 如果对象没有指定属性之一,则该对象的属性值由Sort-Object解释为Null,并放置在排序顺序的末尾。
To find the default parameter value, see help topic for the cmdlet. The parameter description should include the default value. You can also set a custom default value for any parameter of a cmdlet or advanced function. For information about setting custom default values, seeabout_Parameters_Def...