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

标签 powershell runspace

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

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

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

相关文章:

windows - 更改硬盘唯一ID

c# - 在 C# 运行空间中的远程服务器上导入 Powershell 模块

events - powershell 多运行空间事件传递

sql - 自动刷新 Excel ODC 连接和数据透视表,无需打开文件 PowerShell

Powershell 5.# 添加类型加速器并在同一文件的类中使用

internet-explorer - 在 Powershell 中打开浏览器刚刚停止工作

html - 使用 HTML 表单作为 powershell 的 GUI

c# - 为什么无法为远程 RunspacePool 传递 InitialSessionState?

powershell - New-PSSession 并行执行