Link to home
Start Free TrialLog in
Avatar of jbruns2004
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.Fields("ADsPath").Value)
Set objLastLogon = objUser.Get("lastLogonTimestamp")
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.TerminalServicesHomeDirectory & "," & _
      objUser.TerminalServicesProfilePath & "," & _
      objUser.TerminalServicesHomeDrive & "," & _
      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?
Avatar of WillEastbury
WillEastbury

As you've stated, the On Error Resume Next flag is the way to go, but it should be coded so you don't skip the concatenation line on an error.

Try this.
 >>>
On Error Resume Next
Set objUser = GetObject(objRecordSet.Fields("ADsPath").Value)
Set objLastLogon = objUser.Get("lastLogonTimestamp")
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.TerminalServicesHomeDirectory & ","
        s = s & objUser.TerminalServicesProfilePath & ","
        s = s & objUser.TerminalServicesHomeDrive & ","
        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
Avatar of Mike Tomlinson
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.TerminalServicesHomeDirectory
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesProfilePath
outputLine = outputLine & ","
outputLine = outputLine & objUser.TerminalServicesHomeDrive
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.
Avatar of jbruns2004

ASKER

Thanks for the help.

This worked too,

Set objUser = GetObject(objRecordSet.Fields("ADsPath").Value)
On Error Resume Next
Set objLastLogon = objUser.Get("lastLogonTimestamp")
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.TerminalServicesHomeDirectory & "," & _
objUser.TerminalServicesProfilePath & "," & _
objUser.TerminalServicesHomeDrive & "," & _
objUser.ProfilePath & "," & _
objUser.ScriptPath & "," & _
objUser.HomeDrive & "," & _
objUser.AllowLogon & "," & _
objUser.AccountDisabled & "," & _
Chr(34) & strDN & chr(34) & "," & _
intLastLogonTime + #1/1/1601#
Set objUser = nothing
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.

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
Avatar of WillEastbury
WillEastbury

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.