Powershell同时使用可选强制参数


支持所有PS版本

在下面脚本函数中让可选参数和强制参数必须同时使用。

下面演示当可选参数出现,也必须使用这个强制参数。

function Connect-Somewhere
{
 [CmdletBinding(DefaultParameterSetName='A')]
 param
 (
 [Parameter(ParameterSetName='A',Mandatory=$false)]
 [Parameter(ParameterSetName='B',Mandatory=$true)]
 $ComputerName,
 [Parameter(ParameterSetName='B',Mandatory=$false)]
 $Credential
 )
 $chosen = $PSCmdlet.ParameterSetName
 "You have chosen $chosen parameter set."
}

# -Computername is optional
Connect-Somewhere
# here, -Computername is mandatory
Connect-Somewhere -Credential test 

原文地址:Optional and Mandatory at the Same Time

本文链接: https://www.pstips.net/optional-and-mandatory-at-the-same-time.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注