Add group membership via CLI

Dear Community,

I currently setup script to create users from CSV file via CLI, and also add the users to and existing group.

The first part is working fine with command :
/opt/rt3/bin/rt create -t users add Name=$name EmailAddress=$email Password=$pass Privileged=$priv Organization=$orga RealName=“"$realname"”

But the command to add group member doesn’t work :
/opt/rt3/bin/rt edit group/mygroup add Members=$name

I try the several syntax, but none works:

  •   Members=$email
    
  •   Members="realname <$email>"
    
  •   Members=user/id
    
  •   etc
    

The output says :

members: Unkown field

group xxxx updated

Can anyone help me, please ?

Tanguy LAGROY
Département NSS/IPOC
BT Services
Email : tanguy.lagroy@bt.com
Tel : +33 (0)1 78 41 15 39

Dear Community,

I currently setup script to create users from CSV file via CLI, and also add the users to and existing group.

The first part is working fine with command :
/opt/rt3/bin/rt create -t users add Name=$name EmailAddress=$email Password=$pass Privileged=$priv Organization=$orga RealName=“"$realname"”

But the command to add group member doesn’t work :
/opt/rt3/bin/rt edit group/mygroup add Members=$name

I try the several syntax, but none works:

  •   Members=$email
    
  •   Members="realname <$email>"
    
  •   Members=user/id
    
  •   etc
    

The output says :

members: Unkown field

group xxxx updated

Can anyone help me, please ?

well reading this part of rt code, it doesn’t seems you can update groups members
with rt cli (only display members) :frowning:

I would do this using a custom script :wink:

Thanks for your help Emmanuel.
What’s the better ? Perl script or mysql script ?

TanguyLe 19 nov. 2010 à 18:38, “Emmanuel Lacour” elacour@easter-eggs.com a écrit :

On Fri, Nov 19, 2010 at 08:37:22AM +0000, tanguy.lagroy@bt.com wrote:

Dear Community,

I currently setup script to create users from CSV file via CLI, and also add the users to and existing group.

The first part is working fine with command :
/opt/rt3/bin/rt create -t users add Name=$name EmailAddress=$email Password=$pass Privileged=$priv Organization=$orga RealName=“"$realname"”

But the command to add group member doesn’t work :
/opt/rt3/bin/rt edit group/mygroup add Members=$name

I try the several syntax, but none works:

  •   Members=$email
    
  •   Members="realname <$email>"
    
  •   Members=user/id
    
  •   etc
    

The output says :

members: Unkown field

group xxxx updated

Can anyone help me, please ?

well reading this part of rt code, it doesn’t seems you can update groups members
with rt cli (only display members) :frowning:

I would do this using a custom script :wink:

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 );

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@domain,xxxxx,domain,User EXAMPLE,1

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

On Fri, Nov 19, 2010 at 06:16:08PM +0000, tanguy.lagroy@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 );

I followed the script from Emmanuel and Tanguy from Nov 20, 2010 with this
subject to add a list of members to a group and it is failing. I made some
changes to match page 99 of the RT Essentials book.

#!/usr/bin/perl -w
use strict;
use lib “/opt/rt3/lib”;

use RT;
use RT::Interface::CLI qw( CleanEnv GetCurrentUser );

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

my $user = RT::User->new( $RT::SystemUser );
my $group = RT::Group->new( $RT::SystemUser );
my $inputgroup = $ARGV[0];

$group->LoadUserDefinedGroup( $inputgroup );

open FILE, “user_list.txt” or die $!;

while ()
{
$user->Load( $_ );
$group->AddMember( $user->PrincipalObj->Id );
}

close FILE;

