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: Extract Root Domain from URL

Please note that this function requires an updated variable named $domainsDictionary. Since LTD's are being…

PowerShell: How to Create Multiple Arrays or Columns from an Array

# This snippets scans the localhost for common ports and outputs 4 arrays $limitPorts=10000 $netstat=netstat…

PowerShell: Get Windows OS Name

function getOsBuild($computername=$env:computername){ $build="" $pingable=test-connection $computername -Count 1 -Quiet if($pingable){ $psinfoIsAvailable=get-command psinfo -ea SilentlyContinue # Requires…