我在Powershell中创建了一个简单的后台作业:
Start-Job {"Hello"}
我查看了Get-Job:
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
1 Job1 Completed True localhost "Hello"
接下来,我只是收到输出,然后再次运行Get-Job
Receive-Job 1
Get-Job
我可以看到“HasMoreData”现在是假的,因为我没有指定-keep
参数。
但是:似乎每当我开始工作时,不是使用Start-Job
或Invoke-Command
,这个“HasMoreData”参数不会变为False。
例子:
Get-WMIObject win32_bios -AsJob
Test-Connection . -AsJob
我可以绕过这个(错误的)行为,以便属性HasMoreData切换到False,除非我指定-keep
吗?
谢谢!
更新:它似乎适用于使用-AsJob
参数进行的所有调用。如果你跑
Start-Job {Test-Connection .}
它的工作原理(“HasMoreData”在Receive-Job
之后变为False),但是
Test-Connection . -AsJob
才不是。
简短回答:
这是PowerShell 2.0中的一个错误。
它适用于Blaine,因为他使用的是PowerShell 3,我会把钱投入其中。
答案很长:
Start-Job cmdlet和-AsJob开关的工作方式不同。文档通常说明Start-Job旨在在本地运行后台作业,而-AsJob旨在使用在远程计算机上运行但在本地创建作业对象的命令启动作业。虽然这通常是正确的,但是-AsJob也可以用于在本地运行作业,并且根据命令,它有时甚至不能在远程计算机上运行命令。例如,使用-AsJob和-ComputerName调用的Get-WMIObject在指定的远程计算机上运行该命令,而使用-AsJob和-Computername调用的Test-Connection在本地运行该命令并ping指定的计算机。
我还看到了解释Start-Job由本地IPC工作的文档,而-AsJob建立了与指定计算机的WinRM服务的连接,即使它是localhost,并且必须在本地和目标计算机上启用PSRemoting (S)。再说一次,这并不是那么简单。我发现我可以在本地主机上使用-AsJob开关运行作业,同时禁用WinRM和PSRemoting。
在任何情况下,PowerShell都会将作业作为两个JobTypes,PSWmiJob或PSRemotingJob之一启动。这是违反直觉的,因为在本地运行后台作业的Start-Job总是创建一个PSRemotingJob,而-AsJob通常创建一个PSWmiJob,除非它与Invoke-Command一起使用,它始终启动PSRemoting作业,无论命令是否在远程计算机或localhost上调用。
看看下面的会话记录,其中我以不同的方式创建了工作。我测试了三个命令:Get-WMIObject,当使用-AsJob和ComputerName调用时,它在远程计算机上运行;测试连接,在使用-AsJob调用时始终在本地运行(-ComputerName指定要ping的计算机,而不是运行命令的位置);和Get-ChildItem,它没有-AsJob参数。我在远程计算机和本地计算机上使用Start-Job,Invoke-Command -AsJob和本机-AsJob开关(对于拥有它的命令)为每个人启动了作业。
每个命令末尾的| %{$_.Name = '<the command preceding the pipe symbol>'}
的目的是将每个作业命名为创建它的命令,因此在输出中更容易看到哪个作业对应于每个命令。它对作业的操作没有影响,它只是在创建后立即将每个作业重命名为更有意义的名称。
您将看到的是,在收到所有作业后(rcjb * 2>&1|Out-Null
同时接收所有作业并禁止输出),PSRemotingJob对象的HasMoreData属性设置为False,无论它们是由Start-Job还是-AsJob创建的,而是HasMoreData PSWmiJob对象的属性保持为True。除了我在这里复制的例子之外,我发现这一点始终如一。
07-17-13 19:44:56.30 C:\Users\ainbar» Invoke-Command -ComputerName . -ScriptBlock {Get-WMIObject win32_bios} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName . -ScriptBlock {Get-WMIObject win32_bios} -AsJob'}
07-17-13 19:44:56.43 C:\Users\ainbar» Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-WMIObject win32_bios} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-WMIObject win32_bios} -AsJob'}
07-17-13 19:44:56.46 C:\Users\ainbar» Start-Job -ScriptBlock {Test-Connection .} | %{$_.Name = 'Start-Job -ScriptBlock {Test-Connection .}'}
07-17-13 19:44:57.13 C:\Users\ainbar» Test-Connection . -AsJob | %{$_.Name = 'Test-Connection . -AsJob '}
07-17-13 19:44:57.14 C:\Users\ainbar» Invoke-Command -ComputerName . -ScriptBlock {Test-Connection .} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName . -ScriptBlock {Test-Connection .}'}
07-17-13 19:44:57.18 C:\Users\ainbar» Invoke-Command -ComputerName ai8460p -ScriptBlock {Test-Connection .} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName ai8460p -ScriptBlock {Test-Connection .} -AsJob'}
07-17-13 19:44:57.20 C:\Users\ainbar» Start-Job -ScriptBlock {Get-ChildItem C:\} | %{$_.Name = 'Start-Job -ScriptBlock {Get-ChildItem C:\}'}
07-17-13 19:44:57.80 C:\Users\ainbar» Invoke-Command -ComputerName . -ScriptBlock {Get-ChildItem C:\} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName . -ScriptBlock {Get-ChildItem C:\} -AsJob'}
07-17-13 19:44:57.82 C:\Users\ainbar» Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-ChildItem C:\} -AsJob | %{$_.Name = 'Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-ChildItem C:\} -AsJob'}
07-17-13 19:44:57.84 C:\Users\ainbar» $fmt_gjb = 'Id','Name','Location',@{l="JobType";e={$_.GetType().name}},@{l='HasMoreData';e={"$($_.HasMoreData)"}},'State','Command'
07-17-13 19:46:21.36 C:\Users\ainbar» gjb|ft -a $fmt_gjb
Id Name Location JobType HasMoreData State Command
-- ---- -------- ------- ----------- ----- -------
1 Start-Job -ScriptBlock {Get-WMIObject win32_bios} localhost PSRemotingJob True Completed Get-WMIObject win32_bios
3 Get-WMIObject win32_bios -AsJob localhost PSWmiJob True Completed Get-WMIObject
5 Get-WMIObject win32_bios -AsJob -ComputerName ai8460p ai8460p PSWmiJob True Completed Get-WMIObject
7 Invoke-Command -ComputerName . -ScriptBlock {Get-WMIObject win32_bios} -AsJob localhost PSRemotingJob True Completed Get-WMIObject win32_bios
9 Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-WMIObject win32_bios} -AsJob ai8460p PSRemotingJob True Completed Get-WMIObject win32_bios
11 Start-Job -ScriptBlock {Test-Connection .} localhost PSRemotingJob True Completed Test-Connection .
13 Test-Connection . -AsJob . PSWmiJob True Completed Test-Connection
15 Invoke-Command -ComputerName . -ScriptBlock {Test-Connection .} localhost PSRemotingJob True Completed Test-Connection .
17 Invoke-Command -ComputerName ai8460p -ScriptBlock {Test-Connection .} -AsJob ai8460p PSRemotingJob True Completed Test-Connection .
19 Start-Job -ScriptBlock {Get-ChildItem C:\} localhost PSRemotingJob True Completed Get-ChildItem C:\
21 Invoke-Command -ComputerName . -ScriptBlock {Get-ChildItem C:\} -AsJob localhost PSRemotingJob True Completed Get-ChildItem C:\
23 Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-ChildItem C:\} -AsJob ai8460p PSRemotingJob True Completed Get-ChildItem C:\
07-17-13 19:46:37.94 C:\Users\ainbar» rcjb * 2>&1|Out-Null
07-17-13 19:47:14.52 C:\Users\ainbar» gjb|ft -a $fmt_gjb
Id Name Location JobType HasMoreData State Command
-- ---- -------- ------- ----------- ----- -------
1 Start-Job -ScriptBlock {Get-WMIObject win32_bios} localhost PSRemotingJob False Completed Get-WMIObject win32_bios
3 Get-WMIObject win32_bios -AsJob localhost PSWmiJob True Completed Get-WMIObject
5 Get-WMIObject win32_bios -AsJob -ComputerName ai8460p ai8460p PSWmiJob True Completed Get-WMIObject
7 Invoke-Command -ComputerName . -ScriptBlock {Get-WMIObject win32_bios} -AsJob localhost PSRemotingJob False Completed Get-WMIObject win32_bios
9 Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-WMIObject win32_bios} -AsJob ai8460p PSRemotingJob False Completed Get-WMIObject win32_bios
11 Start-Job -ScriptBlock {Test-Connection .} localhost PSRemotingJob False Completed Test-Connection .
13 Test-Connection . -AsJob . PSWmiJob True Completed Test-Connection
15 Invoke-Command -ComputerName . -ScriptBlock {Test-Connection .} localhost PSRemotingJob False Completed Test-Connection .
17 Invoke-Command -ComputerName ai8460p -ScriptBlock {Test-Connection .} -AsJob ai8460p PSRemotingJob False Completed Test-Connection .
19 Start-Job -ScriptBlock {Get-ChildItem C:\} localhost PSRemotingJob False Completed Get-ChildItem C:\
21 Invoke-Command -ComputerName . -ScriptBlock {Get-ChildItem C:\} -AsJob localhost PSRemotingJob False Completed Get-ChildItem C:\
23 Invoke-Command -ComputerName ai8460p -ScriptBlock {Get-ChildItem C:\} -AsJob ai8460p PSRemotingJob False Completed Get-ChildItem C:\
07-17-13 19:47:35.29 C:\Users\ainbar»
底线:错误在PSWmiJob对象中。无论创建作业的方式如何,无论命令是本地还是远程运行,在Receive-Job之后,如果JobType是PSRemotingJob,则HasMoreData属性设置为False,但如果JobType是PSWmiJob,则保持为True。
据我所知,没有办法在PSWmiJob上将HasMoreData设置为False。 Stop-Job不会这样做,重新启动WinRM将不会这样做,并且该属性是只读的。
看到这个输出:
PS C:\dell> Test-Connection . -AsJob
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 Job2 WmiJob Running True . Test-Connection
PS C:\dell> Get-Job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 Job2 WmiJob Completed True . Test-Connection
PS C:\dell> Get-Job Job2 | fl
StatusMessage :
HasMoreData : True
Location : .
Command : Test-Connection
JobStateInfo : Completed
Finished : System.Threading.ManualResetEvent
InstanceId : d16afbe0-31f7-4189-8d2a-30ede40645c4
Id : 2
Name : Job2
ChildJobs : {Job3}
PSBeginTime : 7/16/2013 10:22:58 PM
PSEndTime : 7/16/2013 10:22:58 PM
PSJobTypeName : WmiJob
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
State : Completed
PS C:\dell> Get-Job Job3
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
3 Job3 Completed True .
PS C:\dell> Get-Job Job3 | Receive-Job
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
W4-G9W... localhost 127.0.0.1 32 0
W4-G9W... localhost 127.0.0.1 32 0
W4-G9W... localhost 127.0.0.1 32 0
W4-G9W... localhost 127.0.0.1 32 0
PS C:\dell> Get-Job Job2 | fl
StatusMessage :
HasMoreData : False
Location : .
Command : Test-Connection
JobStateInfo : Completed
Finished : System.Threading.ManualResetEvent
InstanceId : d16afbe0-31f7-4189-8d2a-30ede40645c4
Id : 2
Name : Job2
ChildJobs : {Job3}
PSBeginTime : 7/16/2013 10:22:58 PM
PSEndTime : 7/16/2013 10:22:58 PM
PSJobTypeName : WmiJob
Output : {}
Error : {}
Progress : {}
Verbose : {}
Debug : {}
Warning : {}
State : Completed
PS C:\dell> Get-Job Job3
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
3 Job3 Completed False .
您会看到Job2是顶级作业,它具有使用名称Job3创建的子作业。这就是实际行动发生的地方。
您是否可以收到子作业并检查HasMoreData是否仍然设置?