2017-05-16 19 views
3

我正在创建一个运行空间池,该池将有多个运行空间在不同的时间开始停止。阵列列表中的运行空间的Register-ObjectEvent

为了跟踪这个我所有的运行空间进入一个集合。

我正在尝试为每个对象注册一个对象事件,以便我可以从GUI中向用户返回运行空间的信息。

在那里我有问题的部分是在这里:

$Global:RunspaceCollection 
$Global:x = [Hashtable]::Synchronized(@{}) 

$RunspaceCollection = @() 
[Collections.Arraylist]$Results = @() 

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,5) 
$RunspacePool.Open() 

$action = { 
    Foreach($Runspace in $Global:RunspaceCollection.ToArray()) { 
     If ($Runspace.runspace.iscompleted){ 
      $results.add($runspace.powershell.endinvoke($Runspace.runspace)) 

      $runspace.powershell.dispose() 
      $runspacecollection.remove($Runspace) 
      Write-Host "I AM WORKING" 
      test-return 

    } 
} 
Function Start-PasswordReset($username) { 

    $scriptblock = { 
    Param($userame) 
    start-sleep -Seconds 10 
    $Result = "I have reset" + $username 
    $result 
} 

$Powershell = [PowerShell]::Create().AddScript($ScriptBlock).AddArgument($username) 
$Powershell.RunspacePool = $RunspacePool 

[Collections.Arraylist]$Global:RunspaceCollection += New-Object -TypeName PSObject -Property @{ 
    Runspace = $PowerShell.BeginInvoke() 
    PowerShell = $PowerShell 

} 
Register-ObjectEvent -InputObject $runspacecollection[0].Runspace -EventName statechanged -Action $Action 
} 


} 

的代码在寄存器objectevent线失败,目前我已经硬编码0进行测试,但是这将是目前的运行空间数在最终版本中。我不知道我是否犯了一个小错误,或者对这个工作原理缺乏了解,我愿意接受这两种可能性!

+1

目前尚不清楚你试图实际完成什么。您发布的代码无法正常工作,它无法将New-Object添加到数组列表中,但这不能是完整的代码吗?注册对象事件需要一个对象发出的事件,所以在你的情况下,PSObject.Runspace应该有一个StateChanged事件。请提供更完整的代码清单或更详细的说明,说明您正在努力完成的任务。 –

+0

不,它不是完整的代码,我将在回到Tommorow时发布它。本质上,我期待存储在集合中的Runspace对象有一个statechanged事件,但是当我检查get-member时,它缺少。 – Lister

+0

@BenRichards我已经添加了很多。这只是一个测试函数,除了写入变量和shell之外,它实际上并没有做任何事情。一旦有效,我计划将它放在gui后面,以阻止长时间运行的任务的干扰,因为同时会有多个运行,可能需要2分钟才能完成。 – Lister

回答

0

我看到一些问题,但主要是StateChanged事件是RunspacePool.Powershell对象在RunspaceCollection [index]中的事件,而不是RunspaceCollection.Runspace(PSObject的自定义“Runspace”属性已经创造出来了)。

设置你的PowerShell的对象上运行空间为:

$powershell = [PowerShell]::Create() 
$powershell.RunspacePool = $runspacePool 
$powershell.AddScript($scriptBlock).AddArgument($username) 
$Global:runspaceCollection += @{ 
    PowerShell = $powershell 
    Handle  = $powershell.BeginInvoke() 
} 

然后声明您的活动为:

Register-ObjectEvent -InputObject $Global:runspaceCollection[0].PowerShell.RunspacePool -EventName StateChanged -Action $Action 

在那之后,我不认为你打算有开始 - “动作”功能中的密码重置功能。这里是完整的代码,适用于我:

$Global:RunspaceCollection 
$Global:x = [Hashtable]::Synchronized(@{}) 

$RunspaceCollection = @() 
[Collections.Arraylist]$Results = @() 

$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, 5) 
$RunspacePool.Open() 

$action = { 
    Foreach ($Runspace in $Global:RunspaceCollection.ToArray()) { 
     If ($Runspace.runspace.iscompleted) { 
      $results.add($runspace.powershell.endinvoke($Runspace.runspace)) 

      $runspace.powershell.dispose() 
      $runspacecollection.remove($Runspace) 
      Write-Host "I AM WORKING" 
      test-return 
     } 
    } 
} 

Function Start-PasswordReset($username) { 
    $scriptblock = { 
     Param($userame) 
     start-sleep -Seconds 10 
     $Result = "I have reset" + $username 
     $result 
    } 

    $powershell = [PowerShell]::Create() 
    $powershell.RunspacePool = $runspacePool 
    $powershell.AddScript($scriptBlock).AddArgument($username) 
    $Global:runspaceCollection += @{ 
     PowerShell = $powershell 
     Handle  = $powershell.BeginInvoke() 
    } 

    Register-ObjectEvent -InputObject $Global:runspaceCollection[0].PowerShell.RunspacePool -EventName StateChanged -Action $Action 
} 
+0

非常感谢您的反馈,我会告诉你一个明天,让你知道我是怎么回事。 – Lister

+0

当然可以!我不知道你可以做你正在做的事,我也学到了一些东西! –

+0

它接缝像没有人真的考虑过运行空间的标记结果,而不是线程锁定的方式,如果你运行一个GUI,非常讨厌!我只在过去的一周了解过运行空间,但是一旦我找到了它们,它们就有可能让我的生活变得更加轻松。 – Lister