RT Command Line adding users

A while a go I needed to add a whole raft of users (about 400 or so 🙂 ) to our work RT system. Doing this manually had little appeal so I was looking for a scriptable way of doing it.

I found a script on the wiki ( wiki.bestpractical.com ) but it only supported populating RT from /etc/passwd entires. So close and yet… so with a pointer or two from Bunty, my handy online Perl guru friend 🙂 I managed to use the existing script and bodge it to allow command line arguements 🙂


#!/usr/bin/perl -w
#
# rtadduser: add a local user to RT
# David Maze
# $Id$
# Poorly modified by Tig to accept commandline arguments
# Known issues, will silently fail if a user already exists

BEGIN{
push @INC,"/usr/share/request-tracker3.6/lib/";
}
use lib "/usr/uns/lib";
use strict;
use English;
use RT::Interface::CLI qw(CleanEnv);
use RT::User;
use Getopt::Long;

my ($user, $passwd, $email);

GetOptions (
"user=s" => \$user,
"email=s" => \$email,
"passwd=s" => \$passwd
);

CleanEnv();
RT::LoadConfig();
RT::Init();

print("Creating user with the following variables\n");
print("user : $user\n");
print("e-mail : $email\n");
print("password : $passwd\n");

my $UserObj = new RT::User(RT::SystemUser);
$UserObj->Create(Name => $user,
EmailAddress => "$email",
Privileged => 1,
Password => $passwd
);

print("If there was no error it possibly worked!\n");

To use this you will need Getopt::Long installed, usage is :

perl rtadduser -user=username -email=user@foo.com -passwd=secretpassword

Oh and you will need to poke line 10 (the one with the path to RT.pm) for your distro.

I will put this onto the wiki soon, just needed to dump it somewhere indexable before I forget about it 🙂

2 thoughts on “RT Command Line adding users

  1. Please can you tell me if there are any script or command tool to assign users created to a group ?

  2. mezgani: Just add this to the code at the end of the example and change YOUR-GROUP-NAME to the group you want to add the user to.

    #snip---------------------------------------------------
    my $add_group = RT::Group->new($UserObj->CurrentUser);
    $add_group->LoadUserDefinedGroup('YOUR-GROUP-NAME');
    unless ($add_group->id) {
    print("Could not load Everyone group on user creation.");
    }

    my ($everyone_id, $everyone_msg) = $add_group->_AddMember( InsideTransaction => 1, PrincipalId => $UserObj->PrincipalId);
    unless ($everyone_id) {
    print("Could not add user to Everyone group on user creation.");
    print($everyone_msg);
    }
    #snap---------------------------------------------------

Comments are closed.