The Goal: Pull in user logon Ids from a text file and then pull information about them from Active Directory.
The Script:
' Find information from Active Directory using the logon name
'----- Customize the following constants for your needs -----
Const HostList = "targets.txt"
Const ResultsFile = "Logon_Info.csv"
Const ScriptVersion = 1.0
Const JobSleep = 15000 'Time to sleep between jobs, in milliseconds
'----- Constant Declarations -----
Const ForReading = 1
Const ForWriting = 2
Dim objFSO, strFileEntry
Dim ListFile, LogFile, strUser
'----- Script Banner -----
WScript.Echo
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Echo "Start Acitve Directory Information script version " & ScriptVersion
WScript.Echo "Starting Timestamp: " & Now
WScript.Echo
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists(HostList)) Then
Set ListFile = objFSO.OpenTextFile(HostList, ForReading)
Else
wscript.echo ("ERROR: Unable to find input file " & HostList & ".")
wscript.echo (" Please make sure that the file exists and your user")
wscript.echo (" account has rights to access it.")
Set objFSO = Nothing
wscript.quit(2)
End If ' (objFSO.FileExists(HostList))
Set LogFile = objFSO.OpenTextFile(ResultsFile, ForWriting, True)
If Err.Number <> 0 Then
wscript.echo ("ERROR: Unable to initialize requested log file, " & ResultsFile & ".")
wscript.echo (" Please make sure that the path is valid.")
WScript.Echo
WScript.Echo (" Error Code was " & Err.Number & " with description: " & Err.Description & ".")
ListFile.Close
Set objFSO = Nothing
wscript.quit(2)
End If 'Err.Number <> 0
' Write header line to Results File
LogFile.Writeline "Name,login,workstations,First Name, Last Name,Display Name,Telephone,Email,Description"
'
Do while ListFile.AtEndOfStream <> True
strFileEntry = ListFile.ReadLine
strUser = Trim(strFileEntry)
If strUser <> "" Then
GetUserInfo strUser
End If 'strUser <> ""
Loop 'while ListFile.AtEndOfStream <> True
LogFile.Close
ListFile.Close
set LogFile = Nothing
Set ListFile = Nothing
Set objFSO = Nothing
WScript.Echo
WScript.Echo "Ending Timestamp: " & Now
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Quit
'*******************************************************************
'
'***********************************************************
' SUBROUTINES and FUNCTION
'***********************************************************
'
Function GetUserInfo(strUser)
On Error Resume Next
logfile.writeline "Working on " & strUser
Set objUser = GetObject("LDAP://CN=" & strUser & "CN=Domain%20Users,CN=Users,dc=MYDOMAIN,dc=local")
logfile.writeline objUser.username
logfile.writeline err & " " & err.description
Wscript.Echo err.description
WScript.Echo "User Principal Name: " & objUser.userPrincipalName
WScript.Echo "SAM Account Name: " & objUser.sAMAccountName
WScript.Echo "User Workstations: " & objUser.userWorkstations
WScript.Echo "First Name: " & objUser.givenName
WScript.Echo "Last Name: " & objUser.sn
WScript.Echo "Display Name: " & objUser.displayName
WScript.Echo "Telephone Number: " & objUser.telephoneNumber
WScript.Echo "Email: " & objUser.mail
logfile.writeline strUser & "," & objUser.userPrincipalName & "," & objUser.sAMAccountName & "," & objUser.userWorkstations & "," & objUser.givenName & "," & objUser.sn & "," & objUser.displayName & "," & objUser.telephoneNumber & "," & objUser.mail
end function
The Problem: Getting a "Object Required" error on all the objUser.**** items. Like there is nothing coming from LDAP at all. Is there an "Impersonate" type thing for LDAP? I haven't had to use one before now to pull items. Very new to VBScript, no real training.
The Script:
' Find information from Active Directory using the logon name
'----- Customize the following constants for your needs -----
Const HostList = "targets.txt"
Const ResultsFile = "Logon_Info.csv"
Const ScriptVersion = 1.0
Const JobSleep = 15000 'Time to sleep between jobs, in milliseconds
'----- Constant Declarations -----
Const ForReading = 1
Const ForWriting = 2
Dim objFSO, strFileEntry
Dim ListFile, LogFile, strUser
'----- Script Banner -----
WScript.Echo
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Echo "Start Acitve Directory Information script version " & ScriptVersion
WScript.Echo "Starting Timestamp: " & Now
WScript.Echo
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists(HostList)) Then
Set ListFile = objFSO.OpenTextFile(HostList, ForReading)
Else
wscript.echo ("ERROR: Unable to find input file " & HostList & ".")
wscript.echo (" Please make sure that the file exists and your user")
wscript.echo (" account has rights to access it.")
Set objFSO = Nothing
wscript.quit(2)
End If ' (objFSO.FileExists(HostList))
Set LogFile = objFSO.OpenTextFile(ResultsFile, ForWriting, True)
If Err.Number <> 0 Then
wscript.echo ("ERROR: Unable to initialize requested log file, " & ResultsFile & ".")
wscript.echo (" Please make sure that the path is valid.")
WScript.Echo
WScript.Echo (" Error Code was " & Err.Number & " with description: " & Err.Description & ".")
ListFile.Close
Set objFSO = Nothing
wscript.quit(2)
End If 'Err.Number <> 0
' Write header line to Results File
LogFile.Writeline "Name,login,workstations,First Name, Last Name,Display Name,Telephone,Email,Description"
'
Do while ListFile.AtEndOfStream <> True
strFileEntry = ListFile.ReadLine
strUser = Trim(strFileEntry)
If strUser <> "" Then
GetUserInfo strUser
End If 'strUser <> ""
Loop 'while ListFile.AtEndOfStream <> True
LogFile.Close
ListFile.Close
set LogFile = Nothing
Set ListFile = Nothing
Set objFSO = Nothing
WScript.Echo
WScript.Echo "Ending Timestamp: " & Now
WScript.Echo ("********************************************************")
WScript.Echo
WScript.Quit
'*******************************************************************
'
'***********************************************************
' SUBROUTINES and FUNCTION
'***********************************************************
'
Function GetUserInfo(strUser)
On Error Resume Next
logfile.writeline "Working on " & strUser
Set objUser = GetObject("LDAP://CN=" & strUser & "CN=Domain%20Users,CN=Users,dc=MYDOMAIN,dc=local")
logfile.writeline objUser.username
logfile.writeline err & " " & err.description
Wscript.Echo err.description
WScript.Echo "User Principal Name: " & objUser.userPrincipalName
WScript.Echo "SAM Account Name: " & objUser.sAMAccountName
WScript.Echo "User Workstations: " & objUser.userWorkstations
WScript.Echo "First Name: " & objUser.givenName
WScript.Echo "Last Name: " & objUser.sn
WScript.Echo "Display Name: " & objUser.displayName
WScript.Echo "Telephone Number: " & objUser.telephoneNumber
WScript.Echo "Email: " & objUser.mail
logfile.writeline strUser & "," & objUser.userPrincipalName & "," & objUser.sAMAccountName & "," & objUser.userWorkstations & "," & objUser.givenName & "," & objUser.sn & "," & objUser.displayName & "," & objUser.telephoneNumber & "," & objUser.mail
end function
The Problem: Getting a "Object Required" error on all the objUser.**** items. Like there is nothing coming from LDAP at all. Is there an "Impersonate" type thing for LDAP? I haven't had to use one before now to pull items. Very new to VBScript, no real training.