我有一個奇怪的問題隨機Occouring。New-PsSession停止使用腳本
背景:我使用腳本遠程連接到服務器和廠Sheduled任務。當本地Sheduled任務正在運行時,我經常檢查一個日誌文件的文件夾,告訴我本地腳本什麼時候結束。
現在我遇到了一些奇怪的問題。起初,我在沒有充分理由的情況下進入了睡眠循環。然後我改變了代碼,每分鐘創建一個新的Powershell-Session,以確保會話不會死亡。
今夜的問題又happend和至少循環wasnt退出了。但客戶無法在3或4次嘗試後建立新的PS會話。
繼承人我的代碼的一部分。它可以在10次中的9次中正常工作。
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord
$pssessionoption = new-pssessionoption -operationtimeout 7200000 -IdleTimeout 7200000
#i create a Session after that , plant a sheduled task and start waiting here.
while (!(Invoke-Command -Session $Session -Scriptblock $CheckifClientDone))
{
Remove-PSSession -Session $Session
Start-Sleep -Seconds 120
$doesitconnect = $false
$CurrentPatchingState = "1;$Servername;Status=1;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Still Waiting" | Out-File -Filepath "C:\Logs\Test_Logs\$Servername.txt" -Append -encoding ASCII
while (!($doesitconnect)){
If ($counterfortimeout -eq 300){ #5 Hour timeout
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Something went Wrong on the Client - Aborting after 5h of waiting" | Out-File -Filepath "C:\Test_Logs\$Servername.txt" -Append -encoding ASCII
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Something went Wrong on the Client - Aborting after 5h of waiting" | Out-File -Filepath "C:\Logs\Nagios\$Servername.txt" -encoding ASCII
exit
}
$counterfortimeout = $counterfortimeout + 1
Try {
$Session = New-PSSession -ComputerName $ServerName -credential $Cred -sessionOption $pssessionoption -EA Stop
$doesitconnect = $true
}
Catch [system.exception]
{
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Could not create a Session with the Host at round $counterfortimeout ERROR : $_.Exception.Message" | Out-File -Filepath "C:\Logs\Test_Logs\$Servername.txt" -Append -encoding ASCII
$CurrentPatchingState = "2;$Servername;Status=2;$(Get-Date -format 'dd.MM.yyyy hh:mm:ss') Could not create a Session with the Host at round $counterfortimeout ERROR : $_.Exception.Message" | Out-File -Filepath "C:\Logs\Nagios\$Servername.txt" -encoding ASCII
Remove-PSSession -Session $Session
$doesitconnect = $false
Start-Sleep -Seconds 60
}
}
}
繼承人的日誌的一部分: 它進入了環在3時01分22秒
1;Server1;Status=1;03.12.2014 03:03:22 Still Waiting
1;Server1;Status=1;03.12.2014 03:05:23 Still Waiting
2;Server1;Status=2;03.12.2014 03:05:23 Could not create a Session with the Host at round 2 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
2;Server1;Status=2;03.12.2014 03:06:23 Could not create a Session with the Host at round 3 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
2;Server1;Status=2;03.12.2014 03:07:23 Could not create a Session with the Host at round 4 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
2;Server1;Status=2;03.12.2014 03:08:23 Could not create a Session with the Host at round 5 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
2;Server1;Status=2;03.12.2014 03:09:23 Could not create a Session with the Host at round 6 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
.
.
.
2;Server1;Status=2;03.12.2014 08:03:24 Could not create a Session with the Host at round 300 ERROR Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
2;Server1;Status=2;03.12.2014 08:04:24 Something went Wrong on the Client - Aborting after 5h of waiting
所以他defenetly能的3倍(環路2日前一次性創建一個會話時間在循環中) 進出思想。是否有$ Cred對象或$ SecurePassWord對象的超時? 服務器在此期間無法訪問。我今天早上嘗試創建一個新的會話,它的工作原理並不是Target阻止連接。
UPDATE:
Illegal operation attempted on a registry key that has been marked for deletion. For more information, see the about_Remote_Troubleshooting Help topic..Exception.Message
任何人有一個想法:從OJK我拍攝的錯誤消息感謝小費?它今晚發生4次,中間有幾個小時。我無法纏住我的頭。
有趣的副作用。如果我打開一個新的Powershell窗口(而運行腳本仍然不能連接)。一個新的PS窗口能夠打開一個連接就好了。在目標端重新啓動並不能解決問題。所以它毫無疑問是Serverside上的一個問題。
編輯:我上傳的過程的powershell的整個procmon中循環。她的結果(太長,無法完全發佈)http://textuploader.com/oer3
嘗試登錄異常消息本身,以及,可能會告訴你一些東西。 (你可以在catch塊中使用$ _。Exception.Message來獲取它) – ojk 2014-12-03 10:37:38
感謝您的提示,我將其添加到我的腳本中。可悲的是,我不能真正重現錯誤,因爲它工作10次9次,微軟每個月只發布一次修補程序。一旦它再次發生,我會更新該帖子。任何其他更一般的想法可能是什麼問題? – Ceuse 2014-12-03 11:27:41
會話超時了嗎?嘗試將pssessionoption的idletimeout設置爲-1,這應該完全禁用空閒超時。請注意,這樣做意味着它會使用WSMan shell超時值,而不是我認爲的。 – ojk 2014-12-03 11:37:30