Session 当尝试启动新的Ps会话时,它停止在脚本中工作

Session 当尝试启动新的Ps会话时,它停止在脚本中工作,session,powershell,Session,Powershell,我有一个奇怪的问题随机发生 背景:我使用一个脚本远程连接到服务器并植入一个计划任务。当本地计划任务运行时,我不断检查文件夹中的日志文件,该文件告诉我本地脚本何时完成 现在我遇到了一些奇怪的问题。起初,我所处的睡眠循环无缘无故地消失了。然后我更改了代码,每分钟创建一个新的Powershell会话,以确保会话不会消亡 今晚问题再次发生,至少循环不再退出。但是在第3次或第4次尝试后,客户机无法构建新的PS会话 这是我代码的一部分。10次中有9次效果良好 $SecurePassWord =

我有一个奇怪的问题随机发生

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

现在我遇到了一些奇怪的问题。起初,我所处的睡眠循环无缘无故地消失了。然后我更改了代码,每分钟创建一个新的Powershell会话,以确保会话不会消亡

今晚问题再次发生,至少循环不再退出。但是在第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
            }
        }
        }
以下是日志的一部分: 它在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对象是否有超时? 在这段时间内,服务器是绝对可以访问的。今天早上我尝试创建了一个新会话,它工作正常,因此它肯定不是阻止连接的目标

更新:多亏了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循环。她的结果太长,无法在此处完全发布

问题似乎不在于脚本本身,而在于用于记录用户的Windows设置

我使用并添加了DisableForceUnload注册表项,在脚本启动后通过登录之前无法复制该问题

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


似乎不是用用户而是用nAuthority\system启动这个脚本也应该解决了这个问题。。。。发现这一点非常痛苦

尝试记录异常消息本身,可能会告诉您一些事情。您可以在catch块中使用$\.Exception.Message获得提示,我将其添加到脚本中。可悲的是,我不能真实地重现这个错误,因为它10次中有9次能正常工作,而微软每月只发布一次补丁。我会尽快更新后,它再次发生。还有其他更一般的想法吗?问题出在哪里?是不是会议超时了?尝试将PSSession选项中的idletimeout设置为-1,这将完全禁用空闲超时。请注意,我认为这样做意味着它将使用WSMan shell超时值。如果我每60秒打开一个新会话,会话如何超时?我不知道。。只是在这一点上抛出想法。抱歉。查看您引用的文档,我可以看到以下注意事项:此策略仅适用于由于此特定Windows行为而可能遇到应用程序兼容性问题的情况。默认情况下,不建议启用此策略,因为它可能会阻止用户获取其漫游用户配置文件的更新版本。我需要在人们只连接以部署新代码的服务器上启用此功能。你知道这会不会给用户带来问题吗?