Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function Get-InactiveUsers {
  2. [CmdletBinding()]
  3. param (
  4. # Distinguished Names of search bases
  5. [Parameter(Mandatory=$true)]
  6. [String[]]
  7. $DistinguishedName,
  8.  
  9. # Parameter help description
  10. [Parameter(Mandatory=$false)]
  11. [int]
  12. $LastLogonTreshold = 10
  13.  
  14. )
  15.  
  16. begin {
  17. Import-Module ActiveDirectory -ErrorVariable moduleError -ErrorAction SilentlyContinue
  18. if ($moduleError) {
  19. Write-Error $moduleError.Exception.Message -ErrorAction Stop
  20. }
  21.  
  22. function Get-LastLogonTimeSpan {
  23. param (
  24. # User Object
  25. [Parameter(Mandatory,ValueFromPipeline)]
  26. [Microsoft.ActiveDirectory.Management.ADUser]
  27. $UserObject
  28. )
  29. $lastLogonDateTime = [datetime]::FromFileTime($UserObject.lastLogonTimeStamp)
  30. $daysSinceLastLogon = [math]::Round(((New-TimeSpan -Start $lastLogonDateTime -End (Get-Date)).TotalDays),0)
  31. return $daysSinceLastLogon
  32. }
  33. }
  34.  
  35. process {
  36. $targetContainerArr = $DistinguishedName | ForEach-Object {Get-ADObject -Filter "DistinguishedName -eq '$_'"}
  37. [System.Collections.ArrayList]$returnObjArr = @()
  38.  
  39. foreach ($targetContainerObj in $targetContainerArr) {
  40. $returnObjArr = (Get-ADUser -SearchBase $targetContainerObj.DistinguishedName -Filter "Enabled -eq 'true'" -Properties lastLogonTimeStamp,DisplayName) | Where-Object {(Get-LastLogonTimeSpan -UserObject $_) -gt $LastLogonTreshold} | Select-Object -Property Name,DisplayName,@{Name = "ContainerDN";Expression = {$targetContainerObj.DistinguishedName}},@{Name = "DaysSinceLastLogon";Expression = {Get-LastLogonTimeSpan -UserObject $_}}
  41. }
  42. }
  43.  
  44. end {
  45. return $returnObjArr
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement