Skip to content

Instantly share code, notes, and snippets.

@luisfdez luisfdez/RdpActuator.ps1
Last active Aug 29, 2015

Embed
What would you like to do?
Actuator scripts to restart wsgate when it's stuck (PowerShell functionality + Python Windows Service wrapper)
#
# Create EventLog for messages
#
If(-Not [System.Diagnostics.EventLog]::SourceExists('RdpConsoleActuator'))
{
New-EventLog -LogName Application -Source "RdpConsoleActuator"
}
Write-EventLog -LogName Application -Source "RdpConsoleActuator" -EntryType Information -EventID 1 -Message "[$(Get-Date -Format g)] - RDP Console Actuator starting..."
#
# Test the availability of the RDP port
#
While($true)
{
Try
{
$tcpClient = New-Object System.Net.Sockets.TcpClient
$tcpClient.Connect("localhost", 7080)
}
Catch
{
Write-EventLog -LogName Application -Source "RdpConsoleActuator" -EntryType Error -EventID 100 -Message "[$(Get-Date -Format g)] - Wsgate process stuck. Restarting the FreeRDP HTML5 Proxy."
Stop-Process -Name wsgate -Force
Start-Service wsgate
}
Finally
{
$tcpClient.Dispose()
}
Start-Sleep -Seconds 30
}
import psutil
import servicemanager
import subprocess
import sys
import win32service
import win32serviceutil
def kill_proc_tree(pid, including_parent=True):
parent = psutil.Process(pid)
for child in parent.get_children(recursive=True):
servicemanager.LogInfoMsg("RdpActuator: Stopping (Killing child: %s)" % child)
child.kill()
if including_parent:
servicemanager.LogInfoMsg("RdpActuator: Stopping (Killing parent: %s)" % parent)
parent.kill()
class RdpActuator(win32serviceutil.ServiceFramework):
_svc_name_ = "rdp-console-actuator"
_svc_display_name_ = "rdp-console-actuator"
_svc_description_ = ""
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.p = None
def SvcDoRun(self):
servicemanager.LogInfoMsg('RdpActuator: Starting')
self.p = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
'-File',
r'C:/Program Files (x86)/Cloudbase Solutions/FreeRDP-WebConnect/Binaries/RdpActuator.ps1'])
self.p.wait()
servicemanager.LogInfoMsg("RdpActuator: Stopped")
def SvcStop(self):
servicemanager.LogInfoMsg("RdpActuator: Recieved stop signal")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
kill_proc_tree(self.p.pid)
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(RdpActuator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.