Last active
August 15, 2023 12:52
-
-
Save techdecline/cfb00dd1ecedb1e62f0f5c088208dd96 to your computer and use it in GitHub Desktop.
Revisions
-
techdecline revised this gist
Oct 1, 2019 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ function Get-InactiveUsers { [Microsoft.ActiveDirectory.Management.ADUser] $UserObject ) $lastLogonDateTime = [datetime]::FromFileTime($UserObject.lastLogonTimeStamp) $daysSinceLastLogon = [math]::Round(((New-TimeSpan -Start $lastLogonDateTime -End (Get-Date)).TotalDays),0) return $daysSinceLastLogon } @@ -37,7 +37,7 @@ function Get-InactiveUsers { [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 $_}} } } -
techdecline created this gist
Oct 1, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ 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.lastLogon) $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 lastLogon,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 } }