带有强制和可选参数的函数

时间:2021-07-07 05:10:22

标签: powershell powershell-5.1

下面是我配置所需服务的代码,其中我看到错误,因为无法使用指定的命名参数解析参数集。发出的一个或多个参数不能一起使用或提供的参数数量不足。执行时。

如果传递了任何一个参数,代码应该可以工作,否则应该抛出错误消息。

param (
[Parameter(Position=0,Mandatory=$false,ParameterSetName="checks")]
[ValidateSet("srv")]
[switch]$service,
[Parameter(Position=1,Mandatory=$false,ParameterSetName="checks")]
[ValidateSet("dsk")]
[switch]$disk,
[Parameter(Position=2,Mandatory=$false,ParameterSetName="details")]
[ValidateSet("nme")]
[switch]$name)

function check_types ($service,$disk,$name) {

    if ($disk -or $service -or $name) {
      Write-Host "Argument" $name $service $disk "Provided" -ForegroundColor Green
    } else {
      Write-Host "No arguments provided" -ForegroundColor Red
    }
}

check_types -service srv -disk dsk -name nme

0 个答案:

没有答案