Thursday, September 10, 2020

Citrix Receiver -- Prevent Session Disconnect on Inactivity

Problem

You are connecting to a remote desktop or app using Citrix Receiver on Windows local machine.  After certain period of inactivity, the session drops. You want to prevent session disconnect by simulating KeepAlive packets sent to the remote desktop

Solution

You may use ICA Client Object SDK and ICA Simulation API SDK to dynamically connect to the active session and simulate keepAlive by sending dummy keystrokes to the Citrix sessions.

First, in the Windows Registry, create this key:
 
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Citrix\ICA Client\CCM

and add two values in it:

AllowLiveMonitoring Dword 1
AllowSimulationAPI  Dword 1

 
Save the below JScript (with the .js extention) for Windows Host Scripting locally:

while(1) {
   var icaClient    = new ActiveXObject("Citrix.ICAClient");
   var enumHandle   = icaClient.EnumerateCCMSessions();
   var sessionCount = icaClient.GetEnumNameCount(enumHandle);
   WScript.echo( "Found " + sessionCount + " sessions...");
   var icaSession = new ActiveXObject("Citrix.ICAClient");
   for (var i = 0; i < sessionCount; i++) {
      WScript.echo( " Sending keep alive to session: " + i + ": ");
      var session = icaClient.GetEnumNameByIndex(enumHandle, i);
      WScript.echo( "  " + session + "");
      var icaSession = new ActiveXObject("Citrix.ICAClient");
      icaSession.SetProp("OutputMode", "1");
      icaSession.StartMonitoringCCMSession(session, true);
      icaSession.Session.Keyboard.SendKeyDown(0x91); //VK_SCROLL
      icaSession.StopMonitoringCCMSession(session);
   }
   WScript.echo(" Done sending keep alive to all sessions!");
   WScript.echo();
   WScript.Sleep(240000);
}


Since the ICA Client is a 32 bit, you need to use a 32 bit cscript.exe to execute the script:

C:\Windows\SysWOW64\cscript.exe CitrixKeepAlive.js
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

Found 2 sessions...
 Sending keep alive to session: 0:
  828368652
 Sending keep alive to session: 1:
  111767151
 Done sending keep alive to all sessions!


Reference:




2 comments:

channaman said...

This script works great! I got it to run even though I have no coding experience. It looks like it sends a SCROLL LOCK command to keep the session alive. Is it possible to change the command to something like F15 or some other key? Also, what is the current time interval it sends the command and how can it be adjusted?

Anonymous said...

For me it complains that the library is not registered for line 14 in the script:

icaSession.Session.Keyboard.SendKeyDown(0x91); //VK_SCROLL

Any idea?