Runspace issue - Adding timeout to each runspace

Welcome Forums General PowerShell Q&A Runspace issue - Adding timeout to each runspace

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
9 months, 3 weeks ago.

  • Author
    Posts
  • #92228

    Participant
    Points: 17
    Rank: Member

    I have a script that runs various Active Directory-related tasks in parallel. It runs fine for the most part. However, when running on around 2,000 machines, it gets hung up near the end (I'm guessing due to max runspaces getting hung up). I have yet to find a straight-forward way of using a timer for each runspace. I have a way of exiting the runspace if the TOTAL time reaches a certain number, but not for each individual runspace. Can anyone suggest a way to keep track of how many runspaces are running at once and how to add a timeout for each runspace?

    Here is my timeout/stopwatch for the overall runspace time:

    $sw = [system.diagnostics.stopwatch]::startNew()
    
    While ($RunspaceCollection)
    {
    	Foreach ($Runspace in $RunspaceCollection.ToArray())
    	{
    		
    		If ($Runspace.Runspace.IsCompleted)
    		{
    			[void]$Results.Add($Runspace.PowerShell.EndInvoke($Runspace.Runspace))
    			$Runspace.PowerShell.Dispose()
    			$RunspaceCollection.Remove($Runspace)
    		} 
    	}
    	
    	Start-Sleep -Milliseconds 1000
    	If ($sw.Elapsed.TotalMinutes -gt 30) { break }
    }
    $sw.Stop()
    
  • #92275

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

    Take a look at this series and see if that gives you ideas. Specifically as to your...

    Can anyone suggest a way to keep track of how many runspaces are running at once

    ... See the info in the 4th bullet

    Beginning Use of PowerShell Runspaces

    https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/26/beginning-use-of-powershell-runspaces-part-1
    • Beginning Use of PowerShell Runspaces: Part 1 Begin use with runspaces.
    • Beginning Use of PowerShell Runspaces: Part 2 Begin use with runspaces.
    • Beginning Use of PowerShell Runspaces: Part 3 Use runspace pools for multithreading.
    • A Look at the PoshRSJob Module Learn about a module for working with runspaces.

    As for ..

    how to add a timeout for each runspace?

    See this post... 'powershell.org/forums/topic/timeout-for-runspace'
    and see...
    Runspace​Connection​Info Class
    'docs.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspaceconnectioninfo?view=powershellsdk-1.1.0'

    Cancel​Timeout
    The duration (in ms) for which PowerShell should wait before it times out on cancel operations (close runspace or stop powershell). For instance, when the user hits ctrl-C, New-PSSession cmdlet tries to call a stop on all remote runspaces which are in the Opening state. The administrator wouldn't mind waiting for 15 seconds, but this should be time bound and of a shorter duration. A high timeout here like 3 minutes will give the administrator a feeling that the PowerShell client has hung.

The topic ‘Runspace issue - Adding timeout to each runspace’ is closed to new replies.