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