gangstanthony

finding_disabled_ad_users_in_the_past_30_days

Jun 6th, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # https://www.reddit.com/r/PowerShell/comments/4mtg6a/finding_disabled_ad_users_in_the_past_30_days_and/
  2.  
  3. param (
  4.     [parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, mandatory=$false)]
  5.     [string]$SearchBase,
  6.     [parameter(Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, mandatory=$false)]
  7.     [int]$Days
  8. )
  9.  
  10. Import-Module ActiveDirectory
  11.  
  12. if ($searchBase -eq '') {
  13.     $searchBase = (Get-ADRootDSE).defaultNamingContext;
  14. }
  15.  
  16. if ($Days -lt 1) {
  17.     $Days = 1
  18. }
  19.  
  20. $Days *= -30
  21.  
  22. $users = Get-ADObject -Filter { objectCategory -eq 'Person' -and (userAccountControl -bor 2) } -SearchBase $SearchBase -Properties sAMAccountName
  23.  
  24. $searchFrom = (Get-Date 0:0).AddDays($Days)
  25.  
  26. $results = New-Object System.Collections.ArrayList
  27. $index = 0
  28. $total = $users.Count
  29. $starttime = $lasttime = Get-Date
  30. foreach ($userEntry in $users) {
  31.     $index++
  32.     $currtime = (Get-Date) - $starttime
  33.     $avg = $currtime.TotalSeconds / $index
  34.     $last = ((Get-Date) - $lasttime).TotalSeconds
  35.     $left = $total - $index
  36.     $WrPrgParam = @{
  37.         Activity = (
  38.             "Getting user info $(Get-Date -f s)",
  39.             "Total: $($currtime -replace '\..*')",
  40.             "Avg: $('{0:N2}' -f $avg)",
  41.             "Last: $('{0:N2}' -f $last)",
  42.             "ETA: $('{0:N2}' -f ($avg * $left / 60))",
  43.             "min ($([string](Get-Date).AddSeconds($avg * $left) -replace '^.* '))"
  44.         ) -join ' '
  45.         Status = "$index of $total ($left left) [$('{0:N2}' -f ($index / $total * 100))%]"
  46.         CurrentOperation = "User: $userEntry"
  47.         PercentComplete = $index / $total * 100
  48.     }
  49.     Write-Progress @WrPrgParam
  50.     $lasttime = Get-Date
  51.  
  52.     $user = Get-ADObject -Filter { sAMAccountName -eq $userEntry.sAMAccountName } -Properties sAMAccountName, userAccountControl, mail, 'msDS-ReplAttributeMetaData'
  53.     $repData = $repData = [xml]('<root>' + $user.'msDS-ReplAttributeMetaData' + '</root>').Replace([char]0, ' ')
  54.  
  55.     foreach ($attribute in $repData.root.DS_REPL_ATTR_META_DATA) {
  56.         if ($attribute.pszAttributeName -eq 'userAccountControl') {
  57.             $changedDate = Get-Date $attribute.ftimeLastOriginatingChange
  58.  
  59.             if ($changedDate -gt $searchFrom) {
  60.                 $obj = [pscustomobject]@{
  61.                     Mail = $user.mail
  62.                     sAMAccountName = $user.samaccountname
  63.                     UserAccountControl = $user.useraccountcontrol
  64.                     Changed = $changedDate
  65.                 }
  66.                 Write-Output $obj
  67.                 $null = $results.Add($obj)
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. $smartHost = 'smtp.server.com'
  74. $sendTo = '[email protected]' #Multiple values separated by comma
  75. $sendFrom = '[email protected]'
  76. $mailSubject = 'PowerShell Disabled Users in Past 30 Days'
  77. $mailBody = $results | Format-Table -AutoSize | Out-String
  78. Send-MailMessage -Subject $mailSubject -From $sendFrom -To $sendTo -SmtpServer $smartHost -body $mailBody
Add Comment
Please, Sign In to add comment