在数组列表中为运行空间注册 ObjectEvent
Register-ObjectEvent for runspace in an arraylist
我正在创建一个运行空间池,它将有多个运行空间在不同时间开始停止。
为了跟踪这一点,我所有的运行空间都进入了一个集合。
我正在尝试为每个对象注册一个对象事件,以便我可以 return 从运行空间到 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
}
}
代码在 register-objectevent 行上失败,目前我硬编码了 0 用于测试,但这将是最终版本中的当前运行空间编号。我不知道我是否犯了一个小错误,或者对它的工作原理缺乏根本的了解,我对这两种可能性都持开放态度!
我看到了一些问题,但主要是 StateChanged 事件是 RunspaceCollection[index] 内 RunspacePool.Powershell 对象上的事件,而不是 RunspaceCollection.Runspace 上的事件(自定义 "Runspace" 属性 您创建的 PSObject)。
在 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
在那之后,我认为您不打算在 "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
}
我正在创建一个运行空间池,它将有多个运行空间在不同时间开始停止。
为了跟踪这一点,我所有的运行空间都进入了一个集合。
我正在尝试为每个对象注册一个对象事件,以便我可以 return 从运行空间到 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
}
}
代码在 register-objectevent 行上失败,目前我硬编码了 0 用于测试,但这将是最终版本中的当前运行空间编号。我不知道我是否犯了一个小错误,或者对它的工作原理缺乏根本的了解,我对这两种可能性都持开放态度!
我看到了一些问题,但主要是 StateChanged 事件是 RunspaceCollection[index] 内 RunspacePool.Powershell 对象上的事件,而不是 RunspaceCollection.Runspace 上的事件(自定义 "Runspace" 属性 您创建的 PSObject)。
在 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
在那之后,我认为您不打算在 "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
}