Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change


by Marza (Vicar)
on Apr 12, 2002 at 21:27 UTC ( #158680=sourcecode: print w/replies, xml ) Need Help??
Category: NT Admin
Author/Contact Info Marza

This script as the name implies is used to add ids or a list of IDs to a particular domain/AD group.

There is a logmsg subroutine which can be used for automation or history purposes. If it is not required, then comment it out.

I have not tested AD yet as we don't have an AD setup.

Finally, feel free to use it and mod it as you need. If you think something could be done better, please let me know.

use strict;
use warnings;
use Win32::NetAdmin;

my ($group, @names);
unless ( ($group, @names) = @ARGV ) {
    print "addtogrp <GROUP> <Input file> or <User> <User> ... \n\n";
    die ("   Use double quotes for Multiworded Groups: ie \"Domain Adm

my $domain = Win32::DomainName or die "Unable to obtain the domain nam

my $DC;
Win32::NetAdmin::GetDomainController( '', $domain, $DC ) or 
            die "Unable to locate a Domain Controller\n";
# Do we have an input file?.
if (open(DAT, "$names[0]")) {
    @names = <DAT>;
    close DAT;
# Try and obtain the comment for the group.  If you can't it is either
+ an
# invalid entry or a security problem
my $comment;
unless (Win32::NetAdmin::GroupGetAttributes($DC, $group, $comment)) {
    &logmsg("Invalid group name $group. Error: $^E\n");
    die (" Invalid Group name $group. Error: $^E\n");

foreach my $user (@names) {
    chomp $user;
    print "Processing $user\n";
    # Does the id exist?
    unless (Win32::NetAdmin::UsersExist($DC, $user)) {
        &logmsg("The account $user does not exist.\n");
        print " The account $user does not exist.\n";
    # Verify the id is not already in the group.  If not; add it.
    if (Win32::NetAdmin::GroupIsMember($DC, $group, $user)) {
        &logmsg("$user is already a member of $group\n");
        print " $user is already a member of $group\n";
    } else {
        unless (Win32::NetAdmin::GroupAddUsers($DC, $group, $user)) {
            &logmsg("Unable to add $user to $group due to error $^E ! 
            print " Unable to add $user to $group due to error $^E ! \
        } else {
            &logmsg("Added $user to $group\n");
            print " Added $user to $group\n";
# Just a simple log routine.  Mainly for cases of automation in the fu
sub logmsg {
    my $subject = shift;
    my $log = "addtogrp.log";
    my ($sec,$min,$hour,$mday,$mon,$year) = localtime(time());

    my $date = sprintf("%02d\/%02d\/%04d",$mon+1,$mday,$year+1900);
    my $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);

    unless (open(LOGFILE, ">>$log")) {
        die "Unable to open log file $log";
    print LOGFILE ("$date  $time - $subject\n");
    close LOGFILE;
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://158680]
[ambrus]: and try to leave everything else to automated systems without checking how what they write came out format-wise, and for those people, discounting the part about journals with a specific format above,
[ambrus]: just blindly recommending to use LaTeX is a bad idea now.
[Discipulus]: I havery limited needs in such field. Sometimes (for children party invitations) i draw something by hand and I scan it to include into an ms doc.
Discipulus havery can be a good neologism for: have very..;=)
[ambrus]: Discipulus: sure, it's just that, look, some people (including me) have such unreadable and slow handwriting that writing an article or slides by hand would be a very bad idea.
[choroba]: it might bery useful for other verbs, too
[ambrus]: (and I have a need for at least decent formatting too)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-09-26 11:22 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.