not getting output from receive job

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:26:00

问题


The variable $var is blank when I run this script:

function FOO { write-output "HEY" }

$var = Start-Job -ScriptBlock { ${function:FOO} } | Wait-Job | Receive-Job

$var

How do I get output from receive-job?


回答1:


Start-Job spawns a new PowerShell instance in the background and as such has no knowledge of your function FOO which is defined in your initial instance

There is an additional parameter InitializationScript which is called upfront executing your script block in the new instance which you can use to define FOO like so

$var = Start-Job -InitializationScript { function FOO { write-output "HEY" } } -ScriptBlock ...  

BTW: I guess you want to execute the function instead of getting the function object itself so you may want to change your script block to this

-ScriptBlock { FOO }  


来源:https://stackoverflow.com/questions/39136708/not-getting-output-from-receive-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!