Skip to content

Commit e874012

Browse files
authored
Get User Impersonation Insights (#1773)
* impersonationInsights.js This script help to get the impersonator and impersonated user details, and duration of impersonation * readme.md This script helps to get the impersonator and impersonated user details and duration of impersonation. * impersonationInsights.js This script help to get the impersonator and impersonated user details, and duration of impersonation
1 parent f8120aa commit e874012

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var impersonatorUserID = 'zane.sulikowski'; //Replace it with the user ID of user for whom we need to check impersonation details
2+
3+
var isUserPresent = new GlideRecord('sys_user');
4+
if (isUserPresent.get('user_name', impersonatorUserID)) {
5+
6+
var queryEvents = new GlideRecord('sysevent');
7+
queryEvents.addEncodedQuery("name=impersonation.start^ORname=impersonation.end^parm1=" + impersonatorUserID + "^sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()");
8+
queryEvents.orderBy('sys_created_on');
9+
queryEvents.query();
10+
11+
//This object will hold all events grouped by impersonated user which is in parm2
12+
var userEvents = {};
13+
14+
while (queryEvents.next()) {
15+
var impersonatedId = queryEvents.getValue('parm2');
16+
if (!userEvents[impersonatedId])
17+
userEvents[impersonatedId] = [];
18+
userEvents[impersonatedId].push({
19+
name: queryEvents.getValue('name'),
20+
time: queryEvents.getValue('sys_created_on')
21+
});
22+
}
23+
24+
} else {
25+
gs.info('Invalid User');
26+
}
27+
28+
29+
function getUserName(sysId) {
30+
var getUser = new GlideRecord('sys_user');
31+
if (getUser.get(sysId)) {
32+
return getUser.getDisplayValue('name');
33+
}
34+
return sysId;
35+
}
36+
37+
38+
for (var userId in userEvents) {
39+
var events = userEvents[userId];
40+
var totalSeconds = 0;
41+
var startTime = null;
42+
43+
events.forEach(function(evt) {
44+
if (evt.name === 'impersonation.start') {
45+
startTime = new GlideDateTime(evt.time);
46+
} else if (evt.name === 'impersonation.end' && startTime) {
47+
var endTime = new GlideDateTime(evt.time);
48+
totalSeconds += (endTime.getNumericValue() - startTime.getNumericValue()) / 1000;
49+
startTime = null;
50+
}
51+
});
52+
53+
54+
var hours = Math.floor(totalSeconds / 3600);
55+
var minutes = Math.floor((totalSeconds % 3600) / 60);
56+
var seconds = Math.floor(totalSeconds % 60);
57+
58+
gs.info(impersonatorUserID + " impersonated User: " + getUserName(userId) +
59+
" - Total Duration of impersonation is : " + hours + "hrs " + minutes + "min " + seconds + "sec (" + totalSeconds + "sec)");
60+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This script helps to get the impersonator and impersonated user details and duration of impersonation.
2+
3+
4+
Details of Events:
5+
6+
(impersonation.start) which shows that the impersonation has started,
7+
(impersonation.end) shows that the impersonation has ended.
8+
9+
Parm1 contains the userid of user who started the impersonation.
10+
Parm2 contains the userid of user whom we have impersonated.

0 commit comments

Comments
 (0)