Start-job 与 Invoke-command -asjob
我正在尝试在 PowerShell 2.0 中执行基本后台作业,并且我在 start-job 和 invoke-command -asjob 中看到了不同的情况。
如果我这样做:
start-job -scriptblock {get-process}
我得到一个作业对象,但子作业(由 start-job 自动创建)的 JobStateInfo 始终为“NotStarted” 。
然而,这按预期工作:
invoke-command -scriptblock {get-process} -computer localhost -asjob
我已经运行了enable-psremoting....我需要做的任何其他事情后台工作工作吗?
I'm trying to do basic background jobs in PowerShell 2.0, and I'm seeing different things with start-job and invoke-command -asjob.
If I do this:
start-job -scriptblock {get-process}
I get a job object, but the child job (which is created automatically by start-job) always has a JobStateInfo of "NotStarted".
this, however, works as expected:
invoke-command -scriptblock {get-process} -computer localhost -asjob
I've run the enable-psremoting....anything else I need to do to get background jobs working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个使用 start-job 的示例不使用 HTTP 进行调用,而是使用带有 WinRM 的 IPC 通道来运行;这样就不需要管理权限。使用 invoke-command 的第二个示例确实需要管理员权限(默认情况下),并将通过 HTTP 和 WinRM 连接。
老实说,对于大多数人来说,我预计第二个会失败。如果您针对启动作业调用的 ID 运行:Receive-Job,您是否会收到任何错误消息?
-奥辛
The first example using start-job does not use HTTP for the call and instead uses an IPC channel with WinRM to run; it does not require administrative privileges this way. The second example with invoke-command does require admin rights (by default) and will connect via HTTP and WinRM.
To be honest, I would have expected the second one to fail for most people. If you run: Receive-Job against the ID of the start-job invocation, do you get any error messages?
-Oisin
要接收更新的 JobStateInfo,您需要使用 Get-Job 和 Start-Job 创建的作业。不过,如果您使用此信息来查看作业何时完成,则 Wait-Job 或 Receive-Job -wait 可能更适合您的需求。
Wait-Job 只是等待指示的作业或作业列表完成,然后再继续。 Receive-Job -wait 执行相同的操作,但它还收集作业的结果/输出。
To receive an updated JobStateInfo you'll need to use Get-Job and the job created by Start-Job. Though, if you're using this information to see when the job finishes, Wait-Job or Receive-Job -wait might be better suited to your needs.
Wait-Job simply waits until the job, or list of jobs, indicated is finished before moving on. Receive-Job -wait does the same thing, but it also gathers the results/output of the job.