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

PowerShell: Use RoboCopy & Volume Shadow Copy to Mirror Terrabytes of Data

version 0.2 <#.DescriptionCurrent Features:1. Check for any errors on the Sources or Destinations and generate…

LAMPP Passwords Change

Change ProFTP password: vim /opt/lampp/etc/proftpd.conf <?-- delete the last line in password, leave the first…

Installing Apache on Centos 7

The easy methodsudo yum clean allsudo yum -y updatesudo yum -y install httpd php-pearsudo firewall-cmd…