Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, January 28, 2013

How to speed up the time out process in PowerShell

While executing scripts against a large number of clients in your organization, you will undoubtedly encounter a few that are offline.  This causes your script to hang on each one.  This pesky little behavior can be mitigated in a variety of ways.  The function below is what I use when I need to run a script synchronously.  You simple give it the name of the client you are attempting to contact and the number of retries it gets.  I usually only give it 1.  It will return TRUE if contact was made.  You then make a decision of what to do at that point. I allow the script to proceed if the response is TRUE or I send the client name to an error log if it is FALSE.
Function Test-ClientConnectivity
{
Param (
    $ComputerName,
    $Loop = 4)

    # Set the control value to zero.
    $Count = 0
    Do {
        $Count ++
        $Result = Test-Connection $ComputerName -Count 1 -Quiet
        If ($Result)
        {
            # If a positive responce is recieved, set the value
            # high to exit the loop.
            $Count = 1000000
        }

    } while ($Count -lt $Loop)

    # Return the object to the calling statment.
    Write-Output $Result
} # End Function: Test-CliectConnectivity -------------------------------------

No comments: