PasswordConnectionInfo Timeout doesn't appear to be working in Powershell

Apr 25, 2014 at 4:16 AM
Edited Apr 25, 2014 at 4:36 AM
Hi,

I have the following PowerShell script that I'm testing (using either 2014.4.6-beta1 or 2013.4.7). When I put in a bogus IP Address for a switch, the timeout always seems to be about 20-25 seconds, even though I have set it to 60 seconds. If I set the timeout to 5 seconds, it timesout within 5 seconds as expected. It's only when you try end extend the timeout period that it seems to ignore it.

Does anyone have any idea?

#------------------------------------------------------------------------------------
# Define the Read and Write Stream functions for the script to be used
#------------------------------------------------------------------------------------
function ReadStream($reader)
{
    $line = $reader.ReadLine();
    while ($line -ne $null)
    {
        $line
        $line = $reader.ReadLine()
    }
}

function WriteStream($cmd, $writer, $stream)
{
    $writer.WriteLine($cmd)
    while ($stream.Length -eq 0)
    {
        start-sleep -milliseconds 500
    }
}


#------------------------------------------------------------------------------------
# Define Cmdlet to SSH to client and execute custom script to change account password
#------------------------------------------------------------------------------------
function Set-CiscoHostPassword {
[CmdletBinding()]
    param(
        [String]$HostName,
        [int]$Port,
        [String]$UserName,
        [String]$OldPassword,
        [String]$NewPassword
    )

#Script to be called once a SSH session has been established. Add one command per line.
$Script = @"
    config t
    username $UserName privilege 15 secret $NewPassword
    exit
    wr mem
    exit
"@

    try {
        #Make a connection to the host
        Add-Type -Path "[RenciSSHMetPath]\Renci.SshNet.dll" #Include SSH.NET Assembly
        $connectionInfo = New-Object Renci.SshNet.PasswordConnectionInfo($HostName, $Port, $UserName, $OldPassword)
        $connectionInfo.Timeout = New-TimeSpan -Seconds 60  
        
        $sshclient = New-Object Renci.SshNet.SshClient($connectionInfo)
        $sshclient.Connect()
        $sshclient.SendKeepAlive()

        if ($sshclient.IsConnected)
        {
            #Now that we are connected, attempt to execute the script above
            $stream = $sshclient.CreateShellStream("ssh_stream", 80, 24, 800, 600, 1024)
            
            $reader = new-object System.IO.StreamReader($stream)
            $writer = new-object System.IO.StreamWriter($stream)
            $writer.AutoFlush = $true

            while ($stream.Length -eq 0)
            {
                start-sleep -milliseconds 500
            }
            ReadStream $reader

            WriteStream $Script $writer $stream
        
            $stream.Dispose()
            $sshclient.Disconnect()
            $sshclient.Dispose()

            #Use a Switch statement to build up a list of exceptions to capture as required. If there is no matching exception, it is assumed the execution of the script was a success 
            $results = $reader.ReadToEnd()
            switch -wildcard ($results.ToLower()) 
            { 
                "*invalid input detected*" {Write-Output "Failed to execute script correctly against host '$HostName' for the account '$UserName'. Error = " $results} 
                #Add other wildcard matches here as required
                default {Write-Output "Success"}
            }
        }
        Else 
        {   
            Write-Output "Failed to connect to the host '$HostName' to reset the password for the account '$UserName'. Please check the host is online, or if a Firewall is blocking access."
        }
    } catch {
        Write-Output "Failed to reset password for account '$UserName' on Host '$HostName'. Error = " $error[0].Exception
   }
}


#Make a call to the Set-CiscoHostPassword function
cls
Set-CiscoHostPassword -HostName '10.0.0.253' -Port '22' -UserName '[UserName]' -OldPassword '[OldPassword]' -NewPassword '[NewPassword]'
Thanks
Mark