Solved

Issues with a VBScript

Posted on 2010-08-31
8
977 Views
Last Modified: 2012-05-10
I am getting this error when running the attached script.  Can anyone help?

Code Error  8000500D -
The directory property cannot be found in the cache
'Define Constants
Const ForReading = 1
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000

DQ = Chr(34) 'Double Quote

strOutputHeader = "Date Script was executed: " & Now() & VbCrLf & VbCrLf & _
"USER INFORMATION" & VbCrLf & _
"*************************************************************"

'Prompt for username
Do
	strUsername = _
	InputBox("Please enter the username:", _
	"Username Input", "username")
	If strUsername = False Then
		WScript.Quit
	End If
Loop Until strUsername <> ""

WScript.Echo "Creating file for " & strUsername

'Create Objects
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SecurityGroups = CreateObject("System.Collections.ArrayList")
Set DistributionGroups = CreateObject("System.Collections.ArrayList")

'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")

'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

'Create output text file
strScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strOutputFilePath = strScriptPath & strUsername & ".txt"
Set objOutputFile = objFSO.CreateTextFile(strOutputFilePath)

'Search AD for user
objCommand.CommandText = _
"SELECT ADsPath FROM '" & strADsPath & _
"' WHERE objectCategory='user' and sAMAccountName='" & strUsername & "'"
Set objRecordSet = objCommand.Execute

'Verify user was found
If objRecordSet.EOF Then
	WScript.echo "User named " & strUser & " not found."
Else
	objRecordSet.MoveFirst
	Do Until objRecordSet.EOF
		strUserADsPath = objRecordSet.Fields("ADsPath").Value
		objRecordSet.MoveNext
	Loop
End If

'Connect to user account
Set objUser = GetObject(strUserADsPath)

'Get user information
strsAMAccountName = objUser.sAMAccountName
strEmployeeID = objUser.EmployeeID
struserprincipalname = objUser.userPrincipalName
strdisplayName = objUser.displayName
strtitle = objUser.title
strdescription = objUser.description
strmanager = objUser.manager
strdepartment = objUser.department
strdistinguishedName = objUser.distinguishedName
struserAccountControl = objUser.userAccountControl
strwhenCreated = objUser.whenCreated

