Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Required error... Can anyone explain why?

Status
Not open for further replies.

keybrdcowboy

IS-IT--Management
Aug 31, 2004
96
US
I have run into this issue several times. In the past I have just ended up redoing the whole script and connecting to the user account a different way, but I am hoping someone can explain what I have wrong and why this doesn't work. Here's my code:

Code:
Option Explicit

Dim FSO, oShell, strLogPath, readFile, strOutFile, strOutFile2, strFullDN, oRootDSE, oConnection, oCommand, oRecordSet, strUserCount
Dim outFile, outFile2, iUpperBound, strUser, oUser, strEmail

Set FSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")
strLogPath = oShell.ExpandEnvironmentStrings("%USERPROFILE%")

Const ForAppending = 8

' Path to log file
strOutFile = strLogPath & "\desktop\UserErrors.log"

' Path to output file
strOutFile2 = strLogPath & "\desktop\UserEmail.log"

'If FSO.FileExists(strOutFile2) = True Then
'  Set outFile2 = FSO.OpenTextFile(strOutFile2, ForAppending)
'Else
Set outFile2 = FSO.CreateTextFile(strOutFile2, True)
Set outFile = FSO.CreateTextFile(strOutFile, True) 
'End If

' Path to file containing usernames
Set readFile = FSO.OpenTextFile(strLogPath & "\desktop\Users.txt")

' Reads the contents of the CPSGUsers.txt into an array named arrUsers
iUpperBound = 0
While Not ReadFile.AtEndOfStream
  ReDim Preserve arrUsers(iUpperBound)
  arrUsers(UBound(arrUsers)) = ReadFile.ReadLine
  iUpperBound = iUpperBound + 1
Wend
ReadFile.Close

outFile2.Writeline "User Email Addresses                   " & Date & Time
outFile2.WriteBlankLines(3)

' Just a few lines of code to start the Log File off
outFile.WriteLine "*************************************************************************************************"
outFile.WriteLine "*************************************************************************************************"
outFile.WriteBlankLines(1)
outFile.WriteLine "Starting User Email Retrival Script.     " & Date & "     " & Time
outFile.WriteBlankLines(2)

strUserCount = 0

' The following lines will connect to each user account and pull the Email address
For Each strUser in arrUsers
  On Error Resume Next
  strFullDN = fncConnectAd(strUser)
  On Error Goto 0
  If Err <> 0 Then
    outFile.WriteLine "Could not get Full DN for " & strUser & ".  Please check account and try again.     " & Date & "     " & Time
  Else  
    On Error Resume Next
    Set oUser = GetObject("LDAP://" & strFullDN)
    On Error Goto 0
    If Err <> 0 Then
      outFile.WriteLine "Could not retrieve the email address for account " & strUser & ".  Please ensure they have an email address in AD.     " & Date & _
                        "     " & Time
    Else
      'strEmail = oUser.Get("mail")
      wscript.echo oUser.Get ("mail")
      'outFile2.WriteLine oUser.Get("mail")
      outFile.WriteLine "Retrieved email address for " & strUser & ".     " & Date & "     " & Time
      strUserCount = strUserCount + 1
    End If
  End If    
  strFullDN = null
  oUser = null
  Err.Clear
Next

outFile.WriteBlankLines(3)
outFile.WriteLine "Finished with User Email Retrival Script at " & Time & " on " & Date & "."
outFile.Close 
outFile2.WriteBlankLines(3)
outFile2.WriteLine "Successfully retrieved " & strUserCount & " user account email addresses."
outFile2.Close
WScript.echo "Done     " & Time




