jbruns2004
asked on
Excluding missing values but retaining the rest.
I have a vbscript that I use to unload popular attributes for users. It outputs the data in a csv file. I want to add the lastlogontimestamp to the file. I got the additional code to work but for certain users it seems at certain times, their last logon attribute is missing a value which causes the script to fail with the message,
Active Directory: The directory property cannot be found in the cache.
The 2nd line below is causing the failure.
>>>
Set objUser = GetObject(objRecordSet.Fie lds("ADsPa th").Value )
Set objLastLogon = objUser.Get("lastLogonTime stamp")
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
LogFile.WriteLine _
strDomain & "," & _
objUser.sAMAccountName & "," & _
strDomain & "\" & objUser.sAMAccountName & "," & _
objUser.Name & "," & _
objUser.HomeDirectory & "," & _
objUser.TerminalServicesHo meDirector y & "," & _
objUser.TerminalServicesPr ofilePath & "," & _
objUser.TerminalServicesHo meDrive & "," & _
objUser.ProfilePath & "," & _
objUser.ScriptPath & "," & _
objUser.HomeDrive & "," & _
objUser.AllowLogon & "," & _
objUser.AccountDisabled & "," & _
Chr(34) & strDN & chr(34) & "," & _
intLastLogonTime + #1/1/1601#
Set objUser = nothing
>>>
I know I can add "On Error Resume Next" to the code to bypass these types of errors but I will be missing that users record in the csv file just because the lastlogontimestamp has a missing value.
In other words, is there a way to code this so that if the lastlogontimestamp attribute or any other for that matter, is missing, output the rest of the attributes that do have values?
Active Directory: The directory property cannot be found in the cache.
The 2nd line below is causing the failure.
>>>
Set objUser = GetObject(objRecordSet.Fie
Set objLastLogon = objUser.Get("lastLogonTime
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
LogFile.WriteLine _
strDomain & "," & _
objUser.sAMAccountName & "," & _
strDomain & "\" & objUser.sAMAccountName & "," & _
objUser.Name & "," & _
objUser.HomeDirectory & "," & _
objUser.TerminalServicesHo
objUser.TerminalServicesPr
objUser.TerminalServicesHo
objUser.ProfilePath & "," & _
objUser.ScriptPath & "," & _
objUser.HomeDrive & "," & _
objUser.AllowLogon & "," & _
objUser.AccountDisabled & "," & _
Chr(34) & strDN & chr(34) & "," & _
intLastLogonTime + #1/1/1601#
Set objUser = nothing
>>>
I know I can add "On Error Resume Next" to the code to bypass these types of errors but I will be missing that users record in the csv file just because the lastlogontimestamp has a missing value.
In other words, is there a way to code this so that if the lastlogontimestamp attribute or any other for that matter, is missing, output the rest of the attributes that do have values?
Use "On Error Resume Next", but break your write into many lines:
outputLine = strDomain & ","
outputLine = outputLine & objUser.sAMAccountName
outputLine = outputLine & ","
outputLine = outputLine & strDomain & "\" & objUser.sAMAccountName
outputLine = outputLine & ","
outputLine = outputLine & objUser.Name
outputLine = outputLine & ","
outputLine = outputLine & objUser.HomeDirectory
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesHo meDirector y
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesPr ofilePath
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesHo meDrive
outputLine = outputLine & ","
outputLine = outputLine & objUser.ProfilePath
outputLine = outputLine & ","
outputLine = outputLine & objUser.ScriptPath
outputLine = outputLine & ","
outputLine = outputLine & objUser.HomeDrive
outputLine = outputLine & ","
outputLine = outputLine & objUser.AllowLogon
outputLine = outputLine & ","
outputLine = outputLine & objUser.AccountDisabled
outputLine = outputLine & ","
outputLine = outputLine & Chr(34) & strDN & chr(34) & ","
outputLine = outputLine & intLastLogonTime & #1/1/1601#
LogFile.WriteLine outputLine
outputLine = strDomain & ","
outputLine = outputLine & objUser.sAMAccountName
outputLine = outputLine & ","
outputLine = outputLine & strDomain & "\" & objUser.sAMAccountName
outputLine = outputLine & ","
outputLine = outputLine & objUser.Name
outputLine = outputLine & ","
outputLine = outputLine & objUser.HomeDirectory
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesHo
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesPr
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesHo
outputLine = outputLine & ","
outputLine = outputLine & objUser.ProfilePath
outputLine = outputLine & ","
outputLine = outputLine & objUser.ScriptPath
outputLine = outputLine & ","
outputLine = outputLine & objUser.HomeDrive
outputLine = outputLine & ","
outputLine = outputLine & objUser.AllowLogon
outputLine = outputLine & ","
outputLine = outputLine & objUser.AccountDisabled
outputLine = outputLine & ","
outputLine = outputLine & Chr(34) & strDN & chr(34) & ","
outputLine = outputLine & intLastLogonTime & #1/1/1601#
LogFile.WriteLine outputLine
Sorry WillEastbury, I didn't refresh.
One difference I see though is that my snippet will actually write a blank field if an error occurs since I have the "," being added on a different line.
One difference I see though is that my snippet will actually write a blank field if an error occurs since I have the "," being added on a different line.
ASKER
Thanks for the help.
This worked too,
Set objUser = GetObject(objRecordSet.Fie lds("ADsPa th").Value )
On Error Resume Next
Set objLastLogon = objUser.Get("lastLogonTime stamp")
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
On Error Goto 0
LogFile.WriteLine _
strDomain & "," & _
objUser.sAMAccountName & "," & _
strDomain & "\" & objUser.sAMAccountName & "," & _
objUser.Name & "," & _
objUser.HomeDirectory & "," & _
objUser.TerminalServicesHo meDirector y & "," & _
objUser.TerminalServicesPr ofilePath & "," & _
objUser.TerminalServicesHo meDrive & "," & _
objUser.ProfilePath & "," & _
objUser.ScriptPath & "," & _
objUser.HomeDrive & "," & _
objUser.AllowLogon & "," & _
objUser.AccountDisabled & "," & _
Chr(34) & strDN & chr(34) & "," & _
intLastLogonTime + #1/1/1601#
Set objUser = nothing
This worked too,
Set objUser = GetObject(objRecordSet.Fie
On Error Resume Next
Set objLastLogon = objUser.Get("lastLogonTime
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
On Error Goto 0
LogFile.WriteLine _
strDomain & "," & _
objUser.sAMAccountName & "," & _
strDomain & "\" & objUser.sAMAccountName & "," & _
objUser.Name & "," & _
objUser.HomeDirectory & "," & _
objUser.TerminalServicesHo
objUser.TerminalServicesPr
objUser.TerminalServicesHo
objUser.ProfilePath & "," & _
objUser.ScriptPath & "," & _
objUser.HomeDrive & "," & _
objUser.AllowLogon & "," & _
objUser.AccountDisabled & "," & _
Chr(34) & strDN & chr(34) & "," & _
intLastLogonTime + #1/1/1601#
Set objUser = nothing
ASKER
Minor correction to Idle_Mind solution.
outputLine = outputLine & intLastLogonTime & #1/1/1601#
should be
outputLine = outputLine & intLastLogonTime + #1/1/1601#
Plus sign instead of & sign.
Wish I knew what the + #1/1/1601# does.
outputLine = outputLine & intLastLogonTime & #1/1/1601#
should be
outputLine = outputLine & intLastLogonTime + #1/1/1601#
Plus sign instead of & sign.
Wish I knew what the + #1/1/1601# does.
It's a date literal to be added to whatever is in intlastlogontime, and it basically means never .. So if your program outputs 1/1/1601 for a date, then your user has never logged on to that DC.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I also had to add "On Error Goto 0" before any writeline code. It would trip up and not display any information for a user if there was a field with a missing value.
Try this.
>>>
On Error Resume Next
Set objUser = GetObject(objRecordSet.Fie
Set objLastLogon = objUser.Get("lastLogonTime
intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
s = s & strDomain & ","
s = s & objUser.sAMAccountName & ","
s = s & strDomain & "\" & objUser.sAMAccountName & ","
s = s & objUser.Name & ","
s = s & objUser.HomeDirectory & ","
s = s & objUser.TerminalServicesHo
s = s & objUser.TerminalServicesPr
s = s & objUser.TerminalServicesHo
s = s & objUser.ProfilePath & ","
s = s & objUser.ScriptPath & ","
s = s & objUser.HomeDrive & ","
s = s & objUser.AllowLogon & ","
s = s & objUser.AccountDisabled & ","
s = s & Chr(34) & strDN & chr(34) & ","
s = s & intLastLogonTime + #1/1/1601#
Set objUser = nothing
LogFile.WriteLine s