title: While trying to initiate new Ps-Session it Stops Working in Script
body: <p>I have a strange problem randomly Occurring.</p>
  
  <p>Background : i Use a Script to Remote-Connect to a Server and plant a Sheduled-Task. While the Local Sheduled-Task is running i constantly check a Folder for a Log-file that tells me when the local script finishes.</p>
  
  <p>Now i ran into some Strange problems. At first the Sleep loop i was in got left for no good reason. then i changed the code to create a new Powershell-Session every minute to make sure that the session wasn't dying.</p>
  
  <p>Tonight the Problem happens again and at least the loop wasn't exited anymore. but the client wasn't able to build a new PS-Session after the 3 or 4th try.</p>
  
  <p>Here's the Part of my Code. It works fine in 9 out of 10 Times.</p>
  
  <pre><code>      $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
              }
          }
          }
  </code></pre>
  
  <p>Here's Part of the Log : 
  It Entered the loop at 03:01:22</p>
  
  <pre><code>     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
  </code></pre>
  
  <p>So he was definitely able to create a Session 3 times (one time before the loop and 2 times while in the loop)
  I'm out of Ideas. is there a Timeout for the $Cred Object or the $SecurePassWord Object?
  The Server was definitely reachable during the time. I tried creating a new Session this Morning and it works so its definitely not the Target blocking the connects.</p>
  
  <p>UPDATE: thanks to the tip from ojk i captured the Error Message :</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Anybody got an idea? it happens 4 times this night with several hours in between. i cant wrap my head around.</p>
  
  <p>Fun side fact. if i open a new Powershell window (while the running script still isn't able to connect). A new PS window is able to open a connection just fine. a Reboot on the target side doesn't fix the issue as well. so its definitely a Problem on the Server-side.</p>
  
  <p>Edit: i uploaded a whole procmon loop of the powershell process. hers the result (too long to post it here fully) <a href="http://textuploader.com/oer3" rel="nofollow noreferrer">http://textuploader.com/oer3</a></p>