[rt-users] Add group membership via CLI

tanguy.lagroy at bt.com tanguy.lagroy at bt.com
Mon Nov 22 05:52:51 EST 2010


Thanks Emmanuel,

It works fine.

Here's my script (for everybody) reading a CSV file (also used by a rtadduser.sh script) :

#!/usr/bin/perl -w
# Utilisation : ./rtgroupmember.pl GROUPNAME
# Attention : Le fichier list.txt doit être correctement rempli

use strict;
use lib "/opt/rt3/lib";
use RT;
use RT::User;
use RT::Interface::CLI;

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

my ($file, $field1, $field2, $field3, $field4, $field5, $field6);
my $user = new RT::User($RT::SystemUser);
my $group = new RT::Group($RT::SystemUser);
my $inputgroup = $ARGV[0];

$group->LoadUserDefinedGroup( $inputgroup );
$file = '/home/ipoc/scripts/list.txt';

open (F, $file) || die ("Could not open $file!");

while ()
{
  ($field1, $field2, $field3, $field4, $field5, $field6) = split(',', $_);
  $user->Load( $field1 );
  $group->AddMember( $user->PrincipalObj->Id );
  #print $user->PrincipalObj->Id ."\n";
}

close (F);


The CSV file contain :
# name,email,password,organization,realname,privileged(0-1)
user.example,user at domain,xxxxx,domain,User EXAMPLE,1

Best Regards,
Tanguy

-----Original Message-----
From: rt-users-bounces at lists.bestpractical.com [mailto:rt-users-bounces at lists.bestpractical.com] On Behalf Of Emmanuel Lacour
Sent: vendredi 19 novembre 2010 20:08
To: rt-users at lists.bestpractical.com
Subject: Re: [rt-users] Add group membership via CLI

On Fri, Nov 19, 2010 at 06:16:08PM +0000, tanguy.lagroy at bt.com wrote:
> Thanks for your help Emmanuel. 
> What's the better ? Perl script or mysql script ?
> 

Perl script using the RT API, sample untested code, without error
checking (get it with ($val, $msg) = method; die $msg unless ( $val ); ):

#!/usr/bin/perl -w

use strict;
use lib "/home/rt/rt/lib";
use RT;
use RT::Interface::CLI qw( CleanEnv );

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

my $User = RT::User->new ( $RT::SystemUser );
$User->Create( Name => NAME, EmailAddress => EMAIL, ... );

my $Group = RT::Group->new ( $RT::SystemUser );
$Group->LoadUserDefinedGroup( GROUPNAME );

$Group->AddMember( $User->PrincipalObj->Id );




More information about the rt-users mailing list