$ ./rtgroupmember.pl MYGROUP
[Tue May 14 20:34:50 2013] [error]: Couldn’t get principal for not loaded
object (/opt/rt3/lib/RT/User_Overlay.pm:1154)
[Tue May 14 20:34:50 2013] [crit]: Can’t call method “Id” on an undefined
value at ./rtgroupmember.pl line 28, line 1. (/opt/rt3/lib/RT.pm:377)
Can’t call method “Id” on an undefined value at ./rtgroupmember.pl line 28,
line 1.

So I changed the RT::User->new with RT::User_Overlay->new and I got a
different error saying cannot use object method new.

Don’t speak much OOO perl yet.

Any suggestion would be appreciated.

Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

I am trying to add a list of UIDs into one of my group and it is failing.

I am running the script that was posted with this subject on Nov 22, 2010
by tanhuy.langloy at bt. I am running it on RT 3.8.2

#!/usr/bin/perl

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 $user = new RT::User($RT::SystemUser);
my $group = new RT::Group($RT::SystemUser);
my $inputgroup = $ARGV[0];

$group->LoadUserDefinedGroup( $inputgroup );

open FILE, “usernames.txt” or die $!;

while ()
{
$user->Load( $_ );
$group->AddMember( $user->PrincipalObj->Id );
#print $user->PrincipalObj->Id .“\n”;
}

close FILE;

$ ./rtgroupmember.pl MYGROUP

[Wed May 15 01:23:15 2013] [error]: Couldn’t get principal for not loaded
object (/opt/rt3/lib/RT/User_Overlay.pm:1154)
[Wed May 15 01:23:15 2013] [crit]: Can’t call method “Id” on an undefined
value at ./rtgroupmember.pl line 26, line 1. (/opt/rt3/lib/RT.pm:377)
Can’t call method “Id” on an undefined value at ./rtgroupmember.pl line 26,
line 1

Any suggestion would be appreciated

Thanks

Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

oops… sorry for posting twice. I thought it did not make it to the ML
first timeOn Tue, May 14, 2013 at 9:28 PM, Asif Iqbal vadud3@gmail.com wrote:

I am trying to add a list of UIDs into one of my group and it is failing.

I am running the script that was posted with this subject on Nov 22, 2010
by tanhuy.langloy at bt. I am running it on RT 3.8.2

#!/usr/bin/perl

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 $user = new RT::User($RT::SystemUser);
my $group = new RT::Group($RT::SystemUser);
my $inputgroup = $ARGV[0];

$group->LoadUserDefinedGroup( $inputgroup );

open FILE, “usernames.txt” or die $!;

while ()
{
$user->Load( $_ );
$group->AddMember( $user->PrincipalObj->Id );
#print $user->PrincipalObj->Id .“\n”;
}

close FILE;

$ ./rtgroupmember.pl MYGROUP

[Wed May 15 01:23:15 2013] [error]: Couldn’t get principal for not loaded
object (/opt/rt3/lib/RT/User_Overlay.pm:1154)
[Wed May 15 01:23:15 2013] [crit]: Can’t call method “Id” on an undefined
value at ./rtgroupmember.pl line 26, line 1.
(/opt/rt3/lib/RT.pm:377)
Can’t call method “Id” on an undefined value at ./rtgroupmember.pl line
26, line 1

Any suggestion would be appreciated

Thanks


Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

while ()
{
$user->Load( $_ );
$group->AddMember( $user->PrincipalObj->Id );
}

I found the hint from another email with subject CLI Question - Add Users
to group per Scrip from Feb 28, 2007 from Joe Casadonte at oracle.com.

I removed the PrincipalObj and it is working now. So from the script this
is part that I needed to fix.

while ()
{
chop($);
$user->Load( $
);
$group->AddMember( $user->id );
}

I also had to add more use lib for the extensions to be loaded or failing
to run

use lib ‘/opt/rt3/local/plugins/RT-Authen-ExternalAuth/lib’;
use lib ‘/opt/rt3/local/plugins/RT-Extension-CommandByMail/lib’;

Asif Iqbal
PGP Key: 0xE62693C5 KeyServer: pgp.mit.edu
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?