' This funtion will take a username and return the complete Distinguished Name
Public Function fncConnectAD(strUsername)
  Set oRootDSE = GetObject("LDAP://rootDSE")
  Set oConnection = CreateObject("ADODB.Connection")
  oConnection.Open "Provider=ADsDSOObject;"
  Set oCommand = CreateObject("ADODB.Command")
  oCommand.ActiveConnection = oConnection
  oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;(&(objectCategory=User)(samAccountName=" & _
                         strUserName & "));distinguishedName;subtree"
  Set oRecordSet = oCommand.Execute
  fncConnectAD = oRecordSet("distinguishedName")
End Function


Here's the part where I am getting the error:

Code:
'strEmail = oUser.Get("mail")
 wscript.echo oUser.Get ("mail")
'outFile2.WriteLine oUser.Get("mail")

Gives me an object requried error. Can anyone tell me why this .Get doesn't work? .Put works just fine.... Thanks for any help.
 
Try using

strEmail = oUser.mail

or

oUser.GetInfo
strEmail = oUser.mail
 
Nope, get the same error message. It's on the line where I put oUser.GetInfo. I get an "Object required: " " error. Any other ideas? Thanks.
 
i would say you dont need to use that recordset function to get the DN, tsuji and i posted a NameTranslate way of doing it, though perhaps your way is faster ?

perhaps the "mail" field for the user is NULL, or am i still thinking in record sets..nope thats not it

you use
On Error Goto 0
before the "mail" call

are you sure that On Error Goto 0 doesnt reset the Err.NUmber to 0 as well as turning error checking back off again? therefore regardless of what err has occured in your binding you would never see it.....
 
mrmovie's
>perhaps the "mail" field for the user is NULL
In that case wscript.echo it will result in type mismatch error only.

mrmovie's
>are you sure that On Error Goto 0 doesnt reset the Err.NUmber to 0 as well as turning error checking back off again? therefore regardless of what err has occured in your binding you would never see it.....
This is an excellent observation. I can confirm it. It is not difficult to verify it as well.
The op needs to re-write the on error part. The condition if err(.number)<>0 then must not appear after reset on error goto 0 or it is meaningless. Also err.clear at the shown position is useless as well.

op's
>oUser = null
Very much unorthodox, though cannot see any harmful effect yet.

Overall, it is very well oUser is really required as it is failed to established for the erroneous cases.
 
yes good point tsuji with if one needs to bind to the user object at all. one could just use the already implemented record set approach and retrieve the mail field as well.
i have to admit i have wrote a script to retrieve a users email address as a custom action and went for the LDAP oRS query approach,,,perhaps it is faster
 
I appreciate everyone's help. Okay, I believe the original problem was caused by that first user in my list not actually having an email address in AD. (Someday I will learn to check the small stuff before posting...) I made the recommended Error checking changes, and am now having another problem. For some reason, most of my user accounts' DN comes out with a "/" in it. This is causing me problems when it goes to connect to the account with the LDAP path. But I am sure that is somethingwith my user accounts. I do realize that I can pull the mail field with the function that is returning the DN. The only reason I am not using that is because I do not know how to update like that. How do I update accounts when using that recordset thing? Thanks for everyone's help. I didn't know that the On Error Goto 0 would reset my error numbers, nice to know. Thanks again.
 
i dont see anywhere in your script where you update anything i.e. user object...

'get LDAP object with extended properties using WinNT equiv name
Set objTrans = CreateObject("NameTranslate")
objTrans.Set 3, "domain_name_here\" & strUserName
'add a $ for a computer name
strUserDN = objTrans.Get(1)
Msgbox strUserDN
Set objUser = GetObject("LDAP://" & strUserDN)

or a M$ way...i think it does the same thing

Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)


 
Yes, in this script, I am not updating anything. But in past scripts I had not been able to figure out how update with the recordset method, so I wouldn't ever use it. Since I already have that function that I just cut and paste, I would prefer to just use that. But I need to know how to get it to update... Thanks for your help. Are your examples different ways of doing what I did?
 
if you want to pull back lots of data corresponding to lots of users then on this Macro level oRS's are a good idea.
If you want to pull back info on one user, Micro level, then perhaps binding straight to the user object is the most efficient.

i have scratched on a few occasions with the following common problem.

1. You have the user name, from %username%, WshNetwork blaa blaa
2. You want to update/query user account properties which are not supported via the WinNT provider (WinNT provider is nice that you can just supply it with Domain\user and you are off and running
3. You therefore need to bind to the user object using LDAP provider which likes to have DistName
Ou=blla.net\Ou=Uk\OU=London\Ou....blaa
4. Therefore you need to Translate the WinNT username to its AD/LDAP UserDN

You presented the oRS ADO query approach to returning this information.
I think ;-) i have present 2 alternatives. Perhaps a FAQ is in order. I ask myself often enough.
I havent had the chance to find out which is the quicker approach. I havent had the chance to think about which is the most robust or which can more easily be applied to other situations etc

anyway, i hope your scirpt is now functioning as it should do
 
Yes it is. Turned out I was getting errors due to the OU names that I was trying to pull the user info from. Fixed those (Why did AD allow them to be created with names that don't work correctly??) and it's all working now. Thanks for everyone's help.
 
put crap in, get crap out" shouldnt apply to a script.
so, i would have to disagree and say that the problem was your use of On Error etc.. ;-)
 
Ummmm.... not sure what you mean there. The error control stuff might have been making me get the wrong error message, but the problem that stopped the script from doing what it had to do was the names of the OUs in Active Directory. Fixed those and it worked fine after that.
 
> For some reason, most of my user accounts' DN comes out with a "/" in it.
These are escapeable characters for ldap adspath's component:
[tt],=+<>#;\"[/tt]
If you know systematically that they are in the data file for adspath or consequently dn, you have replace them by having "\" preceeding them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top