自定义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...
DefaultParameterSet Property System.String DefaultParameterSet {get;} Definition Property System.String Definition {get;} Description Property System.String Description {get;set;} Module Property System.Management.Automation. PSModuleInfo Module {get;} ModuleName Property System.String ModuleName {get;} ...
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=...
Stop, "Proc", DefaultParameterSetName:="ProcessId", _ SupportsShouldProcess:=True)> _ Public Class StopProcCommand Inherits PSCmdlet Declaring the Parameters of the CmdletThis cmdlet defines three parameters needed as input to the cmdlet (these parameters also define the parameter sets), as w...
从Windows PowerShell 3.0 开始,可以使用“通过 PowerShell 运行”功能从文件资源管理器运行脚本。 使用PowerShell 运行功能旨在运行不具有所需参数、不将输出返回到控制台且不提示用户输入的脚本。 使用“与 PowerShell 一起运行”功能时,PowerShell 控制台窗口仅短暂显示(如果有)。
Sort-Objectcmdlet 根据命令中指定的属性或对象类型的默认排序属性对对象进行排序。 默认排序属性是使用types.ps1xml文件中名为DefaultKeyPropertySet的PropertySet定义的。 有关详细信息,请参阅about_Types.ps1xml。 如果对象没有指定属性之一,则该对象的属性值由Sort-Object解释为Null,并放置在排序顺序的末尾。
可以在 cmdlet 或函数代码中使用对象的属性和方法来响应使用条件。 例如,ParameterSetName 属性包含正在使用的参数集的名称,ShouldProcess 方法将 WhatIf 和Confirm 参数动态添加到 cmdlet。有关$PSCmdlet 自动变量的详细信息,请参阅 about_Functions_CmdletBindingAttribute 和about_Functions_Advanced。$PSCommandPath...