timeout powershell command execution

Copper Contributor


I would like to know if there is any way we can timeout PowerShell command execution after specific time interval.


Already tried using start-job, wait-job & stop-job(not able to exit ssl session explained in below example).


Also tried to use a timer as explained here (not able to stop the command execution) -> https://mcpmag.com/articles/2018/03/16/wait-action-function-powershell.aspx




I would install openssl in Windows, then try to connect to a RPC port.


- openssl.exe s_client -connect localhost:135 -> this will connect to port -> ctrl+c will exit the connection.


- echo "Q" | openssl.exe s_client -connect localhost:135 -> should actually exit the ssl session but it won't happen only for RPC or some specific ports.


- echo "Q" | openssl.exe s_client -connect localhost:443 -> will exit the session normally as it is running webserver and the port is not RPC port.


For some reason unable to exit out of the SSL session for some ports like 135.


Same issue applies when i try using below method as well for port 135.



$TcpClient = New-Object Net.Sockets.TcpClient
$TcpClient.Connect("", "135")
$SslStream = New-Object Net.Security.SslStream($TcpClient.GetStream(),$false,({$True} -as [Net.Security.RemoteCertificateValidationCallback]))


above sslstream will exit for any other port other than 135 or some other rpc ports like 49664,49665,49667 etc.


https://support.microsoft.com/en-in/help/832017/service-overview-and-network-port-requirements-for-w... -> i see some relation here with RPC ports.


So my requirement is to timeout PowerShell command execution after few seconds.


Thanks a lot in advance for any kind response in advance.



2 Replies


Attached snapshots to explain the issue.

Found Workaround/Fix:


$SslStream.ReadTimeout = 4000
$SslStream.WriteTimeout = 4000