Import Active-Directory users into Mattermost channels

Most of our Enterprise customers are using a directory service called: Active-Directory, provided by Microsoft. This directory service is often structured in logical groups of objects (computers, users, devices). Especially the users are also organized in organizational units (OUs) within the active directory. With having several thousand users, organized in this fashion, some of our customers wants to add specific groups to Mattermost channels before they even joined.

Mattermost allows the bulk import of data into the server with a bulk_import tool which is part of the central platform command. The detail can be found here: https://docs.mattermost.com/deployment/bulk-loading.html

The tool needs the data to be imported in a JSONL (JSON lines) format which canĀ“t be created out of the box on a Windows Active-Directory server. A PowerShell script seems to be the best option here. I wrote the following script which can be easily used and forked: https://github.com/cjohannsen81/mattermost-ad_import

 

# This PowerShell scripts gets all the AD users from a certain searchbase and converts them into a JSON File 
# that is formatted for the Mattermost Bulk Import Tool (Link).

# Set the distinguished name for the users searchbase
$dn = "CN=Users,DC=matter,DC=most,DC=com"

# Get all users from the AD in a specific searchbase
$users = Get-ADUser -Filter * -SearchBase $dn -Properties sAMAccountName,UserPrincipalName 

# Collect the user input for channel and team (ToDo: Add multiple teams)
$teams = Read-Host 'Teams you want to add the users. Please use comma separation if more then one.'
$channels = Read-Host 'Channels you want to add the users. Please use commas separation if more then one.'

# Manadatory attribute
$version = [pscustomobject]@{
    "type" = "version"
    "version" = 1
} 

$version | ConvertTo-Json -Compress | Out-File 'import.json' -Encoding ascii

# Gets all channels and separate them
$channels = $channels.Split(',')

# Get users and convert them into a JSON
foreach ($i in $users)
{
    $userName = $i.sAMAccountName
    if ($userName) 
    { $email = $i.UserPrincipalName
    if ($email) 
    { $user = [pscustomobject]@{
        "type" = "user"
        "user" = [ordered]@{
            "username" = "$userName"
            "email" = "$email"
            "auth_service" = "ldap"
            "auth_data" = "$userName"
            "teams" = @(
                @{            
                    "name" = "$teams"
                    "channels" = @(
                        foreach($i in $channels){
                            @{"name" = $i}
                        }

                    )
                }
            )

        }
    } 
    } else {"$username has no email address."}
} else {"No account name."}
$user | ConvertTo-Json -Depth 5 -Compress | Out-File 'import.json' -Encoding ascii -Append
}

 

There are different important lines that can be changed to solve your use-case. First thing is the search base which can be easily pointed to another OU or CN. The other thing to check is the search filter as you may just want to add specific user to specific channels, like all HR user to the “Onboarding” and “Interviews” channel. Right now the script is written to support one team and many channels which can be changed by adding another loop inside.

The result of the script is a JSONL file that can be used by the bulk import command on the Mattermost server. Please use the –validate switch before uploading the users and keep in mind that the channel names have to be lowercase on the PowerShell import!

 

 

When all users are imported they should be already part of the channels you’ve added them with the script and import (testchannel, secondone).

 

 

 

Leave a Reply