PowerShell: Connecting via WinRM with a Timeout

$credentials=get-credential

$sessionTimeout=New-PSSessionOption -OpenTimeout 120000 # 2 minutes
        $sessionIncludePort=New-PSSessionOption -IncludePortInSPN -OpenTimeout 120000
        $session=if($credentials){
                try{
                    New-PSSession -ComputerName $computername -Credential $credentials -ea Stop -SessionOption $sessionTimeout
                }catch{
                    New-PSSession -ComputerName $computername -Credential $credentials -SessionOption $sessionIncludePort
                }
            }else{
                try{
                    New-PSSession -ComputerName $computername -ea Stop -SessionOption $sessionTimeout
                }catch{
                    New-PSSession -ComputerName $computername -SessionOption $sessionIncludePort
                }
            }
if($session.state -eq 'Opened'){write-host "connected."}else{write-host 'NOT connected.'}

Question: what Problem does this solve?
Answer: PowerShell will timeout indefinitely if a connection timeout value is not specified.

WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: A connection attempt failed because the connected party did not properly respond after a period of time, or 
established connection failed because connected host has failed to respond
WARNING: No connection could be made because the target machine actively refused it

Leave a Reply

Your email address will not be published. Required fields are marked *