From 37aa8b6f5ca66a531a41234e819165462a5ddcbb Mon Sep 17 00:00:00 2001 From: SrijanPatwa <148682493+SrijanPatwa@users.noreply.github.com> Date: Sat, 4 Oct 2025 17:28:06 +0530 Subject: [PATCH 1/3] impersonationInsights.js This script help to get the impersonator and impersonated user details, and duration of impersonation --- .../impersonationInsights.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js diff --git a/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js new file mode 100644 index 0000000000..3fffbc5828 --- /dev/null +++ b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js @@ -0,0 +1,60 @@ +var impersonatorSysId = 'zane.sulikowski'; //Replace it with the userID of userfor whom we need to check impersonation details + +var checkUserId = new GlideRecord('sys_user'); +if (checkUserId.get('user_name', impersonatorSysId)) { + + var eventsGR = new GlideRecord('sysevent'); + eventsGR.addEncodedQuery("name=impersonation.start^ORname=impersonation.end^parm1=" + impersonatorSysId + "^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()"); + eventsGR.orderBy('sys_created_on'); + eventsGR.query(); + + //This object will hold all events grouped by impersonated user which is in parm2 + var userEvents = {}; + + while (eventsGR.next()) { + var impersonatedId = eventsGR.getValue('parm2'); + if (!userEvents[impersonatedId]) + userEvents[impersonatedId] = []; + userEvents[impersonatedId].push({ + name: eventsGR.getValue('name'), + time: eventsGR.getValue('sys_created_on') + }); + } + +} else { + gs.info('Invalid User'); +} + + +function getUserName(sysId) { + var getUser = new GlideRecord('sys_user'); + if (getUser.get(sysId)) { + return getUser.getDisplayValue('name'); + } + return sysId; +} + + +for (var userId in userEvents) { + var events = userEvents[userId]; + var totalSeconds = 0; + var startTime = null; + + events.forEach(function(evt) { + if (evt.name === 'impersonation.start') { + startTime = new GlideDateTime(evt.time); + } else if (evt.name === 'impersonation.end' && startTime) { + var endTime = new GlideDateTime(evt.time); + totalSeconds += (endTime.getNumericValue() - startTime.getNumericValue()) / 1000; + startTime = null; + } + }); + + + var hours = Math.floor(totalSeconds / 3600); + var minutes = Math.floor((totalSeconds % 3600) / 60); + var seconds = Math.floor(totalSeconds % 60); + + gs.info(impersonatorSysId + " impersonated User: " + getUserName(userId) + + " - Total Duration of impersonation is : " + hours + "hrs " + minutes + "min " + seconds + "sec (" + totalSeconds + "sec)"); +} From 4beeead74b695d29c6a0464c91152522674fb8ac Mon Sep 17 00:00:00 2001 From: SrijanPatwa <148682493+SrijanPatwa@users.noreply.github.com> Date: Sat, 4 Oct 2025 17:32:22 +0530 Subject: [PATCH 2/3] readme.md This script helps to get the impersonator and impersonated user details and duration of impersonation. --- .../Retrieve Impersonation Insights/readme.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Retrieve Impersonation Insights/readme.md diff --git a/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/readme.md b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/readme.md new file mode 100644 index 0000000000..c26bfd26d6 --- /dev/null +++ b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/readme.md @@ -0,0 +1,10 @@ +This script helps to get the impersonator and impersonated user details and duration of impersonation. + + +Details of Events: + +(impersonation.start) which shows that the impersonation has started, +(impersonation.end) shows that the impersonation has ended. + +Parm1 contains the userid of user who started the impersonation. +Parm2 contains the userid of user whom we have impersonated. From 15c034797186eafaa8042f3808fb67de0e2b2abb Mon Sep 17 00:00:00 2001 From: SrijanPatwa <148682493+SrijanPatwa@users.noreply.github.com> Date: Sat, 4 Oct 2025 18:23:58 +0530 Subject: [PATCH 3/3] impersonationInsights.js This script help to get the impersonator and impersonated user details, and duration of impersonation --- .../impersonationInsights.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js index 3fffbc5828..b29811335f 100644 --- a/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js +++ b/Server-Side Components/Background Scripts/Retrieve Impersonation Insights/impersonationInsights.js @@ -1,23 +1,23 @@ -var impersonatorSysId = 'zane.sulikowski'; //Replace it with the userID of userfor whom we need to check impersonation details +var impersonatorUserID = 'zane.sulikowski'; //Replace it with the user ID of user for whom we need to check impersonation details -var checkUserId = new GlideRecord('sys_user'); -if (checkUserId.get('user_name', impersonatorSysId)) { +var isUserPresent = new GlideRecord('sys_user'); +if (isUserPresent.get('user_name', impersonatorUserID)) { - var eventsGR = new GlideRecord('sysevent'); - eventsGR.addEncodedQuery("name=impersonation.start^ORname=impersonation.end^parm1=" + impersonatorSysId + "^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()"); - eventsGR.orderBy('sys_created_on'); - eventsGR.query(); + var queryEvents = new GlideRecord('sysevent'); + queryEvents.addEncodedQuery("name=impersonation.start^ORname=impersonation.end^parm1=" + impersonatorUserID + "^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()"); + queryEvents.orderBy('sys_created_on'); + queryEvents.query(); //This object will hold all events grouped by impersonated user which is in parm2 var userEvents = {}; - while (eventsGR.next()) { - var impersonatedId = eventsGR.getValue('parm2'); + while (queryEvents.next()) { + var impersonatedId = queryEvents.getValue('parm2'); if (!userEvents[impersonatedId]) userEvents[impersonatedId] = []; userEvents[impersonatedId].push({ - name: eventsGR.getValue('name'), - time: eventsGR.getValue('sys_created_on') + name: queryEvents.getValue('name'), + time: queryEvents.getValue('sys_created_on') }); } @@ -55,6 +55,6 @@ for (var userId in userEvents) { var minutes = Math.floor((totalSeconds % 3600) / 60); var seconds = Math.floor(totalSeconds % 60); - gs.info(impersonatorSysId + " impersonated User: " + getUserName(userId) + + gs.info(impersonatorUserID + " impersonated User: " + getUserName(userId) + " - Total Duration of impersonation is : " + hours + "hrs " + minutes + "min " + seconds + "sec (" + totalSeconds + "sec)"); }