Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

powershell if statement and timer elapsed using -'powershell-v3.0'

powershell if statement and timer elapsed  using -'powershell-v3.0'

The purpose of my code, is to run a RDP connection (in a custom shell) on thinclients. Whenever the RDP logon is cancelled, or the RDP connection gets terminated, powershell will detect this and initiate the RDP connection again. Users will always have their logon screen in front of them.

This works perfectly. But another problem is, that when the logon screen is openend for 15 or more minutes, RDP connection fails. So I want the RDP connection to be restarted every 15 minutes.

In short, I want the If-Else statement and the Timer.Elapsed condition to run side by side. I am at a loss how to get this working correctly.

$previous_Handles = 0
$timer = New-Object Timers.Timer
$timer.Interval = 10000
$timer.Enabled = $True
$timer.Autoreset = $True
Register-ObjectEvent -InputObject $Timer -EventName Elapsed -Action {Stop-Process -Id $process.Id;$previous_handles = 0;Start-Process C:\Windows\System32\mstsc.exe C:\Connection\Terminal-Server.RDP}

        Start-Process C:\Windows\System32\mstsc.exe C:\Connection\Terminal-Server.RDP

        while (1)

            $process = Get-Process mstsc
          if((($process.Handles -le 500) -and ($previous_Handles -ge 500)) -or ($process -eq $null))
            Stop-Process -Id $process.Id
            $previous_Handles = 0
            Start-Process C:\Windows\System32\mstsc.exe C:\Connection\Terminal-Server.RDP
            Start-Sleep -m 500
            $previous_Handles = $process.Handles


asked Sep 7, 2015 by rajesh
0 votes

Related Hot Questions

Government Jobs Opening