Last active
August 15, 2023 12:52
-
-
Save techdecline/cfb00dd1ecedb1e62f0f5c088208dd96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-InactiveUsers { | |
| [CmdletBinding()] | |
| param ( | |
| # Distinguished Names of search bases | |
| [Parameter(Mandatory=$true)] | |
| [String[]] | |
| $DistinguishedName, | |
| # Parameter help description | |
| [Parameter(Mandatory=$false)] | |
| [int] | |
| $LastLogonTreshold = 10 | |
| ) | |
| begin { | |
| Import-Module ActiveDirectory -ErrorVariable moduleError -ErrorAction SilentlyContinue | |
| if ($moduleError) { | |
| Write-Error $moduleError.Exception.Message -ErrorAction Stop | |
| } | |
| function Get-LastLogonTimeSpan { | |
| param ( | |
| # User Object | |
| [Parameter(Mandatory,ValueFromPipeline)] | |
| [Microsoft.ActiveDirectory.Management.ADUser] | |
| $UserObject | |
| ) | |
| $lastLogonDateTime = [datetime]::FromFileTime($UserObject.lastLogonTimeStamp) | |
| $daysSinceLastLogon = [math]::Round(((New-TimeSpan -Start $lastLogonDateTime -End (Get-Date)).TotalDays),0) | |
| return $daysSinceLastLogon | |
| } | |
| } | |
| process { | |
| $targetContainerArr = $DistinguishedName | ForEach-Object {Get-ADObject -Filter "DistinguishedName -eq '$_'"} | |
| [System.Collections.ArrayList]$returnObjArr = @() | |
| foreach ($targetContainerObj in $targetContainerArr) { | |
| $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 $_}} | |
| } | |
| } | |
| end { | |
| return $returnObjArr | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment