By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

MOM script for Citrix idle sessions: ? about running shell commands

P: n/a
At the end of this message I've pasted a script we're trying to modify
slightly. I don't believe it is VBscript or javascript but these are
the closest groups I could find with my limited programming knowledge.
Basically, we are trying to add a few lines to this script that will
execute a few shell commands (see comments at the very end of the
code). We think this may be ActionScript2 but aren't sure.

If you can offer any help, or know of a more approrpriate place for me
to post this message, please let me know. Thanks!

===========================

/ event constants
var EVENT_TYPE_SUCCESS = 0;
var EVENT_TYPE_ERROR = 1;
var EVENT_TYPE_WARNING = 2;
var EVENT_TYPE_INFORMATION = 4;

// WMI constants
var WBEM_IMPERSONATE = 3;

// Citrix WMI constants
var SESSION_ACTIVE = 0;
function GenerateEvent(EventNo, EventType, Message) {
var MyEvent = ScriptContext.CreateEvent();
MyEvent.Message = Message;
MyEvent.EventNumber = EventNo;
MyEvent.EventType = EventType;
MyEvent.EventSource = "Citrix MetaFrame";

ScriptContext.Submit(MyEvent);
}

function logError(message, error)
{
var str = SCRIPT_NAME + ":\n" + message;

if (error)
{
var num = (error.number<0) ? error.number+0x100000000 :
error.number;
var estr = error.description;

if (num == 0x80010105)
estr += " [RPC_E_SERVERFAULT: The server threw an
exception.]";
else if (num == 0x80041001)
estr += " [WBEM_E_FAILED]";
else if (num == 0x80010108)
estr += " [RPC_E_DISCONNECTED]";
else if (num == 0x8007007e)
estr += " [ERROR_MOD_NOT_FOUND: The specified module could
not be found.]";
else if (num == 0x80041010)
estr += " [WBEM_E_INVALID_CLASS]";
else if (num == 0x8004100e)
estr += " [WBEM_E_INVALID_NAMESPACE]";

if (estr == "")
estr = "<error description not given>";
str += ":\n\tReceived error: 0x" + num.toString(16) + ": " +
estr;
}

GenerateEvent(2, EVENT_TYPE_ERROR, str); }

function ConvertWMITime(wmitime)
{
var year = wmitime.substring(0, 4);
var month = wmitime.substring(4, 6) - 1;
var day = wmitime.substring(6, 8);
var hour = wmitime.substring(8, 10);
var minute = wmitime.substring(10, 12);
var second = wmitime.substring(12, 14);

var date = new Date(year, month, day, hour, minute, second);
return date;
}

function Main()
{
// script should only be run in response to a timed event
if (!ScriptContext.IsEvent()) {
GenerateEvent(1, EVENT_TYPE_ERROR, SCRIPT_NAME +
": this script can only be run in response to an event");
return;
}

// parse parameters: 'limit' is the number of minutes the session
// must have been idle before an event is generated
var limit;
try {
limit = ScriptContext.Parameters.Get("limit");
} catch (e) {
logError("Can't retrieve script parameter", e);
return;
}

// coerce into an integer
var nrminutes = parseInt(limit);
if (isNaN(nrminutes) || nrminutes <= 0) {
logError("`limit' parameter (" + limit +
") is not a valid positive number");
return;
}

// first, retrieve all instances of MetaFrame_Session
var locator, service, sessionobj;
try {
locator = new ActiveXObject("WbemScripting.SWbemLocator");
service = locator.ConnectServer("", "root/Citrix");
sessionobj = service.Get("MetaFrame_Session");
sessionobj.Security_.impersonationLevel = WBEM_IMPERSONATE;
} catch (e) {
logError("WMI error retrieving session object", e);
return;
}

var instances;
try {
instances = sessionobj.Instances_();
} catch (e) {
logError("WMI error retrieving session instances", e);
return;
}
// next check the idle time of active sessions and raise an event
// if it exceeds the limit given in the parameter
try {
// gratuitous access to Count to generate error if instances
// can't be retrieved
var nrsessions = instances.Count;

var e = new Enumerator(instances);
for (; !e.atEnd(); e.moveNext()) {
var session = e.item();
if (session.SessionState != SESSION_ACTIVE)
continue;

var last = ConvertWMITime(session.LastInputTime);
var now = ConvertWMITime(session.CurrentTime);

var diff = (now.getTime() - last.getTime()) / (1000 * 60);

if (diff > nrminutes) {

var user = "(unknown user)";
try {
var userobj = service.Get(session.SessionUser);
userobj.Security_.impersonationLevel =
WBEM_IMPERSONATE;
user = userobj.UserName;
} catch (e) {
}

var ev = ScriptContext.CreateEvent();
ev.EventNumber = EVENT_ID;
ev.EventType = EVENT_TYPE_ERROR;
ev.EventSource = "Citrix MetaFrame";
ev.Message = "Session " + session.SessionID + " for
user " +
user + " has been idle for too long.";
ev.UserName = user;
ev.SetEventParameter(session.SessionID);
ev.SetEventParameter(Math.round(diff));
ev.SetEventParameter(nrminutes);
ScriptContext.Submit(ev);

// XXXXXXXXXXXXXXX CHANGE TO INSERT HERE XXXXXXXXXXXXXXXXXXXXXX
// We need to change this script so that instead of running the
// commands above (that write an entry to the Event Log), it instead
// executes some external shell commands. It would look something
like
// this in VB script:
//
// oShell.Run("c:\utils\pskill winword.exe %username%")
// oShell.Run("c:\utils\pskill excel.exe %username%")
//
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXX
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.