Question : VBScript: Pulling lastLogonTimestamp

Hello Experts!

     I have the functionality of what I'm needing within a DSQUERY and PowerShell, however I think what needs to be done in my particular case is to break the DN path down in arrays so that each OU level is readable.  For example, the present results file that I have for this contains roughly around 75k rows and the DN path can average anwhere from 3 to 6 OU's deep.  So what ends up happening is that some of the other column data will find itself in an OU column or even another column, so when I go to sort it throws the whole thing off.  By properly breaking down the OU's in an array or some sort of split funtion that it will line each OU up in the columns correctly.  Basically the data isn't being columned correctly throughout some of the spreadsheet.

    So with this script I would be wanting to pull the sAMAccountName, DisplayName, UPN and LogonTimestamp for each active user in my AD environment.  

    The present result files that I'm getting back are in .CSV format which includes the comma in the DN path, so it's difficult to separate them to distinguish the OU's.  So I then converted the CSV to a TSV and then set the special operator as a ", "  in the import and this did separate the OU columns out,but as I said because of how deep the OU's go, it causing some of the other data i.e. UPN's, sAMAccountName, LogonTimestamps to carry over into other columns.    Any help with this is greatly appreciated.  

Answer : VBScript: Pulling lastLogonTimestamp


Actually use of the BU list needs a small modification to prevent it being horribly unreliable.

This is the modification:

ForEach ($Name in $BU) { If ($_.DN -Match "OU=$Name,") { $Name } }

With the original it would have matched you to IT if your name included that, with this it'll only match if the complete OU name is IT (hence the OU= prefix and the , suffix).

Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
# Requires Quest CmdLets: http://www.quest.com/powershell/activeroles-server.aspx

# Business Unit List
$BU = "Finance", "Sales", "Marketing", "IT", "Development"

# The interval between dates. Will produce these in the final column: 0 to 30; 30 to 60, etc.
$DateRange = 30

# Use Quest CmdLets to get the users
Get-QADUser -Enabled -IncludedProperties lastLogonTimeStamp -SizeLimit 0 | %{
  # Hold onto this for convenience
  $LastLogon = $_.LastLogonTimeStamp

  # Reset the IntervalString and Multiplier values
  # Starts by looking for dates less than 30 days old, incremented by 30 on each pass of the loop
  $IntervalString = ""; $Multiplier = 1

  If ($LastLogon -ne $Null) {
    Do {
      # See if the logon date is after the specified date
      If ($LastLogon -gt (Get-Date).AddDays(-($DateRange * $Multiplier))) {

        # Record this value as "0 to 30", or "30 to 60", etc
        $IntervalString = "$($DateRange * ($Multiplier - 1)) to $($DateRange * $Multiplier)"
      }

      # Increment the multiplier
      $Multiplier++

    # Until it has a value
    } While ($IntervalString -eq "")
  } Else {
    $IntervalString = "N/A"
  }

  # Generate the output
  $_ | Select-Object DN, UserPrincipalName, DisplayName, LastLogonTimeStamp, WhenCreated,
    @{n='BusinessUnit';e={ ForEach ($Name in $BU) { If ($_.DN -Match "OU=$Name,") { $Name } } }},
    @{n='Range';e={ $IntervalString }}
# Export it all to a CSV using Tab as a delimiter
} | Export-CSV "SomeFile.csv" -NoTypeInformation -Delimiter `t
Random Solutions  
 
programming4us programming4us