Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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_Powershell 2.0 - Fatal编程技术网

Powershell 具有需要另一个参数存在的可选参数

Powershell 具有需要另一个参数存在的可选参数,powershell,powershell-2.0,Powershell,Powershell 2.0,很简单,我如何初始化Powershell脚本的params部分,这样我就可以有一个命令行参数,如 Get-Foo [-foo1] <foo1Arg> [-foo2 <foo2Arg> [-bar <barArg>]] 但是,我不知道如何设置依赖参数。您需要参数集,请阅读此处了解更多信息: 您的代码示例: [CmdletBinding(DefaultParameterSetName="set1")] param ( [Parameter(Param

很简单,我如何初始化Powershell脚本的
params
部分,这样我就可以有一个命令行参数,如

Get-Foo [-foo1] <foo1Arg> [-foo2 <foo2Arg> [-bar <barArg>]]

但是,我不知道如何设置依赖参数。

您需要参数集,请阅读此处了解更多信息:

您的代码示例:

[CmdletBinding(DefaultParameterSetName="set1")]
param (
    [Parameter(ParameterSetName="set1", Mandatory=$true)]
    [string]$foo1,
    [Parameter(ParameterSetName="set2",  Mandatory=$true)]
    [string]$foo2,
    [Parameter(ParameterSetName="set2")]
    [string]$bar
)

我对原始问题的理解与C.B.的略有不同。从

Get-Foo [-foo1] <foo1Arg> [-foo2 <foo2Arg> [-bar <barArg>]]
然后,当您运行上述命令时,您将获得以下输出

Foo is Hello
Foo is Hello with no argument switch
Foo is Hello, Foo2 is There is no bar here
Foo is Hello, Foo2 is There. Bar is Three
This Stops for input as foo2 is not specified

cmdlet Get-Foo at command pipeline position 1
Supply values for the following parameters:
foo2: Typedfoo2
Foo is Hello, Foo2 is Typedfoo2. Bar is No foo2

检查此问题:这正确地要求指定
-foo2
,以便指定
-bar
,但是它不允许您除了指定
-foo1
之外还指定
-foo2
,因为它们位于不同的参数集中。
function Get-Foo
{
[CmdletBinding(DefaultParameterSetName="set1")]
param (
    [Parameter(ParameterSetName="set1", Mandatory=$true, Position=0)]
    [Parameter(ParameterSetName="set2", Mandatory=$true, Position=0) ]
    [string]$foo1,
    [Parameter(ParameterSetName="set2",  Mandatory=$true)]
    [string]$foo2,
    [Parameter(ParameterSetName="set2", Mandatory=$false)]
    [string]$bar
)
    switch ($PSCmdlet.ParameterSetName)
    {
        "set1"
        {
            $Output= "Foo is $foo1"
        }
        "set2"
        {
            if ($bar) { $Output= "Foo is $foo1, Foo2 is $foo2. Bar is $Bar" }
            else      { $Output= "Foo is $foo1, Foo2 is $foo2"}
        }
    }
    Write-Host $Output
}

Get-Foo -foo1 "Hello"
Get-Foo "Hello with no argument switch"
Get-Foo "Hello" -foo2 "There is no bar here"
Get-Foo "Hello" -foo2 "There" -bar "Three"
Write-Host "This Stops for input as foo2 is not specified"
Get-Foo -foo1 "Hello" -bar "No foo2" 
Foo is Hello
Foo is Hello with no argument switch
Foo is Hello, Foo2 is There is no bar here
Foo is Hello, Foo2 is There. Bar is Three
This Stops for input as foo2 is not specified

cmdlet Get-Foo at command pipeline position 1
Supply values for the following parameters:
foo2: Typedfoo2
Foo is Hello, Foo2 is Typedfoo2. Bar is No foo2