Shell Script for generating NTIDs(Usernames) from the email ids which exists in MSAD Directory

Hi Everyone,

I just need a shell script which automatically gives the list of NT IDs mean the Usernames from the list of email ids. We have around 140 users from AMERICAS,ASIAPACIFIC and EMEA User Directories and we have their email ids.For ex. i have email id called naveen-kumar.dasu@hp.com and this user NT ID is dasun.So i need dasun as output for this email id. So the script by taking their email ids or by connecting to User Directory the script needs to give the output of all the list of NT IDs from these directories. All the User Directories exists with MSAD Configured. Any ones help is highly appreciated.

Regards,
Naveen

It sounds like something an Excel macro could do, and just save it as text to drive your user add script. Sun has ldapsearch, so what OS are we dealing with?

this is quiet simple but you are going to run into problems with people using same or similar names if you do not put in enough checks ... funky sample below will create NTID "thereh" twice ...

susegeek:/tmp # for email in hola.there@hp.com hello.there@hp.com como.esta@hp.com
> do
>     lname=$(echo $email | awk -F"[.@]" '{print $2}')
>     finitial=$(echo $email | awk -F"[.@]" '{print $1}' | awk '{split ($0,a,""); print a[1]}')
>     ntid=$lname$finitial
>     echo "$email = NTID = $ntid"
> done
hola.there@hp.com = NTID = thereh
hello.there@hp.com = NTID = thereh
como.esta@hp.com = NTID = estac
susegeek:/tmp # 

Hi,
Many Thanks for your reply we use windows 7 OS and if i can get this with excel macro that is very much good.Can you help me how can we do this using Excel Macro.

Regards,
Naveen

---------- Post updated at 08:12 AM ---------- Previous update was at 08:07 AM ----------

Hello Just Ice,

Thanks for your reply. Actually i do have the list of the people and their name are unique and there are 140 users. So how could i give these as input for this script.I exported the users to excel.

Regards,
Naveen

export the email column from excel into a .txt file on a directory on the unix server and use that file as the input file ...

input file:

hola.there@hp.com
hello.there@hp.com
como.esta@hp.com

sample script:

#! /bin/ksh
list=$1

if [ $# -ne 1 ]
then
      echo "Not enough arguments. Please rerun script with \"$0 \$listfile\""
      exit 1
else
      if [ ! -s $list ]
      then 
          echo "No valid list file found. Rerun script with readable text file."
          exit 2
      fi
fi

for email in $(< $list)
do
     lname=$(echo $email | awk -F"[.@]" '{print $2}')
     finitial=$(echo $email | awk -F"[.@]" '{print $1}' | awk '{split ($0,a,""); print a[1]}')
     ntid=$lname$finitial
     echo "$email = NTID = $ntid"
done

exit 0

Hello Just Ice,

Thanks Again. Actually the NT IDs differ from email to email. I mean all are not like Lastname and Firstnames first initial.Like for Naveen Kumar Dasu its like dasun.Its not same for all the email ids. So basically we have a LDAP Directory.So we need to connect to the LDAP directory and retrive the NT IDs for the 3 User Directories AMERICAS,ASIAPACIFIC and EMEA. I just want a script which connects to LDAP Directory and gets the NT IDs.

Help on this is highly appreciated.

Regards,
Naveen

Make an Excel Ldap Query - Microsoft Community Excel can do ldap, too.

1 Like

i do not have access to active directory at this time so i cannot verify this ...

assuming you have a column for email and assuming each user account is only seen in one ou ... you may need to fix the ldapsearch command with the correct ou and options ...

for email in $(< $list)
do
     for location in AMERICAS ASIAPACIFIC EMEA
     do
         ntid=$(ldapsearch -t "email=$email" -b "o=$location,o=hp,c=us" uid)
     done
     echo "$email = NTID = $ntid"
done