Recieve a null error with objUser.Put

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Recieve a null error with objUser.Put

Post by TXTechie »

Hello,

I'm using the following script to obtain the SMTP e-mail address from our Exchange server and populating the "mail" value in Active Directory (AD) for each user.

Everything seems to be working except that I am receiving a "(null): 0x80005008" error on the "objUser.Put" line of code (highlighted in red below) while logged on with Domain Admin rights.

Here's my script:

Code: Select all

' Create Objects for LDAP Queries
Set rootDSE = GetObject("LDAP://RootDSE")
DomainContainer = rootDSE.Get("defaultNamingContext")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
' LDAP query of all distinguished names and user names where the 'mail' field's value is Empty
objCommand.CommandText = "<LDAP://" & DomainContainer & ">;" & _
  "(&(objectCategory=User)(!mail=*));" & _
  "sAMAccountName,distinguishedName;subtree"
Set objRecordSet = objCommand.Execute
If objRecordSet.EOF Then
 WScript.Echo "All user accounts contain a value for the 'mail' attribute."
Else
 WScript.Echo "User account(s) without a 'mail' value:"
 
 While Not objRecordSet.EOF
  ' Set the LDAP query to Exchange
  strExLDAP = "<LDAP://essilor.res>;(&(objectClass=User)(mailnickname=" & objRecordSet.Fields("sAMAccountName") & "));mail;subtree"
  
  ' Execute the Exchange LDAP query to determine this User Account's E-mail (SMTP) address
  Set objRecordSetEx = objConnection.Execute(strExLDAP)
  If Not IsNull(objRecordSetEx("mail")) Then
   ' Add the Exchange E-mail (SMTP) address to the current User Account's 'mail' field in AD
   Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))
   objUser.Put "mail", objRecordSetEx("mail")
   objUser.SetInfo
   WScript.Echo objRecordSet.Fields("distinguishedName") & " = " & objUser.Get("mail")
   End If
  objRecordSet.MoveNext
 Wend
End If
objConnection.Close

Can someone help me figure out why this script won't update the "mail" value in AD when logged on with Domain Admin rights?

Thank you,

TX TechieTXTechie2009-03-25 13:41:48
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Recieve a null error with objUser.Put

Post by rasimmer »

If you just MsgBox objRecordSetEx("mail"), do you get any values? The IsNull function doesn't always work for me, so I usually do a check like:

If objRecordSetEx("mail") = "" Or IsNull(objRecordSetEx("mail")) Then
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Recieve a null error with objUser.Put

Post by jvierra »

Absolutely - the recipient update service in Exchange should do this automatically unless it has been disabled or misconfigured.

The most common I have seen is that some admin puts weird addresses in teh template and then doesn't know how to stop them from being propagated which causes them to disable the service. I would check to see if this has happened.

The Exchange tab in ADUC should show all of teh addresses propagated via update service and any that have been added by hand. AD is the only place where Exchange stores addresses. Exchange only know about "stores" and mailboxes..( list and things too).

The second recordset you are grabbing from Exchange is always empty.

The GetObject(user) will not bring back the mail attribute as it is not cacheable. It should also be an array of addresses so it has to be assigned as an array.

User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Recieve a null error with objUser.Put

Post by TXTechie »

I appreciate all of the quick responses and assistance.

It is my understanding (from our AD and Exchange Architect and Administrator) that the "mail" field in AD will not be automatically populated with the Exchange primary SMTP address in our environment because our Exchange architecture is in a separate Resource Forest. So, we have a procedure to have this field manually updated during the account creation.

However, some AD accounts have been missed and I have been tasked to determine the AD accounts where the "mail" field is empty and then to populate that field with the correct primary SMTP address from Exchange. So, this is what I'm attempting to do.

Thank you.


P.S. - The variable objRecordSetEx("mail") results in providing me with the primary SMTP address from Exchange.
User avatar
rasimmer
Posts: 182
Last visit: Fri Apr 25, 2014 7:00 am

Recieve a null error with objUser.Put

Post by rasimmer »



If objRecordSetEx("mail") = "" Or IsNull(objRecordSetEx("mail")) Then

Try this?
User avatar
TXTechie
Posts: 13
Last visit: Sun Oct 04, 2020 7:40 pm

Recieve a null error with objUser.Put

Post by TXTechie »

If objRecordSetEx("mail") = "" Or IsNull(objRecordSetEx("mail")) Then

Try this?


If I use:

Code: Select all

If objRecordSetEx("mail") <> "" Or Not IsNull(objRecordSetEx("mail")) Then
I receive a (null): Exception occurred. error referencing that specific line.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Recieve a null error with objUser.Put

Post by jvierra »

This is strange but I think this is caused by type casting of the variable. Even though vbscript is loosely typed it seems like the command:

objUser.Put "mail", objRecordSetEx("mail")
does not like using the object record set. I tried this in my code and got it to work

dim tempEmailAddress
tempEmailAddress = objRecordSetEx("mail")
objUser.Put "mail", tempEmailAddress

Hope this helps.

This topic was closed about two years ago. The cause of teh issue was made clear. You cannot put a null or empty value. Just moving the value in and out of variables won't change anything because the type and contents will still be the same.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Recieve a null error with objUser.Put

Post by jvierra »

I revisted this quickly because something about all claims and solutions bothered me.

The issue is caused when there is no record in Exchange for teh requested mail alias. The voleu will not test to null and the code will execute against a bad value. All other fixes may help to skirt the issue. The correct coding would be to test for EOF before attmpting to do an update.

Rasimmer's fix just hides the error and thus forces the string to be empty. Testing for EOF would correct all issues.

Code: Select all

	
          ' Execute the Exchange LDAP query to determine this User Account's E-mail (SMTP) address
          Set objRecordSetEx = objConnection.Execute(strExLDAP)
          If Not objRecordSetEx.EOF Then
               
               ' Add the Exchange E-mail (SMTP) address to the current User Account's 'mail' field in AD
               Set objUser = GetObject("LDAP://" & objRecordSet.Fields("distinguishedName"))
               objUser.Put "mail", objRecordSetEx("mail")
               objUser.SetInfo
               WScript.Echo objRecordSet.Fields("distinguishedName") & " = " & objUser.Get("mail")
               
          End If
          
          objRecordSet.MoveNext
The above just cahnges the one line to test for EOF tather than assuming that the value will test to null. If ther eis an alias in Exchange then 'mail' can never be null. it is a requirement for the Exchange identity to be created. 'mail' is not required for the AD account but it is for the Exchange alias accout (mailbox).
User avatar
joeboyall
Posts: 1
Last visit: Thu Jun 05, 2014 6:53 am

Recieve a null error with objUser.Put

Post by joeboyall »

it's bothering me too and I dont really understand why this works this way. So I'll give a little background. I'm getting addresses from one domain then populating the address to a different domain. So I have narrowed the code down to get one email address then try to put that value in the object on the side. The email address that I get back is valid and not null and the address does not exist on the target side. I think what I'm experiencing has something to do with type coercion since there really only variants in vbscript. This is what I have at this point.

This is a sub that is called where PrimaryEmailAddress = objrecordsetAD.fields("mail")

This fails:
oUser.put "mail", PrimaryEmailAddress

This works:
oUser.put "mail", cSTR(PrimaryEmailAddress)

I tried vartype(PrimaryEmailAddress) and I get 8 which is string but it still fails. When I force string with cSTR it works.
This topic is 12 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked