Powershell同时使用可选强制参数
支持所有PS版本
在下面脚本函数中让可选参数和强制参数必须同时使用。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。下面演示当可选参数出现,也必须使用这个强制参数。
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
更多精彩