Skip to content

Commit 6de8946

Browse files
authored
Merge pull request #262 from mevops/log-out-all-active-user-sessions
add Fix Script snippet to log out active User sessions
2 parents 223395f + caef3bc commit 6de8946

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Log out active User sessions across all nodes
2+
## Usage
3+
Can be run as a fix or background script.
4+
5+
The function `applyExcludedUsersFilter` excludes selected users from the session cull. To add users to this exclusion list simply add their username to the array `excluded_users`. The current user is added to the array by default in the example.
6+
7+
Due to the significant impact that logging out all users would have I've included a `live_run` variable. If this is not explicitly set to `true` (the boolean, not the string) then the script will log the actions it would have taken, but not actually affect any user sessions. [(Similar to the PowerShell `WhatIf` concept)][WhatIfArticle]
8+
9+
## Sample Outputs
10+
### Dry Run
11+
```
12+
*** Script: Live run: false: would logout sessions for UserName.With.Session.1
13+
*** Script: Live run: false: would logout sessions for UserName.With.Session.2
14+
*** Script: Live run: false: would logout sessions for UserName.With.Session.3
15+
*** Script: Live run: false: Logged out sessions for the following users:
16+
[
17+
"UserName.With.Session.1",
18+
"UserName.With.Session.2",
19+
"UserName.With.Session.3"
20+
]
21+
```
22+
### Live Run
23+
```
24+
*** Script: Live run: true: Logged out sessions for the following users:
25+
[
26+
"UserName.With.Session.1",
27+
"UserName.With.Session.2",
28+
"UserName.With.Session.3"
29+
]
30+
```
31+
[WhatIfArticle]: https://techcommunity.microsoft.com/t5/itops-talk-blog/powershell-basics-don-t-fear-hitting-enter-with-whatif/ba-p/353579 "PowerShell WhatIf"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
function logOutActiveUserSessions(live_run) {
2+
var usernames_to_logout = getUniqueUsernamesWithActiveSessions();
3+
logoutSessionsForEachUsername(usernames_to_logout, live_run);
4+
gs.info(
5+
'Live run: {0}: Logged out sessions for the following users:\n{1}',
6+
live_run,
7+
JSON.stringify(usernames_to_logout, null, 2)
8+
);
9+
}
10+
function getUniqueUsernamesWithActiveSessions() {
11+
/**
12+
* We use an aggregate so we can groupBy username to return
13+
* a unique list of usernames. A user could have multiple active
14+
* sessions, but the method to end user sessions locks out all
15+
* sessions for that user, so there is no need to run it for
16+
* each session they have.
17+
*/
18+
var active_sessions_agg = new GlideAggregate('sys_user_session');
19+
// Filter to currently valid sessions
20+
active_sessions_agg.addQuery('invalidated', 'NULL');
21+
// Filter out non-user sessions eg a non-interactive system/guest session
22+
active_sessions_agg.addQuery('name', '!=', 'NULL');
23+
// Filter out sessions of current user. You could also exlude any
24+
// users you wanted to this way.
25+
applyExcludedUsersFilter(active_sessions_agg);
26+
active_sessions_agg.groupBy('name');
27+
active_sessions_agg.query();
28+
var unique_usernames = [];
29+
while (active_sessions_agg.next()) {
30+
unique_usernames.push(active_sessions_agg.name.toString());
31+
}
32+
return unique_usernames;
33+
}
34+
function applyExcludedUsersFilter(user_sessions_gr) {
35+
var current_user_user_id = gs.getUserName();
36+
var excluded_users = [
37+
current_user_user_id,
38+
'Special.Person.1',
39+
'Special.Person.2'
40+
];
41+
user_sessions_gr.addQuery(
42+
'name',
43+
'NOT IN',
44+
excluded_users
45+
);
46+
}
47+
function logoutSessionsForEachUsername(usernames, live_run) {
48+
for (var i = 0; i < usernames.length; i++) {
49+
logoutSessionsForUsername(usernames[i], live_run);
50+
}
51+
}
52+
function logoutSessionsForUsername(username, live_run) {
53+
if (live_run === true) {
54+
GlideSessions.lockOutSessionsInAllNodes(username);
55+
return;
56+
}
57+
gs.info(
58+
'Live run: {0}: would logout sessions for {1}',
59+
JSON.stringify(live_run), // Differentiate strings from booleans
60+
username
61+
);
62+
}
63+
64+
var live_run = false;
65+
logOutActiveUserSessions(live_run);

0 commit comments

Comments
 (0)