Solved

Perl - Add AD account - check if already exists first

Posted on 2011-09-26
2
207 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
Comment Utility
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-_
Comment Utility
This worked very well! Thank you for the code sample!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now