Does anyone know of a way to change the phone format in AD for user accounts?

I'm a greenhorn on scripting and can use the help!!

Currently, most of our users have the following phone format: ###/###-####

We'd like to change that to: (###) ###-####. Is this possible at all? If
so, how would I go about scripting this?

TIA!!!

Re: VBScript to change phone format in AD.... by Trevor

Trevor
Thu May 17 21:41:38 CDT 2007

kage13 wrote:
> Does anyone know of a way to change the phone format in AD for user accounts?
>
> I'm a greenhorn on scripting and can use the help!!
>
> Currently, most of our users have the following phone format: ###/###-####
>
> We'd like to change that to: (###) ###-####. Is this possible at all? If
> so, how would I go about scripting this?
>
> TIA!!!

You can use string functions to manipulate the phone data, but then
you've gotta call the SetInfo() method on the DS object to serialize the
data once you've changed it. As far as string manipulation goes, you
could do something like this in one line (pseudo-code):

dsobj.phone = "(" & replace(dsobj.phone, "/", ") ")
dsobj.SetInfo()

Hope this gives you a boost.

Trevor Sullivan
MCP
http://develnet.blogspot.com

Re: VBScript to change phone format in AD.... by Richard

Richard
Thu May 17 21:53:56 CDT 2007

kage13 wrote:

> Does anyone know of a way to change the phone format in AD for user
> accounts?
>
> I'm a greenhorn on scripting and can use the help!!
>
> Currently, most of our users have the following phone format: ###/###-####
>
> We'd like to change that to: (###) ###-####. Is this possible at all? If
> so, how would I go about scripting this?

A VBScript program to modify the format of telephoneNumber attribute for all
users in the domain follows. This uses ADO to retrieve the distinguishedName
and telephoneNumber of all users with any value assigned to the
telephoneNumber attribute. ADO cannot modify objects in AD, so the
distinguishedName is used to bind to each user object. The phone number is
modified and the changes saved. I assume the exact formats you gave, so I
used the following to modify the phone number:

strPhone = "(" & Replace(strPhone, "/", ") ")

Others may suggest other methods. This simply appends "(" to the beginning
and replaces every instance of "/" with ") ". If the format is not as
expected, the result might be strange.
============
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN
Dim strPhone, objUser

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects that have a value assigned
' to the telephoneNumber attribute.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(telephoneNumber=*)"

' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,telephoneNumber"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strDN = adoRecordset.Fields("distinguishedName").Value
strPhone = adoRecordset.Fields("telephoneNumber").Value
' Modify format of phone number.
strPhone = "(" & Replace(strPhone, "/", ") ")
' Bind to user object.
Set objUser = GetObject("LDAP://" & strDN)
' Assign new phone number.
objUser.telephoneNumber = strPhone
' Save changes.
objUser.SetInfo
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: VBScript to change phone format in AD.... by Trevor

Trevor
Thu May 17 22:01:26 CDT 2007

Richard Mueller [MVP] wrote:
> kage13 wrote:
>
>> Does anyone know of a way to change the phone format in AD for user
>> accounts?
>>
>> I'm a greenhorn on scripting and can use the help!!
>>
>> Currently, most of our users have the following phone format: ###/###-####
>>
>> We'd like to change that to: (###) ###-####. Is this possible at all? If
>> so, how would I go about scripting this?
>
> A VBScript program to modify the format of telephoneNumber attribute for all
> users in the domain follows. This uses ADO to retrieve the distinguishedName
> and telephoneNumber of all users with any value assigned to the
> telephoneNumber attribute. ADO cannot modify objects in AD, so the
> distinguishedName is used to bind to each user object. The phone number is
> modified and the changes saved. I assume the exact formats you gave, so I
> used the following to modify the phone number:
>
> strPhone = "(" & Replace(strPhone, "/", ") ")
>
> Others may suggest other methods. This simply appends "(" to the beginning
> and replaces every instance of "/" with ") ". If the format is not as
> expected, the result might be strange.
> ============
> Option Explicit
> Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
> Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN
> Dim strPhone, objUser
>
> ' Setup ADO objects.
> Set adoCommand = CreateObject("ADODB.Command")
> Set adoConnection = CreateObject("ADODB.Connection")
> adoConnection.Provider = "ADsDSOObject"
> adoConnection.Open "Active Directory Provider"
> adoCommand.ActiveConnection = adoConnection
>
> ' Search entire Active Directory domain.
> Set objRootDSE = GetObject("LDAP://RootDSE")
> strDNSDomain = objRootDSE.Get("defaultNamingContext")
> strBase = "<LDAP://" & strDNSDomain & ">"
>
> ' Filter on user objects that have a value assigned
> ' to the telephoneNumber attribute.
> strFilter = "(&(objectCategory=person)(objectClass=user)" _
> & "(telephoneNumber=*)"
>
> ' Comma delimited list of attribute values to retrieve.
> strAttributes = "distinguishedName,telephoneNumber"
>
> ' Construct the LDAP syntax query.
> strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
> adoCommand.CommandText = strQuery
> adoCommand.Properties("Page Size") = 100
> adoCommand.Properties("Timeout") = 30
> adoCommand.Properties("Cache Results") = False
>
> ' Run the query.
> Set adoRecordset = adoCommand.Execute
>
> ' Enumerate the resulting recordset.
> Do Until adoRecordset.EOF
> ' Retrieve values.
> strDN = adoRecordset.Fields("distinguishedName").Value
> strPhone = adoRecordset.Fields("telephoneNumber").Value
> ' Modify format of phone number.
> strPhone = "(" & Replace(strPhone, "/", ") ")
> ' Bind to user object.
> Set objUser = GetObject("LDAP://" & strDN)
> ' Assign new phone number.
> objUser.telephoneNumber = strPhone
> ' Save changes.
> objUser.SetInfo
> ' Move to the next record in the recordset.
> adoRecordset.MoveNext
> Loop
>
> ' Clean up.
> adoRecordset.Close
> adoConnection.Close
>

There you go. His reply is a lot more comprehensive than mine. Same
overall concept though.

Trevor Sullivan
MCP
http://develnet.blogspot.com

Re: VBScript to change phone format in AD.... by kage13

kage13
Fri May 18 07:24:01 CDT 2007

AWSOME! Thanks Richard and Trevor, much appreciated. Now if I can only
start thinking like a programmer then I may be able to pickup VBS a little
quicker than I am.

"Richard Mueller [MVP]" wrote:

> kage13 wrote:
>
> > Does anyone know of a way to change the phone format in AD for user
> > accounts?
> >
> > I'm a greenhorn on scripting and can use the help!!
> >
> > Currently, most of our users have the following phone format: ###/###-####
> >
> > We'd like to change that to: (###) ###-####. Is this possible at all? If
> > so, how would I go about scripting this?
>
> A VBScript program to modify the format of telephoneNumber attribute for all
> users in the domain follows. This uses ADO to retrieve the distinguishedName
> and telephoneNumber of all users with any value assigned to the
> telephoneNumber attribute. ADO cannot modify objects in AD, so the
> distinguishedName is used to bind to each user object. The phone number is
> modified and the changes saved. I assume the exact formats you gave, so I
> used the following to modify the phone number:
>
> strPhone = "(" & Replace(strPhone, "/", ") ")
>
> Others may suggest other methods. This simply appends "(" to the beginning
> and replaces every instance of "/" with ") ". If the format is not as
> expected, the result might be strange.
> ============
> Option Explicit
> Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
> Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN
> Dim strPhone, objUser
>
> ' Setup ADO objects.
> Set adoCommand = CreateObject("ADODB.Command")
> Set adoConnection = CreateObject("ADODB.Connection")
> adoConnection.Provider = "ADsDSOObject"
> adoConnection.Open "Active Directory Provider"
> adoCommand.ActiveConnection = adoConnection
>
> ' Search entire Active Directory domain.
> Set objRootDSE = GetObject("LDAP://RootDSE")
> strDNSDomain = objRootDSE.Get("defaultNamingContext")
> strBase = "<LDAP://" & strDNSDomain & ">"
>
> ' Filter on user objects that have a value assigned
> ' to the telephoneNumber attribute.
> strFilter = "(&(objectCategory=person)(objectClass=user)" _
> & "(telephoneNumber=*)"
>
> ' Comma delimited list of attribute values to retrieve.
> strAttributes = "distinguishedName,telephoneNumber"
>
> ' Construct the LDAP syntax query.
> strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
> adoCommand.CommandText = strQuery
> adoCommand.Properties("Page Size") = 100
> adoCommand.Properties("Timeout") = 30
> adoCommand.Properties("Cache Results") = False
>
> ' Run the query.
> Set adoRecordset = adoCommand.Execute
>
> ' Enumerate the resulting recordset.
> Do Until adoRecordset.EOF
> ' Retrieve values.
> strDN = adoRecordset.Fields("distinguishedName").Value
> strPhone = adoRecordset.Fields("telephoneNumber").Value
> ' Modify format of phone number.
> strPhone = "(" & Replace(strPhone, "/", ") ")
> ' Bind to user object.
> Set objUser = GetObject("LDAP://" & strDN)
> ' Assign new phone number.
> objUser.telephoneNumber = strPhone
> ' Save changes.
> objUser.SetInfo
> ' Move to the next record in the recordset.
> adoRecordset.MoveNext
> Loop
>
> ' Clean up.
> adoRecordset.Close
> adoConnection.Close
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>

Re: VBScript to change phone format in AD.... by kage13

kage13
Fri May 18 07:59:00 CDT 2007

Richard,

When running the code provided I'm getting an 80040E14 error at the
following line:
Set adoRecordset = adoCommand.Execute

In researching the error, I've found that it may represent a syntax error
but cannot find anything that jumps out.

Ideas?

TIA.

"Richard Mueller [MVP]" wrote:

> kage13 wrote:
>
> > Does anyone know of a way to change the phone format in AD for user
> > accounts?
> >
> > I'm a greenhorn on scripting and can use the help!!
> >
> > Currently, most of our users have the following phone format: ###/###-####
> >
> > We'd like to change that to: (###) ###-####. Is this possible at all? If
> > so, how would I go about scripting this?
>
> A VBScript program to modify the format of telephoneNumber attribute for all
> users in the domain follows. This uses ADO to retrieve the distinguishedName
> and telephoneNumber of all users with any value assigned to the
> telephoneNumber attribute. ADO cannot modify objects in AD, so the
> distinguishedName is used to bind to each user object. The phone number is
> modified and the changes saved. I assume the exact formats you gave, so I
> used the following to modify the phone number:
>
> strPhone = "(" & Replace(strPhone, "/", ") ")
>
> Others may suggest other methods. This simply appends "(" to the beginning
> and replaces every instance of "/" with ") ". If the format is not as
> expected, the result might be strange.
> ============
> Option Explicit
> Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
> Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strDN
> Dim strPhone, objUser
>
> ' Setup ADO objects.
> Set adoCommand = CreateObject("ADODB.Command")
> Set adoConnection = CreateObject("ADODB.Connection")
> adoConnection.Provider = "ADsDSOObject"
> adoConnection.Open "Active Directory Provider"
> adoCommand.ActiveConnection = adoConnection
>
> ' Search entire Active Directory domain.
> Set objRootDSE = GetObject("LDAP://RootDSE")
> strDNSDomain = objRootDSE.Get("defaultNamingContext")
> strBase = "<LDAP://" & strDNSDomain & ">"
>
> ' Filter on user objects that have a value assigned
> ' to the telephoneNumber attribute.
> strFilter = "(&(objectCategory=person)(objectClass=user)" _
> & "(telephoneNumber=*)"
>
> ' Comma delimited list of attribute values to retrieve.
> strAttributes = "distinguishedName,telephoneNumber"
>
> ' Construct the LDAP syntax query.
> strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
> adoCommand.CommandText = strQuery
> adoCommand.Properties("Page Size") = 100
> adoCommand.Properties("Timeout") = 30
> adoCommand.Properties("Cache Results") = False
>
> ' Run the query.
> Set adoRecordset = adoCommand.Execute
>
> ' Enumerate the resulting recordset.
> Do Until adoRecordset.EOF
> ' Retrieve values.
> strDN = adoRecordset.Fields("distinguishedName").Value
> strPhone = adoRecordset.Fields("telephoneNumber").Value
> ' Modify format of phone number.
> strPhone = "(" & Replace(strPhone, "/", ") ")
> ' Bind to user object.
> Set objUser = GetObject("LDAP://" & strDN)
> ' Assign new phone number.
> objUser.telephoneNumber = strPhone
> ' Save changes.
> objUser.SetInfo
> ' Move to the next record in the recordset.
> adoRecordset.MoveNext
> Loop
>
> ' Clean up.
> adoRecordset.Close
> adoConnection.Close
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>

Re: VBScript to change phone format in AD.... by Richard

Richard
Fri May 18 08:40:29 CDT 2007


"kage13" <kage13@discussions.microsoft.com> wrote in message
news:BE6FDA79-7A64-49A5-82DB-14C54AD486A1@microsoft.com...
> Richard,
>
> When running the code provided I'm getting an 80040E14 error at the
> following line:
> Set adoRecordset = adoCommand.Execute
>
> In researching the error, I've found that it may represent a syntax error
> but cannot find anything that jumps out.
>
> Ideas?

My mistake. I missed the final closing ")" in the ADO filter. It should be:

strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(telephoneNumber=*))"

This time I tested and it worked for me.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: VBScript to change phone format in AD.... by kage13

kage13
Fri May 18 10:06:01 CDT 2007

Thanks a million Richard! You saved me many hours of either manually
changing phone numbers and/or banging my head agianst a wall trying to write
a script.

Do you have any suggestions on jumping into VBS at all? I have the
trainging cd's by Don Jones, but those can drag and my interest wanes. I
know its a matter of perserverence, but do thought I'd see if you have an
recommendations.

Thanks again.

"Richard Mueller [MVP]" wrote:

>
> "kage13" <kage13@discussions.microsoft.com> wrote in message
> news:BE6FDA79-7A64-49A5-82DB-14C54AD486A1@microsoft.com...
> > Richard,
> >
> > When running the code provided I'm getting an 80040E14 error at the
> > following line:
> > Set adoRecordset = adoCommand.Execute
> >
> > In researching the error, I've found that it may represent a syntax error
> > but cannot find anything that jumps out.
> >
> > Ideas?
>
> My mistake. I missed the final closing ")" in the ADO filter. It should be:
>
> strFilter = "(&(objectCategory=person)(objectClass=user)" _
> & "(telephoneNumber=*))"
>
> This time I tested and it worked for me.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>

Re: VBScript to change phone format in AD.... by Richard

Richard
Fri May 18 11:55:17 CDT 2007

If your goal is administering AD, the best resource is the text "Microsoft
Windows 2000 Scripting Guide - Automating System Administration". I have the
book, but it is also available online at:

http://www.microsoft.com/technet/scriptcenter/guide/sagsas_overview.mspx

The Technet Script Center has many examples:

http://www.microsoft.com/technet/scriptcenter/default.mspx

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--

"kage13" <kage13@discussions.microsoft.com> wrote in message
news:3561142B-4CF0-4356-ABF8-66C8C38B5876@microsoft.com...
> Thanks a million Richard! You saved me many hours of either manually
> changing phone numbers and/or banging my head agianst a wall trying to
> write
> a script.
>
> Do you have any suggestions on jumping into VBS at all? I have the
> trainging cd's by Don Jones, but those can drag and my interest wanes. I
> know its a matter of perserverence, but do thought I'd see if you have an
> recommendations.
>
> Thanks again.
>
> "Richard Mueller [MVP]" wrote:
>
>>
>> "kage13" <kage13@discussions.microsoft.com> wrote in message
>> news:BE6FDA79-7A64-49A5-82DB-14C54AD486A1@microsoft.com...
>> > Richard,
>> >
>> > When running the code provided I'm getting an 80040E14 error at the
>> > following line:
>> > Set adoRecordset = adoCommand.Execute
>> >
>> > In researching the error, I've found that it may represent a syntax
>> > error
>> > but cannot find anything that jumps out.
>> >
>> > Ideas?
>>
>> My mistake. I missed the final closing ")" in the ADO filter. It should
>> be:
>>
>> strFilter = "(&(objectCategory=person)(objectClass=user)" _
>> & "(telephoneNumber=*))"
>>
>> This time I tested and it worked for me.
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>>