The Script Console is the place for running one-off ad hoc scripts, and for learning and experimenting with the Confluence API.

Either enter your script directly in the browser, or click the File tab, and type the path to a file. The file can be a fully-qualified path name to a .groovy file accessible to the server. If you provide a relative path name, the file is resolved relative to your script roots. By default there is one script root automatically created, which is <confluence_home>/scripts/.

Read more about script roots and script plugins.

Samples

Take one of these samples, and use it as a starting point for your own situation.

Enumerate All Public Spaces

Use the following code to return all the the names of all global spaces:

import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator

def spaceManager = ComponentLocator.getComponent(SpaceManager)
spaceManager.allSpaces.findAll {
    it.isGlobal()
}.collect {
    it.name
}

List Pages and Authors Across Spaces

Use the following code to find all spaces, pages, and authors for all spaces in the Confluence instance. This example is limited to three spaces and ten pages because it’s not practical to hold all this information in a string for large instances.

The following code returns a Map, where the keys are the Space objects and the values are a list of Page objects.

import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.PageManager
import com.atlassian.confluence.spaces.Space
import com.atlassian.confluence.spaces.SpaceManager
import com.atlassian.sal.api.component.ComponentLocator
import groovy.xml.MarkupBuilder


def spaceManager = ComponentLocator.getComponent(SpaceManager)
def pageManager = ComponentLocator.getComponent(PageManager)

def spaces = spaceManager.allSpaces.findAll { it.isGlobal() }.take(3) (1)

Map<Space, List<Page>> pagesInfo = spaces.collectEntries { space ->

    [
        (space): pageManager.getPages(space, true).findAll {
            it instanceof Page
        }.take(10) (2)
    ]
}

The called out lines in the code above perform the followng tasks:

1 Gets the first three spaces in this Confluence instance
2 Gets page information for up to 10 pages for each space

To view this data in a useful way, format it into a table by appending this code to the example code above:

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

builder.table(class: "aui") {
    thead {
        tr {
            th("Space")
            th("Page")
            th("Author")
        }
    }

    tbody {
        pagesInfo.each { space, pages ->

            if (pages.size() > 0) {

                pages.each { page ->
                    tr {
                        th() {
                            b(space.name)
                        }
                        td(page.title)
                        td(page.creator?.name ?: "N/A")
                    }
                }
            }
        }
    }
}

return writer.toString()

When you click Run, your screen should look like the following image:

list space page

Disable Inactive Users

Test all potentially wide-ranging operations like this in a staging environment first.

Use the following code to disable all the users who are inactive for last three months:

import com.atlassian.confluence.security.PermissionManager
import com.atlassian.confluence.security.login.LoginManager
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.UserManager
import groovy.time.TimeCategory

def userManager = ComponentLocator.getComponent(UserManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def loginManager = ComponentLocator.getComponent(LoginManager)
def permissionManager = ComponentLocator.getComponent(PermissionManager)

def threeMonthsBack
use(TimeCategory) {
    threeMonthsBack = 3.months.ago (1)
}
def users = userManager.users (2)

def page = users.currentPage

while (true) {

    page.each { user ->

        if (userAccessor.isDeactivated(user)) {
            return
        }

        log.debug(user)
        def userLoginInfo = loginManager.getLoginInfo(user) (3)
        def lastSuccessfulLoginDate = userLoginInfo?.lastSuccessfulLoginDate

        def toDeactivate = !lastSuccessfulLoginDate || lastSuccessfulLoginDate < threeMonthsBack (4)
        if (toDeactivate) {
            log.warn "Found user: ${user.name} with last login date ${lastSuccessfulLoginDate}, will deactivate"
            return (5)

            if (!permissionManager.isSystemAdministrator(user)) { (6)
                log.info "Deactivating user.."
                userAccessor.deactivateUser(user)
            }
        }
    }

    if (users.onLastPage()) {
        return
    } else {
        users.nextPage()
    }
}

The called out lines in the code above perform the followng tasks:

1 Selects a date three months prior to today
2 Gets all the users
3 Gets login information for a specific user to get the last successful login date
4 Compares the two dates to see whether user is inactive for last 3 months or not, or if they have never logged in
5 Prints the message; you can remove this return statement to actually disable them
6 Disables the user if the user if required and not a system administrator
The script takes a while to execute if you have a large user base.

Add/Remove User from Group

The following example shows how easily you can add or remove users from Confluence groups. In this example, a group called deactivated users is created. You could combine this code with the example code above to move all deactivated users to a group for easy identification.

import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.user.GroupManager

def groupManager = ComponentLocator.getComponent(GroupManager)
def userAccessor = ComponentLocator.getComponent(UserAccessor)

def inactiveGroup = groupManager.getGroup("Deactivated users") ?: groupManager.createGroup("Deactivated users") (1)

def usersToMove = ["anuser", "u100"] (2)

usersToMove.each { username ->

    def user = userAccessor.getUserByName(username)

    if (user) {
        log.info "Removing user ${user.name} from confluence-users"
        def confUsersGroup = groupManager.getGroup("confluence-users")
        groupManager.removeMembership(confUsersGroup, user) (3)

        log.info "Add user ${user.name} to the group: 'Deactivated Users'"
        groupManager.addMembership(inactiveGroup, user) (4)
    }
}

The called out lines in the code above perform the followng tasks:

1 Creates a new user group "Inactive Users" if it does not exists
2 Hard-coded list of users to modify
3 Removes group membership for the user
4 Adds the user to the group we have created in step 1

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.