How do I Start a job of a function i just defined?

后端 未结 7 810
臣服心动
臣服心动 2020-11-27 23:34

How do I Start a job of a function i just defined?

function FOO { write-host \"HEY\" } Start-Job -ScriptBlock { FOO } |
Receive-Job

Receive-Job: The term \'         


        
7条回答
  •  失恋的感觉
    2020-11-28 00:06

    As @Shay points out, FOO needs to be defined for the job. Another way to do this is to use the -InitializationScript parameter to prepare the session.

    For your example:

    $functions = {
        function FOO { write-host "HEY" }
    }
    
    Start-Job -InitializationScript $functions -ScriptBlock {FOO}|
        Wait-Job| Receive-Job
    

    This can be useful if you want to use the same functions for different jobs.

提交回复
热议问题