|
| 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 | +} |
0 commit comments