我有一个奇怪的问题随机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