Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement