Solved

A script to move the IPPhone value to the Telephone Field

Posted on 2011-02-24
6
1,350 Views
Last Modified: 2013-12-24
Folks I'm trying to move the Iphone info for every user in our AD to the Telephone field.

I think I'm 90% there but I need some help to finish.

Can you please help?
'****************************************Moveadinfo.vbs**********************************************
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset
Dim strDN, objUser, strIpFone

Const ADS_PROPERTY_CLEAR = 1

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

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

' Filter on user objects with value assigned to info attribute.
strFilter = "(&(objectCategory=person)(objectClass=user))"
wscript.echo strFilter

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

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
wscript.echo strQuery
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 DN value.
 strDN = adoRecordset.Fields("distinguishedName").Value
wscript.echo strDN
 ' Bind to user object.
 Set objUser = GetObject("LDAP://" & strDN)
 ' Clear info attribute.
strIpFone = objUser.ipphone
wscript.echo strIpFone 
objUser.Put "telephonenumber", strIpFone
 ' Save changes.
 objUser.SetInfo
 ' Move to the next record in the recordset.
 adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close
'****************************************************************************************************

Open in new window

0
Comment
Question by:alankool
  • 3
  • 2
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34967979
Your script looks fine are you getting any error messages or in what way is it not working
0
 

Author Comment

by:alankool
ID: 34968137
The prblem is with line 49.

The objUser.Put method doesn't like the strIpFone variable.

I don't have a test environment to re-run the script just at the moment and I'm reluctant to run the script as it is on our production system.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 124 total points
ID: 34968378
Is it possible that sometimes the users IPPhone attribute is going to return null and so the put line doesn't like it when you try a put a null.
Try checking for a null in the strIpFone variable before performing the put.
Also not sure if it matters but the correct capitalisation for the attributes you are using are : telephoneNumber and ipPhone, I have known this to cause problems in the past if not used correctly.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Accepted Solution

by:
MisterTwelve earned 126 total points
ID: 34969269
try this

'****************************************Moveadinfo.vbs**********************************************
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset
Dim strDN, objUser, strIpFone

Const ADS_PROPERTY_CLEAR = 1
Const ADS_PROPERTY_UPDATE = 2 


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

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

' Filter on user objects with value assigned to info attribute.
strFilter = "(&(objectCategory=person)(objectClass=user))"
wscript.echo strFilter

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

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
wscript.echo strQuery
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 DN value.
 strDN = adoRecordset.Fields("distinguishedName").Value
wscript.echo strDN
 ' Bind to user object.
 Set objUser = GetObject("LDAP://" & strDN)
 ' Clear info attribute.
strIpFone = objUser.ipphone
wscript.echo strIpFone 
If strIpFone <> "" Then
	objUser.Put "telephonenumber", strIpFone
 	' Save changes.
 	objUser.SetInfo
End If

strIpFone = ""
 ' Move to the next record in the recordset.
 adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

0
 

Author Comment

by:alankool
ID: 34975366
Guys
Just spent ages at this foolishly without checking back on the forum.

jacko72 you were absolutely correct... the problem related to the null value.

MisterTwelve thanks for your input. Without trying your script I can see it is a slimmed down version of what I eventually ended up with. I wish I had known about
Const ADS_PROPERTY_UPDATE = 2

I made the script do some validation first and I also included some logging. I have tried it on my test environment and it appears to work well.

I'd appreciate if you could take a look at my script and check if there are any glaring errors.
If everything looks good please say so too :)

I'm thinking I should split the points but insightful feedback could tip the balance in your favour :)

Thanks a lot!!!
 
'******************************************MoveadinfoAdv.vbs**************************************

Option Explicit
'Variables
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset
Dim strDN, objUser, strIpFone, WshShell, fso, ts, strTelephone, objLogFile, strFullName


'Constant
Const ADS_PROPERTY_CLEAR = 1

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

Set adoCommand.ActiveConnection = adoConnection
'Set Windows Scripting Shell
Set WshShell = WScript.CreateObject("WScript.Shell")

'Set File System Object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

'Create and release file
Set ts = fso.CreateTextFile("z:\tv\log.txt")
set fso = Nothing 
set ts = Nothing 

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

' Filter on user objects with value assigned to info attribute.
strFilter = "(&(objectCategory=person)(objectClass=user))"

'debug command used on test environment
'wscript.echo strFilter

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

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
'debug command used on test environment
'wscript.echo strQuery

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 DN value.
 strDN = adoRecordset.Fields("distinguishedName").Value

'debug command used on test environment
'wscript.echo strDN

 ' Bind to user object.
 Set objUser = GetObject("LDAP://" & strDN)
 ' Clear info attribute.

'Define Name variable for log File
strFullName = objUser.name



'Define TelePhone Number variable for log File
If objuser.telephonenumber <> "" then 
strTelephone = objuser.telephonenumber
Else 
strTelephone = "+BLANK FIELD+"
End If

'Define IpPhone variable for log File
if objUser.ipphone <> "" then
strIpFone = objUser.ipphone

'**********Copy/Delete Operation*****************************
objUser.Put "telephonenumber", strIpFone
objUser.PutEx ADS_PROPERTY_CLEAR, "IPPhone", 0
 ' Save changes.
 objUser.SetInfo

'debug command used on test environment
'wscript.echo strTelephone & "/" & strIpFone

'*************Log File***************************************
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objLogFile = fso.OpenTextFile("z:\tv\log.txt",8,True)

objLogFile.Writeline strFullName & " telephone number --" & strTelephone & "-- was replaced by the IP Phone Field --" & strIpFone 
objLogFile.Close
set fso = Nothing 
Else
'IP Phone field is Empty
strIpFone = "+EMPTY FIELD+"

Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objLogFile = fso.OpenTextFile("z:\tv\log.txt",8,True)

objLogFile.Writeline strFullName & " telephone number --" & strTelephone & "--  was not replaced as IP Phone field was " & strIpFone 
objLogFile.Close
set fso = Nothing
End If

 ' Move to the next record in the recordset.
 adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Open in new window

0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975417
It looks good, split the points anyway.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now