Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl - Add AD account - check if already exists first

Posted on 2011-09-26
2
Medium Priority
?
215 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-_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
rbride earned 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

688 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