gpt4 book ai didi

powershell - 在数组列表中为运行空间注册 ObjectEvent

转载 作者:行者123 更新时间:2023-12-02 03:03:55 31 4
gpt4 key购买 nike

我正在创建一个运行空间池,它将有多个运行空间在不同时间开始停止。

为了跟踪这一点,我所有的运行空间都放在一个集合中。

我正在尝试为每个对象注册一个对象事件,以便我可以将信息从运行空间返回给 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 上的事件(您拥有的 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
}

关于powershell - 在数组列表中为运行空间注册 ObjectEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43997112/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com