$ui.BTN_Ping.Add_Click({ $ui.Grid_Output_Computer.Columns | ForEach { $_.CanUserSort = $True } $ui.Grid_Output_Computer.Dispatcher.Invoke('Normal', [action] { [void]$collection.observableCollection.clear() }) $ui.PB_Computers.Value = [int] "0" $ps = [powershell]::Create().AddScript({ Param ($Collection, $ui, $jobs) $RunspacePool = [runspacefactory]::CreateRunspacePool(1, 5) $ScriptBlock = { Param ($ui, $Collection, $server) $status = "" ; $t = "" ; $IP = "" ; $t = Test-Connection $server -count 1 $status = "OFFline" if ($t2) { $status = "ONline" } $IP = "N/A" if ($t) { $IP = $t.IPV4Address.ToString() } $Object = [pscustomobject][ordered]@{ Date = (Get-Date).ToString() ; Computername = $server ; IP = $IP ; Status = $status ; } Start-Sleep -Seconds 1 $ui.Grid_Output_Computer.Dispatcher.Invoke('Normal', [action] { [void]$collection.observableCollection.Add($Object) #$ui.Grid_Output_Computer.Columns | ForEach-Object { $_.CanUserSort = $True } $ui.PB_Computers.Value++ }) # <# ok only for 1 offline $ui.Grid_Output_Computer.Dispatcher.Invoke('Normal', [action] { $myRow = $ui.Grid_Output_Computer.ItemContainerGenerator.ContainerFromIndex( $ui.Grid_Output_Computer.Items.status.IndexOf("OFFline") ) $myRow.Background = ([System.Windows.Media.Brushes]::Red) }) ##> $ui.Grid_Output_Computer.Columns | ForEach-Object { $_.CanUserSort = $True } <# ok if 1 offline if ($ui.Grid_Output_Computer.Items.status.Contains("OFFline")) { $index = $ui.Grid_Output_Computer.Items.status.IndexOf("OFFline") $myRow = $ui.Grid_Output_Computer.ItemContainerGenerator.ContainerFromIndex($index) } $ui.Grid_Output_Computer.Dispatcher.Invoke('Normal',[action]{ $myRow.Background = ([System.Windows.Media.Brushes]::Red) }) #> <# ok only for 1 offline $off = @($ui.Grid_Output_Computer.Items.status.Contains("OFFline")) if ($off) { $ui.Grid_Output_Computer.Items.status.ForEach("OFFline") $index = $ui.Grid_Output_Computer.Items.status.IndexOf("OFFline") $myRow = $ui.Grid_Output_Computer.ItemContainerGenerator.ContainerFromIndex($index) $ui.Grid_Output_Computer.Dispatcher.Invoke('Normal',[action]{ $myRow.Background = ([System.Windows.Media.Brushes]::Red) #$myRow.Background = ([System.Windows.Media.Brushes]::Green) }) } #> } $servers = ($ui.LV_Selected_Computers.Items).name ForEach ($server in $servers) { #$throttle = [int] "3" # 1..$throttle | foreach { $ps = [powershell]::Create() $ps.RunspacePool = $RunspacePool $RunspacePool.Open() [void]$ps.AddScript($ScriptBlock).AddArgument($ui).AddArgument($Collection).AddArgument($server) [void]$jobs.Add([pscustomobject]@{ PowerShell = $Ps Handle = $PS.BeginInvoke() }) } }).AddArgument($Collection).AddArgument($ui).AddArgument($jobs).AddArgument($server) [System.Threading.Monitor]::Enter($Jobs.syncroot) $jobs.Add([pscustomobject]@{ PowerShell = $ps Handle = $Ps.BeginInvoke() }) [System.Threading.Monitor]::Exit($Jobs.syncroot) })