Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
Function powershell中是否有方法捕获所有命名参数_Function_Powershell_Arguments - Fatal编程技术网

Function powershell中是否有方法捕获所有命名参数

Function powershell中是否有方法捕获所有命名参数,function,powershell,arguments,Function,Powershell,Arguments,考虑这一点: Function Foo { param( #???? ) } 我想这样称呼Foo: Foo -Bar "test" 在没有爆炸的情况下,我没有指定$bar参数。这可能吗?:) 更新: 我想让它起作用: Function IfFunctionExistsExecute { param ([parameter(Mandatory=$true)][string]$func, [parameter(Mandatory=$false)][str

考虑这一点:

Function Foo 
{
    param(
        #????
    )
}
我想这样称呼Foo:

Foo -Bar "test"
在没有爆炸的情况下,我没有指定$bar参数。这可能吗?:)

更新:

我想让它起作用:

Function IfFunctionExistsExecute
{
    param ([parameter(Mandatory=$true)][string]$func, [parameter(Mandatory=$false)][string]$args)
    begin 
    {
        # ...
    }
    process
    {
        if(Get-Command $func -ea SilentlyContinue)
        {
            & $func $args   # the amperersand invokes the function instead of just printing the variable
        }
        else
        {
            # ignore
        }       
    }
    end
    {
        # ...
    }
}


Function Foo
{
    param([string]$anotherParam)
    process 
    {
        $anotherParam
    }
}

IfFunctionExistsExecute Foo -Test "bar"
这给了我:

IfFunctionExistsExecute : A parameter cannot be found that matches parameter name 'Test'.
At C:\PSTests\Test.ps1:35 char:34
+ IfFunctionExistsExecute Foo -Test <<<<  "bar"
    + CategoryInfo          : InvalidArgument: (:) [IfFunctionExistsExecute], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,IfFunctionExistsExecute
IfFunctionExistsExecute:找不到与参数名称“Test”匹配的参数。
在C:\PSTests\Test.ps1:35 char:34

+如果FunctionExistsExecute Foo-Test您可以在函数中使用$args变量,它是传递给函数的参数数组,例如

function Foo()
{
    Write-Output $args[0];
    Write-Output $args[1];
}

Foo -Bar "test"
产出:

-Bar
test

我建议有两种选择

第一:您可能想考虑传递整个函数+它的参数作为Script块参数到您的函数…< /P> 或:使用剩余参数中的值:

function Test-SelfBound {
param (
    [Parameter(
        Mandatory = $true,
        HelpMessage = 'Help!'
    )]
    [string]$First,
    [Parameter(
        ValueFromRemainingArguments = $true
    )]
    [Object[]]$MyArgs
)

$Arguments = foreach ($Argument in $MyArgs) {
    if ($Argument -match '^-([a-z]+)$') {
        $Name = $Matches[1]
        $foreach.MoveNext() | Out-Null
        $Value = $foreach.Current
        New-Variable -Name $Name -Value $Value
        $PSBoundParameters.Add($Name,$Value) | Out-Null
    } else {
        $Argument
    }
}
    $PSBoundParameters | Out-Default
    "Positional"
    $Arguments

}

Test-SelfBound -First Test -Next Foo -Last Bar Alfa Beta Gamma

在本例中,我使用$MyArgs存储除必需参数“First”之外的所有内容。而一些简单的if会告诉我它是命名参数(-Next,-Last)还是位置参数(Alfa,Beta,Gamma)。通过这种方式,您可以同时拥有高级函数绑定(整体[Parameter()]装饰)的优点也为$args样式的参数留出空间。

不是包含传递到函数中的所有参数,$args将包含所有未绑定的参数。如果我调用
Test SelfBound-out foo
我得到一个错误:参数无法处理,因为参数名称“out”不明确。可能的匹配项包括:-OutVariable-exputffer。如果我调用
Test SelfBound-d foo
它并没有声明一个名为“d”的变量,而是一个名为“Debug”的变量-(我猜这会发生在所有人身上:-(True:函数定义使它成为高级函数(由
[Parameter()]
装饰触发),因此它将始终定义公共参数。-$Argument match'^-([\S]+)$”--会更好。我们只需要空白。最好的样本