timeout powershell command execution

%3CLINGO-SUB%20id%3D%22lingo-sub-1527130%22%20slang%3D%22en-US%22%3Etimeout%20powershell%20command%20execution%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1527130%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20know%20if%20there%20is%20any%20way%20we%20can%20timeout%20PowerShell%20command%20execution%20after%20specific%20time%20interval.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlready%20tried%20using%20start-job%2C%20wait-job%20%26amp%3B%20stop-job(not%20able%20to%20exit%20ssl%20session%20explained%20in%20below%20example).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20tried%20to%20use%20a%20timer%20as%20explained%20here%20(not%20able%20to%20stop%20the%20command%20execution)%20-%26gt%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fmcpmag.com%2Farticles%2F2018%2F03%2F16%2Fwait-action-function-powershell.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fmcpmag.com%2Farticles%2F2018%2F03%2F16%2Fwait-action-function-powershell.aspx%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20install%20openssl%20in%20Windows%2C%20then%20try%20to%20connect%20to%20a%20RPC%20port.%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E-%20openssl.exe%20s_client%20-connect%20localhost%3A135%20-%26gt%3B%20this%20will%20connect%20to%20port%20-%26gt%3B%20ctrl%2Bc%20will%20exit%20the%20connection.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E-%20echo%20%22Q%22%20%7C%26nbsp%3Bopenssl.exe%20s_client%20-connect%20localhost%3A135%20-%26gt%3B%20should%20actually%20exit%20the%20ssl%20session%20but%20it%20won't%20happen%20only%20for%20RPC%20or%20some%20specific%20ports.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E-%20echo%20%22Q%22%20%7C%26nbsp%3Bopenssl.exe%20s_client%20-connect%20localhost%3A443%20-%26gt%3B%20will%20exit%20the%20session%20normally%20as%20it%20is%20running%20webserver%20and%20the%20port%20is%20not%20RPC%20port.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3EFor%20some%20reason%20unable%20to%20exit%20out%20of%20the%20SSL%20session%20for%20some%20ports%20like%20135.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3ESame%20issue%20applies%20when%20i%20try%20using%20below%20method%20as%20well%20for%20port%20135.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E------------------------%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E%24TcpClient%20%3D%20New-Object%20Net.Sockets.TcpClient%3CBR%20%2F%3E%24TcpClient.Connect(%22127.0.0.1%22%2C%20%22135%22)%3CBR%20%2F%3E%24SslStream%20%3D%20New-Object%20Net.Security.SslStream(%24TcpClient.GetStream()%2C%24false%2C(%7B%24True%7D%20-as%20%5BNet.Security.RemoteCertificateValidationCallback%5D))%3CBR%20%2F%3E%24SslStream.AuthenticateAsClient(%22127.0.0.1%22%2C%24null%2C%24null)%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E------------------------%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3Eabove%20sslstream%20will%20exit%20for%20any%20other%20port%20other%20than%20135%20or%20some%20other%20rpc%20ports%20like%2049664%2C49665%2C49667%20etc.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3E%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-in%2Fhelp%2F832017%2Fservice-overview-and-network-port-requirements-for-windows%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsupport.microsoft.com%2Fen-in%2Fhelp%2F832017%2Fservice-overview-and-network-port-requirements-for-windows%3C%2FA%3E%26nbsp%3B-%26gt%3B%20i%20see%20some%20relation%20here%20with%20RPC%20ports.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3ESo%20my%20requirement%20is%20to%20timeout%20PowerShell%20command%20execution%20after%20few%20seconds.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%3CSPAN%20class%3D%22s1%22%3EThanks%20a%20lot%20in%20advance%20for%20any%20kind%20response%20in%20advance.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22p1%22%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1527130%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%20Commands%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1527191%22%20slang%3D%22en-US%22%3ERe%3A%20timeout%20powershell%20command%20execution%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1527191%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAttached%20snapshots%20to%20explain%20the%20issue.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1566895%22%20slang%3D%22en-US%22%3ERe%3A%20timeout%20powershell%20command%20execution%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1566895%22%20slang%3D%22en-US%22%3E%3CP%3EFound%20Workaround%2FFix%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24SslStream.ReadTimeout%20%3D%204000%3CBR%20%2F%3E%24SslStream.WriteTimeout%20%3D%204000%3C%2FP%3E%3C%2FLINGO-BODY%3E
New 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

 

Example: 

 

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("127.0.0.1", "135")
$SslStream = New-Object Net.Security.SslStream($TcpClient.GetStream(),$false,({$True} -as [Net.Security.RemoteCertificateValidationCallback]))
$SslStream.AuthenticateAsClient("127.0.0.1",$null,$null)

------------------------

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