The Powershell code below is how I am starting the remote PSSession.

$compName = "server1"

$jobname = "admin_test"

$cred = Get-Credential -UserName "DOMAIN\$env:username" -Message "DOMAIN\$env:username password"

$s_opt = New-PSSessionOption -IdleTimeout -1

$s1 = New-PSSession -ComputerName $compName -Name $jobname -SessionOption $s_opt -Credential $cred

This works great for the most part and we are able to $s1 | Enter-PSSession and run commands, or use Invoke-Command -Session $s1 -ScriptBlock {some code}. When we run particular jobs (usually python or java) that may take many hours to complete, sometimes the PSSession dies unexpectedly.

Do I need to add different or more -SessionOptions ?

Is there a way to find out why the PSSession died ?


Below is the output of New-PSSessionOption

PS C:\> New-PSSessionOption

MaximumConnectionRedirectionCount : 5
NoCompression                     : False
NoMachineProfile                  : False
ProxyAccessType                   : None
ProxyAuthentication               : Negotiate
ProxyCredential                   :
SkipCACheck                       : False
SkipCNCheck                       : False
SkipRevocationCheck               : False
OperationTimeout                  : 00:03:00
NoEncryption                      : False
UseUTF16                          : False
IncludePortInSPN                  : False
OutputBufferingMode               : None
MaxConnectionRetryCount           : 0
Culture                           :
UICulture                         :
MaximumReceivedDataSizePerCommand :
MaximumReceivedObjectSize         :
ApplicationArguments              :
OpenTimeout                       : 00:03:00
CancelTimeout                     : 00:01:00
IdleTimeout                       : -00:00:00.0010000

Edit 2

I added code below to my routine to start the PSSession, however the PSSession still stops for no reason after about 2 hours.

## -IdleTimeoutSec in sec/min * min/hr * hrs/day * days
Disconnect-PSSession -Session $s1 -IdleTimeoutSec 60*60*24*3

This was based on the following the description of -IdleTimeoutSec optional parameter in Microsoft's Powershell docs Disconnect-PSSession

Also explained well in the ninth command of Example 3 of the MS docs for the Powershell command Connect-PSSession Examples excerpted below.

# The ninth command disconnects from the session in the $s variable.The administrator closes PowerShell and closes the computer. She can reconnect to the session on the next day and check the script status from her work computer.
PS C:\> Disconnect-PSSession -Session $s -OutputBufferingMode Drop -IdleTimeoutSec 60*60*15

One crazy thing that is either an error in the MS docs or has to do with a different Powershell version is that the command above gives the following error:

Disconnect-PSSession : Cannot bind parameter 'IdleTimeoutSec'. Cannot convert value "60*60*24*3" to
type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:76
+ ... sion -Session $x -IdleTimeoutSec 60*60*24*3
+                                      ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Disconnect-PSSession], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.Disconnect

However, the following command works fine when the integer value passed to -IdleTimeoutSec is converted to a string.

PS C:> Disconnect-PSSession -Session $s1 -IdleTimeoutSec "259200"

 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
  5 admin_test      Server1         RemoteMachine   Disconnected  Microsoft.PowerShell          None

You might be running into another default timeout parameter than IdleTimeout that you have used.

Run the command New-PSSessionOption without parameters (include the output in your post if you wish us to have a look). Pay special attention to OperationTimeout.

From the documentation for New-PSSessionOption:


Determines the maximum time that any operation in the session can run. When the interval expires, the operation fails. Enter a value in milliseconds.

The default value is 180000 (3 minutes). A value of 0 (zero) means no time-out; the operation continues indefinitely.

  • edited OP to include output of New-PSSessionOption – Clay Oct 10 '18 at 23:19
  • Also added other things I have tried like Disconnect-PSSession -Session $s1 -IdleTimeoutSec 60*60*24*3 - Session still stops after ~ 2 hours. – Clay Oct 10 '18 at 23:34
  • Your OperationTimeout is still 3 minutes. I think that the documentation of Powershell is not fully correct, but it's still a mystery that your operation works successfully for 2 hours. – harrymc Oct 11 '18 at 7:52
  • the final code Disconnect-PSSession -Session $s1 -IdleTimeoutSec "259200" seems to be working. I have had a simple job running successfully in that PSSession for approximately 20 hours now. – Clay Oct 11 '18 at 14:35
  • Seems that OperationTimeout is ignored, but one never knows, and the next major update to Windows 10 arrives very soon ... Wouldn't hurt to set it also to "259200". – harrymc Oct 11 '18 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.