用于arraylist

时间:2017-05-16 09:11:53

标签: 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用于测试,但这将是最终版本中的当前运行空间编号。我不知道我是否犯了一个小错误,或者根本不了解它是如何工作的,我对两种可能性持开放态度。

1 个答案:

答案 0 :(得分:1)

我看到一些问题,但主要是StateChanged事件是RunspaceCollection [索引]中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

之后,我认为您不打算在“Action”功能中使用Start-Password重置功能。以下是适用于我的完整代码:

$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
}