Link to home
Start Free TrialLog in
Avatar of _-W-_
_-W-_Flag for United States of America

asked on

Perl - Add AD account - check if already exists first

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

ASKER CERTIFIED SOLUTION
Avatar of rbride
rbride

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _-W-_

ASKER

This worked very well! Thank you for the code sample!