在模块中使用时,函数未绑定 [英] Function does not get binding when used in Module

查看:80
本文介绍了在模块中使用时,函数未绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此功能:

function Test-Any {
    [CmdletBinding()]
    param($EvaluateCondition,
        [Parameter(ValueFromPipeline = $true)] $ObjectToTest)
    begin {
        $any = $false
    }
    process {
        if (-not $any -and (& $EvaluateCondition $ObjectToTest)) {
            $any = $true
        }
    }
    end {
        $any
    }
}

进入模块.我刚刚创建了一个新模块my-scripts.psm1文件,其中仅包含上述函数,并使用Import-Module <absolute path>导入.

into a module. I just created a new module, the my-scripts.psm1 file, which contains just the above function and import it with Import-Module <absolute path>.

问题是,如果我使用模块1..4 | Test-Any { $_ -gt 3 }中的函数,则返回false,因为$_未设置为管道中的值.

The problem is that if I use the function from the module 1..4 | Test-Any { $_ -gt 3 } returns false, because $_ is not set to the value from the pipe.

如果我在脚本中正常定义该函数并从那里使用它,则该函数将按预期工作(将$ _分配给整数值).

If I define the function normally in a script and use it from there it works as expected (with $_ getting assigned the integer values).

在Windows 7下使用PowerShell v4.0会发生这种情况.

This happens with PowerShell v4.0 under Windows 7.

推荐答案

该命令:& $EvaluateCondition $ObjectToTest —不与$_绑定任何内容.如果ScriptBlock中没有param()块,则$ObjectToTest的值将绑定到$args[0].

That command: & $EvaluateCondition $ObjectToTest — does not bind anything to $_. In absence of a param() block in ScriptBlock, the value of $ObjectToTest will be bound to $args[0].

$SB = {"`$_: '$_'; `$args[0]:'$($args[0])'"}
1..3 | ForEach-Object {& $SB ($_+3)}

输出:

$_: '1'; $args[0]:'4'
$_: '2'; $args[0]:'5'
$_: '3'; $args[0]:'6'

为什么引用$_起作用:您只需从父作用域引用$_变量即可.

Why does referencing $_ work: you simply reference the $_ variable from the parent scope.

您看到的$_的值是传递给Test-Any函数的当前管道输入对象.

The value of $_ that you see, is a current pipeline input object, passed to the Test-Any function.

function Test-Any {
    param($EvaluateCondition)
    process {
        "Test-Any `$_: '$_'"
        & $EvaluateCondition
    }
}
1..2 | %{3..4 | Test-Any {"EvaluateCondition `$_:'$_'"}}

输出:

Test-Any $_: '3'
EvaluateCondition $_:'3'
Test-Any $_: '4'
EvaluateCondition $_:'4'
Test-Any $_: '3'
EvaluateCondition $_:'3'
Test-Any $_: '4'
EvaluateCondition $_:'4'

在模块作用域中定义Test-Any时,带有Test-Any管道输入的变量$_也已在该模块作用域中定义,并且在该范围之外不可用.

When you define Test-Any in module scope, then variable $_ with pipeline input to Test-Any also got defined in that module scope and was not available outside of it.

New-Module {
    function Test-Any {
        param($EvaluateCondition)
        process {
            "Test-Any `$_: '$_'"
            & $EvaluateCondition
        }
    }
} | Out-Null
1..2 | %{3..4 | Test-Any {"EvaluateCondition `$_:'$_'"}}

输出:

Test-Any $_: '3'
EvaluateCondition $_:'1'
Test-Any $_: '4'
EvaluateCondition $_:'1'
Test-Any $_: '3'
EvaluateCondition $_:'2'
Test-Any $_: '4'
EvaluateCondition $_:'2'

如果要调用绑定了$_的某些值的脚本块,则执行此操作的一种方法是:

If you want to invoke a script block with some value bound to $_, then one way to do this would be:

ForEach-Object $EvaluateCondition -InputObject $ObjectToTest

这篇关于在模块中使用时,函数未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