'Format Last Logon Time Stamp
Set objLastLogon = objUser.Get("lastLogonTimestamp")
intLastLogonTime = objLastLogon.HighPart * (2 ^ 32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
dtmLastLogon = intLastLogonTime + # 1 / 1 / 1601 #

'Put groups in arrays based on type and sort them
strGroups = objUser.memberOf
For Each strGroup In strGroups
	Set objGroup = GetObject("LDAP://" & strGroup)
	strGroupType = GetType(objGroup.GroupType)
	If strGroupType = "Security" Then
		SecurityGroups.Add objGroup.CN
	End If
	If strGroupType = "Distribution" Then
		DistributionGroups.Add objGroup.CN
	End If
Next
SecurityGroups.Sort()
DistributionGroups.Sort()

'Populate text file
objOutputFile.WriteLine strOutputHeader
objOutputFile.WriteLine "sAMAccountName - " & strsAMAccountName & VbCrLf
objOutputFile.WriteLine "EmployeeID - " & strEmployeeID & VbCrLf
objOutputFile.WriteLine "userprincipalname - " & struserprincipalname & VbCrLf
objOutputFile.WriteLine "displayName - " & strdisplayName & VbCrLf
objOutputFile.WriteLine "title - " & strtitle & VbCrLf
objOutputFile.WriteLine "description - " & strDescription & VbCrLf
objOutputFile.WriteLine "manager - " & strmanager & VbCrLf
objOutputFile.WriteLine "department - " & strdepartment & VbCrLf
objOutputFile.WriteLine "distinguishedName - " & strdistinguishedName & VbCrLf
objOutputFile.WriteLine "userAccountControl - " & struserAccountControl & VbCrLf
objOutputFile.WriteLine "whenCreated - " & strwhenCreated & VbCrLf
objOutputFile.WriteLine "lastlogon - " & dtmLastLogon & VbCrLf
objOutputFile.WriteLine "*************************************************************" & VbCrLf
objOutputFile.WriteLine "Distribution Group List"
objOutputFile.WriteLine "*************************************************************"

For Each strGroup In DistributionGroups
	objOutputFile.WriteLine strGroup
Next

objOutputFile.WriteLine "*************************************************************" & VbCrLf
objOutputFile.WriteLine "Security Group List"
objOutputFile.WriteLine "*************************************************************"

For Each strGroup In SecurityGroups
	objOutputFile.WriteLine strGroup
Next
objOutputFile.WriteLine "*************************************************************"

WScript.Echo "Finished creating file"
objOutputFile.Close

'Open file
strOpenFile = MsgBox("Do you want to open the output file?", VbYesNo + VBQuestion, "Open Output File?")
If strOpenFile = VbYes Then
	objShell.Run("notepad.exe " & DQ & strOutputFilePath & DQ)
End If

Function GetType(ByVal lngFlag)
	' Function to determine group type.
	If ((lngFlag And ADS_GROUP_TYPE_SECURITY_ENABLED) <> 0) Then
		GetType = "Security"
	Else
		GetType = "Distribution"
	End If
End Function

Open in new window

0
Comment
Question by:seaninman
8 Comments
 
LVL 4

Expert Comment

by:mikesuss
ID: 33570222
http://www.computerperformance.co.uk/Logon/code/code_8000500D.htm


Sounds like a typo in ldap reference, or the ldap changed names
0
 
LVL 5

Expert Comment

by:talkinsmak
ID: 33570236
Your VBScript contains an illegal LDAP reference, probably a typing mistake, maybe an extra letter.  Check the spelling of your objects in the script.
 
What does the error message say is the source?
 
0
 
LVL 4

Author Comment

by:seaninman
ID: 33571077
Source is Active Directory.  However it doesn't error when i run it on other user account's.  Just this one particular account.  
0
 
LVL 4

Author Comment

by:seaninman
ID: 33571150
looks like it errors on line 82 when its trying to get the last login in time stamp.  If I comment that out it runs fine but I need that information.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:seaninman
ID: 33571264
Okay so I found this which makes since.  http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_23906325.html

However, would someone be able to help me figure out a way to update this script so that if it gets that error on that line that it will not error out that it will simply put User has Never Logged In?
0
 
LVL 16

Expert Comment

by:ThinkPaper
ID: 33571674
Then you would need to include some error handling.
You would need to include something like this.......

http://technet.microsoft.com/en-us/library/ee692852.aspx
Throw this line on the top of your code:

On Error Resume Next



Then right under your line 82:

'=====The user never logged on so timestamp does not exist========

If Err.Number <> 0 Then                  

    WScript.Echo "User has never logged in"

    ' It doesn't exist so you are skipping that portion of code and making the default values NULL or blank 



    intLastLogonTime = ""

    dtmLastLogon = ""



     Err.Clear



'=====The user logged on, so timestamp exists =======

Else                                                 



   'Format Last Logon Time Stamp

  Set objLastLogon = objUser.Get("lastLogonTimestamp")

  intLastLogonTime = objLastLogon.HighPart * (2 ^ 32) + objLastLogon.LowPart

  intLastLogonTime = intLastLogonTime / (60 * 10000000)

  intLastLogonTime = intLastLogonTime / 1440

  dtmLastLogon = intLastLogonTime + # 1 / 1 / 1601 #





End If



' The rest of your code (lines 87 and up)

Open in new window

0
 
LVL 4

Author Comment

by:seaninman
ID: 33590148
Thats good, but I'd like to have it write in the output file on the line for last logon if it doesnt exist then have it say User has never logged on.
0
 
LVL 16

Accepted Solution

by:
ThinkPaper earned 500 total points
ID: 33875880
Then you should simply modify dtmLastLogon, so instead of blank  it displays what you want it to print out:

dtmLastLogon = "USER NEVER LOGGED ON"

Then continue with the rest of the code...
'=====The user never logged on so timestamp does not exist========

If Err.Number <> 0 Then                  

    WScript.Echo "User has never logged in"

    ' It doesn't exist so you are skipping that portion of code and making the default values NULL or blank 



    intLastLogonTime = ""

    dtmLastLogon = "USER NEVER LOGGED ON"  'Change it right here to print this out



     Err.Clear



'=====The user logged on, so timestamp exists =======

Else                                                 



   'Format Last Logon Time Stamp

  Set objLastLogon = objUser.Get("lastLogonTimestamp")

  intLastLogonTime = objLastLogon.HighPart * (2 ^ 32) + objLastLogon.LowPart

  intLastLogonTime = intLastLogonTime / (60 * 10000000)

  intLastLogonTime = intLastLogonTime / 1440

  dtmLastLogon = intLastLogonTime + # 1 / 1 / 1601 #





End If

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now