无法重复接收作业

时间:2015-05-28 06:00:30

标签: powershell jobs

我可以在ScheduledJob中使用Start-Job吗?

以下是故事:我使用来自System.Management.Automation,Remoting和Runspaces的.Net类从我的应用程序内部使用PowerShell Remoting。 我运行一个脚本,在远程主机上创建一个ScheduledJob,用Hyper-V做一些基本的东西,比方说,Restart-Vm。操作本身按预期执行,就像我在此远程主机上的控制台中运行它一样。

虽然有一点不同 - 如果我把它放到控制台,我会立即得到操作的结果。例如,如果VM处于禁用状态,我将收到“虚拟机处于当前状态时无法执行操作”。在使用SJ的情况下,当某个SJ触发器被激活并且SJ运行时,我不可能在收到Restart-Vm命令之后立即得到Restart-Vm命令的结果。相反,我必须检查在SJ被触发时创建的相应BackgroundJob并检查其状态并接收其结果。这不好,因为我的应用程序必须将此类事件写入远程计算机上的EventLog。即使Start-Job -ScriptBlock { params($guid) Get-Vm $guid | Restart-Vm -Force; } -ArgumentList $id; 操作没有成功,也不会抛出异常,我显然无法写入预定作业正文中的EventLog。

我认为解决方案可能是这样的:在预定作业中创建一个新作业,使此作业执行目标操作并将处理程序绑定到此作业的状态更改事件,该事件将写入EventLog。

但是,当我实现这个

HasMoreData

失败了。它是如何发生的:SJ被解雇,相应的Job被创建,我检查它的状态 - 它已完成并且它是Receive-Job。当我Receive-Job : Parameter set cannot be resolved using the specified named parameters. At line:1 char:8 + $res | receive-job + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (System.Manageme...n.PSRemotingJob:PSObject) [Receive-Job], ParameterBindingEx ception + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ReceiveJobCommand 时,我收到此错误

Restart-Vm

我尝试使用-AsJob切换运行Restart-Vm操作,传递凭据,但没有任何作用。

2015年6月5日更新

事实上,这个问题源于我需要捕获-ErrorAction Stop抛出的异常。直到最近我才意识到的事实是,在调用-ErrorVariable时我必须指定Restart-Vmvar yMax = 1.2; var svg1 = dimple.newSvg("body", 370, 230); var data = [{ "Brand":"A", "Day":"Mon", "SalesVolume":10 }, { "Brand":"B", "Day":"Mon", "SalesVolume":20 }]; var myChart = new dimple.chart(svg1, data); myChart.setBounds(120, 10, 170, 150) var x = myChart.addCategoryAxis("x", "Day"); var y = myChart.addPctAxis("y", "SalesVolume"); y.overrideMax = yMax; y.addOrderRule("SalesVolume"); var s = myChart.addSeries("Brand",dimple.plot.bar); s.getTooltipText = function (e) { var graphValue = (e.aggField[0]); return [ ""+graphValue+"" ]; }; s.barGap=0.7; myChart.draw(); var defs = svg1.append("defs"); defs.append("marker") .attr("id", "triangle-start") .attr("viewBox", "0 0 10 10") .attr("refX", 10) .attr("refY", 5) .attr("markerWidth", 10) .attr("markerHeight", 10) .attr("orient", "auto") .append("path") .attr("class", "marker") .attr("d", "M 0 0 L 10 5 L 0 10 z"); svg1.append("line") .attr("x1", 140) .attr("x2", 195) .attr('stroke','black') .attr("y1", y._scale(0.5)) .attr("y2", y._scale(0.5)) .attr("marker-start", "url(#triangle-start)"); 参数,以便我可以控制其执行。因此,不再需要像嵌套工作那样的任何黑客。

1 个答案:

答案 0 :(得分:1)

执行语句$res = Get-Job | Receive-Job$res的值是从作业收到的输出。该输出不是作业,可以再次输入Receive-Job

演示:

PS C:\> Start-Job -ScriptBlock { 'foo' }

Id  Name  PSJobTypeName  State    HasMoreData  Location   Command
--  ----  -------------  -----    -----------  --------   -------
6   Job6  BackgroundJob  Running  True         localhost  'foo'

PS C:\> $res = Get-Job | Receive-Job
PS C:\> $res
foo
PS C:\> $res | Receive-Job
Receive-Job : The input object cannot be bound to any parameters for the command
either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:8
+ $res | Receive-Job
+        ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (foo:PSObject) [Receive-Job], ...
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Comman...

如果您想多次接收,则需要保留作业对象(或其ID):

PS C:\> $job = Start-Job -ScriptBlock { 'foo' }
PS C:\> $job | Receive-Job
foo
PS C:\> $job | Receive-Job
PS C:\> Get-Job -Id $job.Id | Receive-Job
PS C:\> _
相关问题