首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数在ScriptBlock中不可访问

函数在ScriptBlock中不可访问
EN

Stack Overflow用户
提问于 2016-08-28 10:41:20
回答 2查看 1.8K关注 0票数 5

我有一个脚本,它有一些函数,然后在使用这些函数的同一个脚本中有多个作业。当我开始一份新的工作时,他们似乎无法在我的工作的[ScriptBlock]中找到。

下面是一个很小的例子,说明了这一点:

代码语言:javascript
复制
# A simple test function
function Test([string] $string)
{
    Write-Output "I'm a $string"
}

# My test job
[ScriptBlock] $test =
{
    Test "test function"
}

# Start the test job
Start-Job -ScriptBlock $test -Name "Test" | Out-Null

# Wait for jobs to complete and print their output
@(Get-Job).ForEach({
    Wait-Job -Job $_ |Out-Null
    Receive-Job -Job $_ | Write-Host
})

# Remove the completed jobs
Remove-Job -State Completed

我在PowerShell ISE中遇到的错误是:

代码语言:javascript
复制
The term 'Test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Test:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : localhost
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-28 12:52:26

Start-Job在单独的PowerShell进程中运行作业。这样,作业就不能访问调用PowerShell会话的会话状态。您需要在每个作业中定义函数,这些函数被作业使用。在不重复代码的情况下,一种简单的方法是使用-InitializationScript参数,其中可以定义所有公共函数。

代码语言:javascript
复制
$IS = {
    function CommonFunction1 {
        'Do something'
    }
    function CommonFunction2 {
        'Do something else'
    }
}
$SB1 = {
    CommonFunction1
    CommonFunction2
}
$SB2 = {
    CommonFunction2
    CommonFunction1
}
$Job1 = Start-Job -InitializationScript $IS -ScriptBlock $SB1
$Job2 = Start-Job -InitializationScript $IS -ScriptBlock $SB2
Receive-Job $Job1,$Job2 -Wait -AutoRemoveJob
票数 6
EN

Stack Overflow用户

发布于 2016-08-28 15:00:47

只要扩展PetSerAl的答案,就可以使用Runspace,如果您想要更快的代码和更有组织性的代码的话。查看以下问题:39180266

因此,当您在不同的运行空间中运行某项时,您需要在这两个运行空间中导入函数。因此,完成的结构看起来应该是:

  1. Module: functions.ps1 -您在这里存储函数以便与两个作用域共享。
  2. Main script: script.ps1 -它基本上是您的脚本,有运行空间,但是没有来自functions.ps1的函数。

在script.ps1的开头,只需调用Import-module .\functions.ps1,即可访问您的函数。请记住,runscape有不同的作用域,并且在它们的scriptblock中,您必须再次调用导入模块。完整的例子:

代码语言:javascript
复制
#file functions.ps1
function add($inp) {
     return $inp + 2
}


#file script.ps1
Import-module .\functions.ps1        #or you can use "dot call": . .\function.ps1
Import-module .\invoke-parallel.ps1  #it's extern module
$argument = 10                       #it may be any object, even your custom class

$results = $argument | Invoke-Parallel -ScriptBlock {
    import-module .\functions.ps1    #you may have to use here absolute path, because in a new runspace PSScriptRoot may be different/undefined
    return (add $_)                  # $_ is simply passed object from "parent" scope, in fact, the relationship between scopes is not child-parent
}

echo $result # it's 12
echo (add 5) # it's 7
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39190435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档