keybrdcowboy
IS-IT--Management
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:
Here's the part where I am getting the error:
Gives me an object requried error. Can anyone tell me why this .Get doesn't work? .Put works just fine.... Thanks for any help.
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.