Update server scripts for user account email in AD based on FirstName and LastName

Source: Internet
Author: User
Tags command line ldap

To add an email address to each user in the ad because of the job requirement. Our company is using Lotus Notes, and unlike the exchange company, the email address field in AD is empty.

However, the good news is that our emails are regular, Firstname.Lastname@mycompany.com in this form. So simply write a script, read the user account FirstName and LastName value, and then merged into an email address, and then write to the ad in the user account.

function

(1) Read FirstName and Lastname, merge into Firstname.Lastname@mycompany.com, and then write back to email in this field

(2) If you encounter FirstName or LastName is empty, skip the account and show the account on the screen.

(3) After all changes are completed, the total number of accounts that have been updated is displayed.

(4) The script automatically operates on all accounts under a given OU.

Usage

(1) Copy the script below to a text file named Updateemail.vbs

(2) Modify your company's domain name, and your company to store the user Account OU, and save

(3) Then open a command line window and type the following command

cscript Updateemail.vbs

"Disclaimer"

This script is I write, please first in the test environment for debugging, all normal after the use. I do not assume any responsibility for data loss and damage caused by the use of the process.

' ======================start========================

' Modify User Email by FirstName.LastName@mycompany.com

Option Explicit

Dim Strou, Strdomain, Stradsou

Dim objOU, objuser, objRootDSE

Dim I

Dim Charfirstname, Charlastname, Charemail

On Error Resume Next

'===========================================

' Type Contrainer here. The Format is "ou=xxx,ou=yyy"

'===========================================

Strou = "Ou=people,ou=allusers"

Set objRootDSE = GetObject ("LDAP://rootDSE")

Strdomain = Objrootdse.get ("defaultNamingContext")

Stradsou = "ldap://" & Strou & "," & Strdomain

' Connect to OU

Set objOU = GetObject (Stradsou)

WScript.Echo "The emails is added into" & Objou.get ("name") & "OU"

Objou.filter= Array ("user")

I=0

WScript.Echo ""

WScript.Echo "The following AD accounts don ' t have FirstName or LastName:"

For each objuser in objOU

Charfirstname = ""

Charlastname = ""

Charfirstname = Objuser.get ("givenname")

Charlastname = Objuser.get ("sn")

If (charfirstname = null) or (Charfirstname = "") or (Charlastname = null) _

Or (Charlastname = "") Then

WScript.Echo objuser.cn

Else

Charemail = charfirstname+ "." +charlastname+ "@mycompany. com"

objUser.Put "Mail", Charemail

Objuser.setinfo

I=i+1

End If

Next

' Display the ' count

WScript.Echo ""

WScript.Echo "Total record changed:" & I

' ===================end=========================

This article is from the "Delxu Live notepad" blog, please be sure to keep this source http://delxu.blog.51cto.com/975660/276039

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.