Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell参数集和_Powershell - Fatal编程技术网

Powershell参数集和

Powershell参数集和,powershell,Powershell,我有一个我想要的函数,它需要一个文件名、输出字符串和可选的值数组(基本上是传递给该函数的所有其他内容) 声明的起始点如下: func { [CmdletBinding()] Param ( [Parameter(Mandatory=$true, ParameterSetName="Default", Position=1)] [Parameter(Mandatory=$true, ParameterSetName="Variable", Position=1)

我有一个我想要的函数,它需要一个文件名、输出字符串和可选的值数组(基本上是传递给该函数的所有其他内容)

声明的起始点如下:

func {
    [CmdletBinding()]

    Param (

    [Parameter(Mandatory=$true, ParameterSetName="Default", Position=1)]
    [Parameter(Mandatory=$true, ParameterSetName="Variable", Position=1)]
    [ValidateNotNullOrEmpty()] 
    [String]$FileName, 

    [Parameter(Mandatory=$true, ParameterSetName="Default", Position=2)]
    [Parameter(Mandatory=$true, ParameterSetName="Variable", Position=2)]
    [String]$Output, 

    [Parameter(Mandatory=$false, ParameterSetName="Default", Position=3)]
    [Parameter(Mandatory=$true, ParameterSetName="Variable", ValueFromRemainingArguments=$true )]
    [string[]]$Variables 

    )
}
但是,我无法使函数按我的意愿运行。调用函数时出现以下错误:

func: Parameter set cannot be resolved using the specified named parameters.
At S:\PS\Home\Library\PSStdLib\PSStdLib.psm1:595 char:9
+         func"$psExecutionLog" "{0,-19} {1,-35} {2,-25} {3,-25} { ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [func], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,func

我已经盯着这个看了好几个小时了。我试图理解我做错了什么。我就是看不出来。

PowerShell被要使用的参数集弄糊涂了

  • 您的函数无效。缺少
    功能
    关键字
  • 参数集不需要有一个可选参数
  • 您不必告诉
    value from remainingarguments
    为必填项(参数本身为可选项)。要么有参数,要么显式调用参数,要么没有值
我会忽略参数设置,只是这样做

function func {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$true, Position=1)]
        [ValidateNotNullOrEmpty()] 
        [String]$FileName, 

        [Parameter(Mandatory=$true, Position=2)]
        [String]$Output, 

        [Parameter(Position=3, ValueFromRemainingArguments=$true)]
        [string[]]$Variables 

    )
}
请注意,
value from remainingarguments
可能会使用法有点混乱。如果
-Variables
用作参数,则数组元素用逗号
分隔。如果使用它来捕获参数,则元素之间用空格分隔。不能同时使用这两个参数(明确使用参数和捕捉参数)


如何调用该方法?为什么要定义两次参数属性?答案有效吗?
#No parameter or arguments
func -FileName "file" -Output "out"

#Parameter
func -FileName "file" -Output "out" -Variables "hello","world"

#ValueFromRemainingArguments
func -FileName "file" -Output "out" "hello" "world"

#ERROR: Can't use both parameter and ValueFromRemainingArguments at the same time
func -FileName "file" -Output "out" -Variables "hello","world" "foo"