Posted On April 30, 2021

PowerShell: Connecting via WinRM with a Timeout

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

Use PowerShell to Get GeoLocation of a Computer’s Public IP

Method 1: Invoke-RestMethod -Uri ('http://ipinfo.io/'+(Invoke-WebRequest -uri "http://ifconfig.me/ip").Content) Method 2: function getIPGeolocation($ipAddress) {$request = Invoke-RestMethod -Method…

Sample Forth Shift Script

REM Daily Batch for Fourth Shiftrem E:\FShift\cabsauto\timeout 14400rem E:\FShift\cabsauto\timeout 7200rem E:\FShift\cabsauto\timeout 7200rem E:\FShift\cabsauto\timeout 7200rem E:\FShift\cabsauto\timeout…

PowerShell: Convert Array to Hash and back to Array

# convert 2D Array to Hash Table while removing any duplicate keys function arrayToHash($arr){ $hash…