VB Script for User Info

Joined
Nov 2, 2005
Messages
3
Reaction score
0
I have this VB script (below)

The script extracts info that I need, but I would like to get a little more info with it. I want the users display name and description. The areas in bold are where I'm having problems.

Thanks
M Hasse


'get domain and container
Dim sTarget, sOU
sTarget = InputBox("Target what domain?", "Domain?", _
"domain")
sLDAP = InputBox("LDAP version of domain name?", "LDAP Domain?", _
"dc=domain,dc=next,dc=again,dc=area,dc=local,dc=com")
'iterate through all DCs?
Dim bCheckAll
bCheckAll = MsgBox("Check all DCs for last logon date?", _
4, "Check all DCs?")
If bCheckAll = 6 Then
bCheckAll = True
Else
bCheckAll = False
End If
'objects for output file
Dim sOutput, oFSO, oTS
sOutput = InputBox("Output path and filename?", "Filename?", _
"a:\UserLogin.csv")
Set oFSO = CreateObject("Scripting.FileSystemObject")
'see if file exists
If oFSO.FileExists(sOutput) Then
MsgBox("File exists. Please select another.")
WScript.Quit
End If
'create output file
Set oTS = oFSO.CreateTextFile(sOutput)
Const cComma = ","
'output header
oTS.WriteLine "User" & cComma & "Display Name" & _
cComma & "Description" & _
cComma & "Disabled" & cComma & "Locked" & _
cComma & "IdleDays" & cComma & "Login Date"
'get destination container
On Error Resume Next
Set oOU = GetObject("WinNT://" & sTarget)
If Err <> 0 Then
MsgBox "Error: " & vbCrLf & Err.Description
WScript.Quit
End If
On Error Goto 0
'filter for user objects
oOU.Filter = Array("User")
'get the Default Domain Controllers container
Dim oDCs, oDC
Set oDCs = GetObject("LDAP://ou=Domain Controllers," & _
sLDAP)
'go through container objects
Dim oObject
For Each oObject In oOU
'get attributes
Dim bDisabled, bLocked, iIdleDays
Dim dLastLogin, dLocalLastLogin, sDisplayName
'reset last login date
dLastLogin = CDate("1/1/1900")
'is user disabled?
If oObject.accountdisabled Then
bDisabled = True
Else
bDisabled = False
End If
'is user locked out?
If oObject.isaccountlocked Then
bLocked = True
Else
bLocked = False
End If
'Get Display Name
sDisplayName = "Code to extract users full name"

'Get Descritpion Field
sDescription = "Code to extract user Description field"


'output basic info
oTS.Write oObject.name & cComma & _
sDisplayName & cComma & _
sDescription & cComma & _
bDisabled & cComma & _
bLocked & cComma
'check for last login?
If bCheckAll Then
'checking last login on all DCs
For Each oDC In oDCs
'get last login from this DC
Dim oLocalUser

On Error Resume Next
Set oLocalUser = GetObject("WinNT://" & _
Replace(oDC.sAMAccountName,"$","") & "/" & _
oObject.name & ",user")
On Error Resume Next
dLocalLastLogin = oLocalUser.LastLogin
If Err <> 0 Then
dLocalLastLogin = CDate("1/1/1900")
End If
On Error Goto 0
'is that more recent?
If dLocalLastLogin > dLastLogin Then
dLastLogin = dLocalLastLogin
End If

Next
'output last login
oTS.Write DateDiff("d",dLastLogin,Date) & cComma & _
dLastLogin & vbcrlf
Else
'not checking last login
oTS.Write "n/a" & vbcrlf
End If
Next
'close text file and notify
oTS.Close
MsgBox "Finished. Output is at " & sOutput
 
Joined
Nov 2, 2005
Messages
3
Reaction score
0
I found the answer for getting Display Name and descriptions.

I ended up using oObject.FullName and oObject.Descrition.

I would be interested if anyone knows the attribute to pull account creation date. oObject.(creation date attribute here)

Thanks again.
M Hasse

My script now looks like this and work well.
'get domain and container
Dim sTarget, sOU
sTarget = InputBox("Target what domain?", "Domain?", _
"domain")
sLDAP = InputBox("LDAP version of domain name?", "LDAP Domain?", _
"domain info goes here")
'iterate through all DCs?
Dim bCheckAll
bCheckAll = MsgBox("Check all DCs for last logon date?", _
4, "Check all DCs?")
If bCheckAll = 6 Then
bCheckAll = True
Else
bCheckAll = False
End If
'objects for output file
Dim sOutput, oFSO, oTS
sOutput = InputBox("Output path and filename?", "Filename?", _
"a:\UserLogin.csv")
Set oFSO = CreateObject("Scripting.FileSystemObject")
'see if file exists
If oFSO.FileExists(sOutput) Then
MsgBox("File exists. Please select another.")
WScript.Quit
End If
'create output file
Set oTS = oFSO.CreateTextFile(sOutput)
Const cComma = ","
'output header
oTS.WriteLine "User" & cComma & "Display Name" & _
cComma & "Description" & _
cComma & "Disabled" & cComma & "Locked" & _
cComma & "IdleDays" & cComma & "Login Date"
'get destination container
On Error Resume Next
Set oOU = GetObject("WinNT://" & sTarget)
If Err <> 0 Then
MsgBox "Error: " & vbCrLf & Err.Description
WScript.Quit
End If
On Error Goto 0
'filter for user objects
oOU.Filter = Array("User")
'get the Default Domain Controllers container
Dim oDCs, oDC
Set oDCs = GetObject("LDAP://ou=Domain Controllers," & _
sLDAP)
'go through container objects
Dim oObject
For Each oObject In oOU
'get attributes
Dim bDisabled, bLocked, iIdleDays
Dim dLastLogin, dLocalLastLogin, sDisplayName
'reset last login date
dLastLogin = CDate("1/1/1900")
'is user disabled?
If oObject.accountdisabled Then
bDisabled = True
Else
bDisabled = False
End If
'is user locked out?
If oObject.isaccountlocked Then
bLocked = True
Else
bLocked = False
End If

'output basic info
oTS.Write oObject.name & cComma & _
oObject.FullName & cComma & _
oObject.Description & cComma & _
bDisabled & cComma & _
bLocked & cComma
'check for last login?
If bCheckAll Then
'checking last login on all DCs
For Each oDC In oDCs
'get last login from this DC
Dim oLocalUser

On Error Resume Next
Set oLocalUser = GetObject("WinNT://" & _
Replace(oDC.sAMAccountName,"$","") & "/" & _
oObject.name & ",user")
On Error Resume Next
dLocalLastLogin = oLocalUser.LastLogin
If Err <> 0 Then
dLocalLastLogin = CDate("1/1/1900")
End If
On Error Goto 0
'is that more recent?
If dLocalLastLogin > dLastLogin Then
dLastLogin = dLocalLastLogin
End If

Next
'output last login
oTS.Write DateDiff("d",dLastLogin,Date) & cComma & _
dLastLogin & vbcrlf
Else
'not checking last login
oTS.Write "n/a" & vbcrlf
End If
Next
'close text file and notify
oTS.Close
MsgBox "Finished. Output is at " & sOutput
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top