Skip to content

Instantly share code, notes, and snippets.

@techdecline
Last active August 15, 2023 12:52
Show Gist options
  • Save techdecline/cfb00dd1ecedb1e62f0f5c088208dd96 to your computer and use it in GitHub Desktop.
Save techdecline/cfb00dd1ecedb1e62f0f5c088208dd96 to your computer and use it in GitHub Desktop.

Revisions

  1. techdecline revised this gist Oct 1, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Get-InactiveUsers.ps1
    Original 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.lastLogon)
    $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 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 $_}}
    $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 $_}}
    }
    }

  2. techdecline created this gist Oct 1, 2019.
    47 changes: 47 additions & 0 deletions Get-InactiveUsers.ps1
    Original 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
    }
    }