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

Python Module: JSON

Overview JavaScript Object Notation (JSON) is written in plain-text that is very useful to store…

PowerShell: File Server Cluster Migration Scripts

<# File-Server-Migration-Intra-Domain.ps1 Purposes: 1. Create Volumes with Labels 2. Create Virtual Clustered File Servers 3.…

Tasks to Perform After Windows 10 Upgrade

Target for Windows upgrade cleanup: C:\$Windows.~BT\* C:\$Windows.~LS\* C:\$Windows.~WS\* C:\ESD\Download\* C:\ESD\Windows\* C:\$WINDOWS.~Q\* C:\$INPLACE.~TR\* C:\Windows.old\* C:\Windows\Panther Please…