首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >"HasMoreData“即使在收到后也是正确的-作业

"HasMoreData“即使在收到后也是正确的-作业
EN

Stack Overflow用户
提问于 2012-06-07 14:21:00
回答 2查看 8K关注 0票数 7

我在Powershell中创建了一个简单的后台作业:

代码语言:javascript
运行
复制
Start-Job {"Hello"}

我问过Get-Job:

代码语言:javascript
运行
复制
    Id        Name         State         HasMoreData      Location       Command
    --        ----         -----         -----------      --------       -------
    1         Job1         Completed     True             localhost      "Hello"

接下来,我只需接收输出,然后再次运行Get

代码语言:javascript
运行
复制
Receive-Job 1
Get-Job

我可以看到"HasMoreData“现在是假的,因为我没有指定-keep参数。

但是,似乎每当我开始一个作业,而不是使用Start-JobInvoke-Command时,这个"HasMoreData“参数就不会更改为False。

示例:

代码语言:javascript
运行
复制
Get-WMIObject win32_bios -AsJob
Test-Connection . -AsJob

我是否可以绕过这种(错误)行为,使属性HasMoreData切换为False,除非我指定了-keep

谢谢!

更新:它似乎适用于使用-AsJob参数进行的所有调用。如果你跑

代码语言:javascript
运行
复制
Start-Job {Test-Connection .}

它可以工作("HasMoreData“在Receive-Job之后变为False ),但是

代码语言:javascript
运行
复制
Test-Connection . -AsJob

不会的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-18 00:57:56

简短答覆:

这是PowerShell 2.0中的一个bug。

这对布莱恩来说很好,因为他使用的是PowerShell 3,我会把钱放在上面。

较长的答覆:

启动-作业 cmdlet和-AsJob开关的工作方式不同。文档通常解释说, start -的目的是在本地运行后台作业,而-AsJob则使用在远程计算机上运行但在本地创建作业对象的命令来启动作业。虽然这通常是正确的,但是-AsJob也可以在本地运行作业,根据命令的不同,有时甚至无法在远程计算机上运行该命令。例如,Get-WMIObject使用-AsJob调用,-ComputerName在指定的远程计算机上运行命令,而使用-AsJob-Computername调用的Test-Connection在本地运行命令并调用指定的计算机。

我还看到了说明Start-作业由本地IPC工作的文档,而-AsJob连接到指定计算机的WinRM服务,即使它是本地主机,并且必须在本地和目标计算机上启用PSRemoting。再说一次,这并不是那么简单。我发现我可以在本地主机上使用-AsJob开关运行作业,WinRM和PSRemoting都是禁用的。

无论如何,PowerShell作为两个JobTypes、PSWmiJob或PSRemotingJob之一启动作业。这是违反直觉的,因为本地运行后台作业的Start-作业总是会创建一个PSRemotingJob,而-AsJob通常会创建一个PSWmiJob,除非它与总是在远程计算机或本地主机上调用命令的PSRemoting作业一起使用。

看看下面的会议记录,其中我以不同的方式创造了工作机会。我用三个命令进行了测试:Get-WMIObject,(当使用-AsJob调用时在远程计算机上运行)和ComputerNameTest-Connection,(在使用-AsJob (-ComputerName调用时总是在本地运行),它指定要ping的计算机,而不是运行命令的位置);Get-ChildItem,没有-AsJob参数。我在远程计算机和本地计算机上都使用Start-Invoke-命令-AsJob和本机-AsJob开关(对于具有此功能的命令)启动作业。

每个命令末尾的| %{$_.Name = '<the command preceding the pipe symbol>'}的目的是将每个作业命名为创建它的命令,因此在输出中更容易看到与每个命令对应的作业。它对作业的操作没有影响,它只是在创建作业后立即将每个作业重命名为一个更有意义的名称。

您将看到的是,在接收到所有作业(rcjb * 2>&1|Out-Null一次都接收到它们并取消输出)之后,不管PSRemotingJob对象是由Start创建的--作业还是-AsJob创建的,PSRemotingJob对象的HasMoreData属性都被设置为False,但是PSWmiJob对象的HasMoreData属性仍然是True。除了我在这里复制的例子之外,我还发现这是一贯正确的。

代码语言:javascript
运行
复制
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»

底线: bug在PSWmiJob对象中。不管作业是以何种方式创建的,也不管命令是在本地还是远程运行,在接收-作业之后,如果JobType是PSRemotingJob,则HasMoreData属性设置为False,但如果JobType是PSWmiJob,则保持为True。

据我所知,没有办法将HasMoreData设置为PSWmiJob上的False。Stop-作业不会执行它,重新启动WinRM不会执行它,并且属性是只读的。

票数 4
EN

Stack Overflow用户

发布于 2013-07-16 16:57:37

请参阅此输出:

代码语言:javascript
运行
复制
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是toplevel作业,它有一个名为Job3的子作业。这就是实际行动发生的地方。

您能否接收子作业并检查是否仍然设置了HasMoreData?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10933732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档