Solved

Issues with a VBScript

Posted on 2010-08-31
8
1,003 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

617 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