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
Solved

Perl - Add AD account - check if already exists first

Posted on 2011-09-26
2
210 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

856 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