如何在scriptblock外调用函数 [英] How to call function outside a scriptblock

查看:254
本文介绍了如何在scriptblock外调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,我应该可以从我的powershell脚本中的任何地方调用。

I have a function that I should be able to call from any place in my powershell script.

问题在于它没有标识脚本块中的函数。

The problem is that it doesn't identify the function in a script block.

在下面的例子中,我有函数 getNumberFive ,它应该返回数字5.

我希望能够在脚本块中使用这个函数,当我开始一个新的工作时,也是在脚本的末尾。

In the following example I have the function getNumberFive which should return the number 5.
I want to be able to use this function inside a scriptblock when I start a new job and also in the end of the script.

预期结果:

将数字 15 写入文件C:\\ \\ tmp\result.txt

写入控制台:我喜欢数字5

Expected result:
Write the number 15 to the file "C:\tmp\result.txt"
Write to the console: "I like the number 5"

实际上: strong>

将数字 10 写入文件C:\ tmp\result.txt

写入控制台:我喜欢数字5

In reality:
Write the number 10 to the file "C:\tmp\result.txt"
Write to the console: "I like the number 5"

我可以通过在scriptblock中定义相同的函数来解决这个问题,但是我会复制函数,这不是一个好的编程。

I can workaround this issue by defining the same function inside the scriptblock but then I will duplicate the function and this is not a good programming.

另一种方法是定义:

Another way is to define:

$func = {
    function getNumberFive(){
        return 5
    }
}
$scriptBlock = {

    Function printSum(){
        $number = getNumberFive
        $newNumber = 10 + $number  # => $newNumber should be 15
        $newNumber >> "C:\tmp\result.txt"
      }
}
Start-Job -ScriptBlock $scriptBlock -InitializationScript $func

但是在这种情况下,我将无法调用 $ five = getNumberFive

But in this case I won't be able to call $five = getNumberFive.

我读了很多方法,但我不明白如何使用它们:

调用POWERSHELL FUNCTION IN A START-作业脚本BLOCK时,它的定义。在相同的脚本

I read number of methods but I didn't understand how exactly to use them:
CALLING A POWERSHELL FUNCTION IN A START-JOB SCRIPT BLOCK WHEN IT’S DEFINED IN THE SAME SCRIPT

如何传递一个命名函数作为参数(scriptblock)

htt PS://social.technet.microsoft.com/Forums/ie/en-US/485df2df-1577-4770-9db9-a9c5627dd04a/how-to-pass-a-function-to-a-scriptblock论坛= winserverpowershell

PowerShell:传递函数作为参数

在带参数的函数上使用Invoke-Command -ScriptBlock

我的脚本:

function getNumberFive(){
    return 5
}

$scriptBlock = {

    Function printSum(){
        # $number = getNumberFive => DOESN'T WORK
        # $number = Invoke-Expression ($(get-command getNumberFive) | Select -ExpandProperty Definition) => DOESN'T WORK AS EXPECTED
        # $number = &(${function:getNumberFive}) => DOESN'T WORK AS EXPECTED
        # $number = &(Get-Item function:getNumberFive) => DOESN'T WORK AS EXPECTED
        $newNumber = 10 + $number  # => $newNumber should be 15
        $newNumber >> "C:\tmp\result.txt"
    }

    printSum
}

Start-Job -ScriptBlock $scriptBlock 

$five = getNumberFive
Write-Host "I like the number"$five

Get-Job | Wait-Job
Get-Job | Stop-Job
Get-Job | Remove-Job


推荐答案

当您将脚本块传递给 start-job (或 invoke-expression )执行该脚本块的PowerShell实例只能访问该脚本块,块加载以及PowerShell实例中已存在的任何内容。

When you pass a scriptblock to start-job (or invoke-expression) the PowerShell instance that executes that scriptblock only has access to that scriptblock, anything that script block loads, and anything that already exists in the PowerShell instance.

脚本的其他部分不包含在内。 (对于在脚本中本地定义的函数,可以从PowerShell的其他可能是远程的实例中获得,整个脚本 - 不仅仅是脚本块 - 以及任何依赖关系都需要可以从其他实例访问)。

Other parts of your script are not included. (For functions locally defined in your script to be available from other, possibly remote, instances of PowerShell the whole script – not just the scriptblock – and any dependencies would need to be accessible from the other instance.)

您可以在两个位置将您想要的代码重构为脚本块加载的模块以及作业创建脚本。

You could refactor the code you want in both places into a module which the script block loads as well as the job creating script.

当使用在另一个进程中执行代码的作业:就像任何远程操作一样,远程执行是一个单独的环境。

When using jobs you are executing code in another process: like any remote operation the remote executing is a separate environment.

这篇关于如何在scriptblock外调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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