遠端 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 會意外終止。

我需要添加不同的或更多的-SessionOptions 嗎?

有沒有辦法找出 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

這是基於下列-IdleTimeoutSecMicrosoft Powershell 文件中可選參數的描述斷開連接-PSSession

Powershell 指令的 MS 文件範例 3 的第九個指令也對此進行了很好的解釋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 版本有關:

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(零)表示沒有超時;該操作無限期地繼續。

相關內容