New-PsSession停止在脚本中工作

时间:2014-12-03 07:54:58

标签: session powershell

我随机出现了一个奇怪的问题。

背景:我使用脚本远程连接到服务器并植入一个Sheduled-Task。当Local Sheduled-Task正在运行时,我不断检查文件夹中的日志文件,告诉我本地脚本何时完成。

现在我遇到了一些奇怪的问题。起初睡眠循环我没有任何理由离开了。然后我改变了代码,每分钟创建一个新的Powershell-Session,以确保会话不会死亡。

今晚问题再次发生,至少循环不再退出。但是在第3次或第4次尝试之后,客户端无法建立新的PS-Session。

这是我的守则的一部分。它在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
        }
    }
    }

Heres部分日志: 它在03: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

所以他无法创造一个会话3次(循环前一次,循环中2次) 我没有想法。 $ Cred对象或$ SecurePassWord对象是否有超时? 在此期间,服务器无法访问。我尝试在今天早上创建一个新的Session,它的工作原理是它的目标是阻止连接。

更新:感谢ojk的提示我捕获了错误信息:

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

有人有个主意吗?这个晚上发生了4次,中间有几个小时。我无法绕过头。

有趣的副作用。如果我打开一个新的Powershell窗口(而运行的脚本仍然无法连接)。一个新的PS窗口可以很好地打开连接。目标方面的重新启动并不能解决问题。所以它在服务器端是一个问题。

编辑:我上传了powershell流程的整个procmon循环。她的结果(太长了,不能在这里完全发布)http://textuploader.com/oer3

1 个答案:

答案 0 :(得分:0)

似乎问题不在于SCript本身,而在于用户日志记录的Windows设置。

我使用了http://support.microsoft.com/kb/2287297并添加了DisableForceUnload注册表项,并且无法复制问题(在脚本启动之后我能够记录之前)。

我会将此标记为已解决,直到我再次遇到此问题。

似乎不是用户而是用ntauthority \ system启动这个脚本也应该解决了这个问题....发现这很痛苦