Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,367,189
Community Members
 
Community Events
168
Community Groups

Need script to remove 60-day inactive users from jira-users group

I am not a strong script writer so I am looking for someone to provide me a script that I can run each night to look for users who have not logged in in the last 60 days and remove them from the jira-users group so they no longer take a license up.  I have ScriptRunner. I have found other scripts out there to de-activate users, but those scripts don't work for me because of our AD/LDAP setup.  So I just want to remove the inactive from the jira-users group.  I would be SO GREATFUL for someone to provide me a script to do this by username.  We have Jira 8.19.

3 answers

1 accepted

0 votes
Answer accepted

@Mirco Fabbri Provided the answer in a different write up:   https://community.atlassian.com/t5/Adaptavist-questions/Scriptrunner-deactivate-inactive-users-script-needs-to-be/qaq-p/914110?utm_source=atlcomm&utm_medium=email&utm_campaign=immediate_general_reply&utm_content=topic 

 Here is the script he provided...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.login.LoginManager
import java.text.SimpleDateFormat;
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption


def userUtil = ComponentAccessor.userUtil
def userManager = ComponentAccessor.userManager
def groupManager = ComponentAccessor.getGroupManager();
def loginManager = ComponentAccessor.getComponentOfType(LoginManager.class)
CrowdService crowdService = ComponentAccessor.crowdService
def maxDays = 90 // maximum number of days you will allow

 

def excludedGroups = [ // only externals
"jira-administrators",
"jira-system-administrators"
]

 

long count = 0;
def list
def lastlogin
def output = "Display Name; Username; Email; Last Login <br>"
def today = new Date()
def lastLogin
SimpleDateFormat df = new SimpleDateFormat("dd.MM.yy hh:mm")
userManager.getUsers().each { // loop through every user
list = true;
groupManager.getGroupNamesForUser(it).each { // loop through his groups
if (excludedGroups.contains(it)) { // if at least one group is in the list, add user
list = false;
}
}
if(list && it.isActive()) { //check if user is active & supposed to be listed
UserWithAttributes user = crowdService.getUserWithAttributes(it.getName())
Long lastLoginTime = loginManager.getLoginInfo(it.username).getLastLoginTime()
lastlogin = "Never Logged In"
if(lastLoginTime != null) {
Date date = new Date(lastLoginTime)
lastlogin = df.format(date)
}
if(lastLoginTime != null && groovy.time.TimeCategory.minus(new Date(), new Date(lastLoginTime)).days > maxDays) {
userUtil.removeUserFromGroup(ComponentAccessor.groupManager.getGroup("jira-software-user"), it)
output += "${it.getDisplayName()}; ${it.getUsername()}; ${it.getEmailAddress()}; ${lastlogin};<br>"
count++
}
}
}
output

Hi @kdickason 
Glad you asked this,


you can consider using the miniOrange Bulk User management plugin for JIRA.
There, you can find a variety of features regarding user management such as deactivating inactive users (the same requirement you have), you can also perform bulk operations on the users (which native Jira user management does not support) You can import or export users and auto deactivate users who have never logged in. On top of that, if you need
any custom features, we also provide tailor-made solutions just for your requirements.
Check out the plugin here. https://marketplace.atlassian.com/apps/1224360/mo-bulk-user-management-deactivate-inactive-users-for-jira?hosting=datacenter&tab=overview

1 vote
Fazila Ashraf Community Leader Oct 26, 2021

Hi @kdickason 

You can refer to https://library.adaptavist.com/entity/remove-specified-users-from-a-group and tweek it for removing the users from the group jira-users 

To get the inactive users, as mentioned in the page, you can refer to https://library.adaptavist.com/entity/deactivate-idle-users to get the list of users with some more tweeks.

Hi @Fazila Ashraf ,

Is it possible to get inactive users in Jira Cloud using the script Runner addon?

Regards,

Prasad E

Fazila Ashraf Community Leader Jan 17, 2022

Haven't done that in cloud but a quick search got me this free plugin that could help with this use case https://marketplace.atlassian.com/apps/1224410/manage-users-for-jira-cloud?tab=overview&hosting=cloud

 

Hope it helps... Let me know

Hi @Prasad Eda ,

Did you find/created the script to inactive users in Jira Cloud using the script Runner addon? 

I am also looking for the same in Jira cloud using Script Runner.

Thanks,

Alok

Suggest an answer

Log in or Sign up to answer