-1

I would like to create a script that would add an AD account, but check for duplicates first.

Lets just say $first = John $last = Doe

The naming convention is "DoeJ" If "DoeJ" already exists (example: Jane Doe), then it should be last name, first 2 characters of first name.. example: "DoeJo". If "DoeJo" already exists, then last name, first 3 characters of first name, and so on...

How can I accomplish this? I am looking for Sript examples, as I am new to Perl.

Will Fix
  • 105
  • 1
  • 4
  • 16

2 Answers2

3

I just wanted to hack a bit before going to sleep. I'm sorry, I'm to lazy to explain anything right now, but I'm sure you will find pretty much, when looking for anything special on perldoc.

@Everybody: Feel free to comment, modify, extend, beautify, whatever this code if you feel like that.

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %accounts;

while (<DATA>) {
    my ( $id, $first_name, $last_name ) = split;
    my $account_name = $last_name;

    for my $letter ( split ( "" => $first_name ) ) {
        $account_name .= $letter;
        last unless exists $accounts{ $account_name };
    }

    my ( $prefix, $suffix ) = ( $account_name, 0 );
    while ( exists $accounts{ $account_name } ) {
        $account_name = $prefix . ++$suffix;
    }

    $accounts{ $account_name } = "$id: $first_name $last_name";
}

print Dumper(\%accounts);

__DATA__
1 Jane Doe
2 John Doe
3 Joho Doe
4 John Doe
5 John Doe
6 John Doe
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Egga Hartung
  • 1,061
  • 11
  • 23
1

Always check CPAN! CPAN is the Comprehensive Perl Archive Network with hundreds of Perl modules that you can use for specific tasks.

There happens to be a Win32::AD module which looks like it'll do exactly what you want. (You're on Windows. Right? If not, you might have to fall back onto LDAP. Here's an example from Perl Monks.

I guess the question is how much Perl do you know. There's a Perl command called perldoc which can be very useful. For example:

C:> perldoc Win32::AD

Will print out the documentation for the Win32::AD module. If nothing prints, you probably don't have the module installed. There's also another command called cpan that allows you to download and specify the modules in CPAN you want. I don't know if you're on Windows, Linux, etc. and I don't know if you are on Windows whether you're using ActiveState or Strawberry Perl, so I can't give you more help than that.

If you're new to Perl, you might want to look at the Llama Book. This is an excellent beginning Perl book, My biggest complaint is that it doesn't do much in the way of Object Oriented Perl programming which has become extremely important in recent years.

I usually recommend that you go through the Perl 5 tutorials which will cover complex data structures (imagine having an array of hashes that contain other hashes). Then, once you understand how references work, go through the beginning tutorial on Object Oriented Programming.

If you can give us a few more details about your situation (Windows vs. Linux, how familiar you are with Perl, do you know CPAN, etc.) we can help you a bit more.

David W.
  • 105,218
  • 39
  • 216
  • 337