Solved

Perl - Add AD account - check if already exists first

Posted on 2011-09-26
2
209 Views
Last Modified: 2012-05-12
Please don't vote down my question. If you don't like it, comment on it & tell me that, I will remove it right away..

I am trying to find out the login for line #25, which says "elsif ($line =~ /$accountname/i)" That does not work. If likusto and likust exist, it will try to make an account for likust.

I also need help with logic of counting numbers at/around line 43 That does not work. The line says "if($line =~ /$accountname$num\s+/i)"

Let me know what I can do to correctly check if it exists & choose the correct new account name if not.

Here is how To Likus accounts are created: likust likusto likust1 likust2

use net::ldaps;
$last = "likus";
$first = "tim";
@accounts = `net user /domain`;
$firstcount = $first =~ tr/[a-zA-Z1-9]//;
print "first name has $firstcount characters\n";
$count = "1";
$char = substr( $first, $i , $count );
$accountname = $last . $char;
print "$accountname is the first account name\n";
$counter = "0";
$counted = $firstcount;
$char = substr( $first, $i , 1 );
$accountname = $last . $char;
$foundcount = "0";
#@accounts = /\s/\n/i;
while ($counter <= $counted) {
foreach $line (@accounts) {
$canuse = "cocho";
if($line =~ /$accountname\s+/i)
		{
		#print "account name exists $accountname\n";
		$foundcount ++;
		if ($counter =~ $firstcount){print "found full first name at #$counter vs letter count $firstcount, must try numbers\n";}
	} elsif ($line =~ /$accountname/i){print "found spot for accountname at $accountname\n"; &makeaccount;}
}
#print "loop #$counter vs letter count $firstcount\n";
$char = substr( $first, $i , ++$count );
$accountname = $last . $char;
$counter ++;
#print "loop #$counter\n";
#last unless exists $accountname{ @accounts };
}
#print "found $foundcount character\(s\) of first name\n":
$foundcount ++;
$char = substr( $first, $i , $foundcount );
$accountname = $last . $char;
print "account name is currently $accountname before number puller\n";
$foundnum = "not_found";
$num = "1";
while ($foundnum eq "not_found"){
foreach $line (@accounts) {
if($line =~ /$accountname$num\s+/i)
		{
		    print "found $accountname$num\n";
		} elsif ($line ne /$accountname$num/i)
		    {
		    $foundnum = "no_siesto\n";
		    &makeaccountnum;
		    }
$num ++;
print "not found $accountname$num";
}
}
sub makeaccount {
print "this is will the LDAP script will add $accountname";
exit 0; #exit main
}
sub makeaccountnum {
print "this is will the LDAP script will add $accountname$num";
exit 0; #exit main
}

Open in new window

0
Comment
Question by:_-W-_
2 Comments
 
LVL 4

Accepted Solution

by:
rbride earned 500 total points
ID: 36708234
The problem on line 20/25 is that you need to match the whole word.

what you want is a regular expression that matches on a word boundary:
How about this one:
$line =~ /\b$accountname\b/i

Ditto for line #43

Also, how about using an associative array (hash) to store your accounts. It would make it a bit easier and efficient to process:

my %accountHash = {};
# Define an account
$accountHash{$accountname} = 0;
# Check that an account exists:
if (defined $accountHash{$accountName}) ...
# Get count for $accountName
$num = $accountHash{$accountname}++;
0
 
LVL 3

Author Closing Comment

by:_-W-_
ID: 36711222
This worked very well! Thank you for the code sample!
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

OfficeMate Freezes on login or does not load after login credentials are input.
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question