リモートPowershellセッションがランダムに終了する

リモートPowershellセッションがランダムに終了する

以下の Powershell コードは、リモートを起動する方法を示しています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

これはほとんどの場合うまく機能し、$s1 | Enter-PSSessionや コマンドを実行したり、 を使用したりできますInvoke-Command -Session $s1 -ScriptBlock {some code}。完了までに何時間もかかる特定のジョブ (通常は Python または Java) を実行すると、PSSession が予期せず終了することがあります。

別の s またはさらに s を追加する必要がありますか-SessionOption?

PSSession が停止した理由を調べる方法はありますか?

編集

以下は出力です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

編集2

を起動するためのルーチンに以下のコードを追加しましたPSSessionが、PSSession約 2 時間後に理由もなく が停止してしまいます。

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

-IdleTimeoutSecこれは、MicrosoftのPowershellドキュメントのオプションパラメータの説明に基づいています。切断-PSSession

また、PowershellコマンドのMSドキュメントの例3の9番目のコマンドでもよく説明されています。Connect-PSSession 以下に抜粋します。

# 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

MS ドキュメントのエラーか、Powershell のバージョンの違いによる奇妙な点の 1 つは、上記のコマンドで次のエラーが発生することです。

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
   PSSessionCommand

ただし、渡された整数値が-IdleTimeoutSec文字列に変換されると、次のコマンドは正常に機能します。

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

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

答え1

IdleTimeout 使用したものとは別のデフォルトのタイムアウト パラメータが実行されている可能性があります。

パラメータなしでコマンドを実行しますNew-PSSessionOption(確認したい場合は、投稿に出力を含めてください)。 に特に注意してくださいOperationTimeout

のドキュメントから 新しいPSSessionOption:

-操作タイムアウト

セッション内の任意の操作を実行できる最大時間を決定します。間隔が経過すると、操作は失敗します。値をミリ秒単位で入力します。

デフォルト値は 180000 (3 分) です。値が 0 (ゼロ) の場合、タイムアウトはなく、操作は無期限に続行されます。

関連情報