Write to Active Directory profile using VBScript logon script?

skrysiak used Ask the Experts™
At our head office we have several phones and networked PCs. On each desk there is a phone and a PC. The phones' extension numbers range from 10 up to 63; the PCs names are "Dell" appended with a number between 00 and 54 (e.g. "Dell13").

The users hot-desk; that is, John may use computer "Dell13" today while Jane uses "Dell14", but John may use "Dell14" tomorrow while Jane uses "Dell02" in another office, depending on which PC is free to use. The PCs and phones are never moved from the desks, so "Dell13" is always on the desk that can be reached on phone extension 24, and so on.

We're running a Windows 2003 client server with Windows XP PCs on the network. I use a simple VBScript logon script to map drives, printers and suchlike. Is it possible to use the logon script to get the computer name, match this to the desk's phone extension number from an array in the script, and then update the user's Active Directory profile with this extension number?

If this is possible, each time a user logs on their Active Directory profile will be updated to show their current phone extension number. Our administration team can then look-up a user's contact details on their Active Directory profile, making hot-desking users easier to contact by phone.

Unfortunately (due to the order in which our equipment was installed) the phone extension numbers do not correspond to the numbers in the PCs' names, possibly necessitating an array (or external CSV file) to hold these pairs.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
remmett70Network Manager

Quick question.  What to do if a users is logged on at multiple computers?

I'm afraid I can't actually write the script for you, but...

You want to look at Windows Script host, mostly.  Grabbing the computer name is simple enough.  It's the ComputerName string that's part of the WScript.Network object.  So you create the object and then pull the computer name out as seen in the first part of the code snippet.

The second code snippet uses VBS to pull out a user account, then changes a couple phone numbers.  (Make sure to change the OU and DC variables, and make CN the user's account name. - You need an ou= for each organizational unit the user is in (Hopefully they are all in the same one), and read each DC= as having a dot between them )  

Hope this helps.
'Getting the computer name...
Set WshNetwork = WScript.CreateObject("WScript.Network")
MyComputerName = WshNetwork.ComputerName
'For setting the phone number...
Set objUser = GetObject _
objUser.Put "homePhone", "(425) 555-0100"
objUser.Put "pager", "(425) 555-0101"
objUser.Put "mobile", "(425) 555-0102"
objUser.Put "facsimileTelephoneNumber", "(425) 555-0103"   
objUser.Put "ipPhone", "5555"

Open in new window



A user will remain on the same computer for the session, and log off when they are done working.

It is unlikely that a user will be logged on at more than one computer simultaneously (there are rarely enough vacant computers here to do this!), but if they forget to log off on one computer and log on to another, their most recent logon will write their Active Directory profile with the extension number corresponding to the current desk/computer in in use.

Oh crap, I didn't mention, but the active directory code snippet (somewhat shortened) is from http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/usersgroups/users/  Originally.  Check there for the full version of the script and other scripts.
Most Valuable Expert 2012
Top Expert 2014
Hi, something like should work fine as a login script.

You will need to correct your phone numbers for each PC.


Set objPCs = CreateObject("Scripting.Dictionary")
objPCs.Add UCase("DELL00"), "10"
objPCs.Add UCase("DELL01"), "11"
objPCs.Add UCase("DELL02"), "12"
objPCs.Add UCase("DELL03"), "13"
objPCs.Add UCase("DELL04"), "14"
objPCs.Add UCase("DELL05"), "15"
objPCs.Add UCase("DELL06"), "16"
objPCs.Add UCase("DELL07"), "17"
objPCs.Add UCase("DELL08"), "18"
objPCs.Add UCase("DELL09"), "19"
objPCs.Add UCase("DELL10"), "20"
Set objNetwork = CreateObject("WScript.Network")
Set objADSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objADSysInfo.UserName)
strComputer = UCase(objNetwork.ComputerName)
If objPCs.Exists(strComputer) = True Then
	objUser.telephonenumber = objPCs(strComputer)
End If

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial