函数无法在ScriptBlock中访问

时间:2016-08-28 10:41:21

标签: powershell asynchronous powershell-v5.0

我有一个脚本,它具有一些功能,然后在使用这些功能的同一个脚本中有多个作业。当我开始一份新工作时,他们似乎无法通过[ScriptBlock]访问我的工作。

这是一个证明这一点的最小例子:

# 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中遇到的错误是:

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

2 个答案:

答案 0 :(得分:4)

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

$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

答案 1 :(得分:0)

只是扩展PetSerAl的答案,你可以使用Runspaces,如果你想要更快的代码和更有条理的话。看看这个问题: 39180266

因此,当您在不同的运行空间中运行某些内容时,需要在这两个运行空间中导入函数。所以完成的结构看起来像:

  1. Module: functions.ps1 - 您在此处存储与两个范围共享的功能。
  2. Main script: script.ps1 - 它基本上是你的脚本,带有运行空间,但没有来自functions.ps1的函数。
  3. 在script.ps1的开头,只需调用Import-module .\functions.ps1即可访问您的函数。请记住,runscape具有不同的范围,并且在其scriptblock中,您必须再次调用import-module。完整的例子:

    #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