启动作业问题
为什么这段代码不起作用?
function teste
{
begin
{
function lala {
while ($true) {
"JJJJ" | Out-File c:\Testes\teste.txt -Append
}
}
}
process {
Start-Job -ScriptBlock {lala}
}
}
Why this code not works ?
function teste
{
begin
{
function lala {
while ($true) {
"JJJJ" | Out-File c:\Testes\teste.txt -Append
}
}
}
process {
Start-Job -ScriptBlock {lala}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最好的猜测是范围界定。当 Start-Job 运行脚本块时,它会在不同的上下文中运行它——其中未定义“lala”。但是,如果您要像这样重新表述代码:
后台作业不会尝试调用未定义的名称 - 相反,整个脚本块将传递给它并且事情应该可以正常工作。
请注意,我建议您像我一样不使用 while 循环进行测试,因为这会很快填满您的磁盘。
另外,在发布代码时,请以更有意义的函数和变量名称为目标。 :-)
My best guess is scoping. When Start-Job runs your script block, it runs it in a different context -- one where "lala" is not defined. However, if you were to rephrase your code like so:
the background job wouldn't try to invoke a name that isn't defined -- instead, the entire script block would be passed to it and things should work.
Note, that I recommend you test without the while loop like I did, because that's going to fill up your disk rather quickly.
Also, please aim for more meaningful function and variable names when posting code. :-)