 |
|
| IMPORTANT: PowerShellCommunity.org is moving! - Wednesday, August 15, 2012PowerShellCommunity.org is moving! This community software, and the hardware that it sits on, are no longer serving the purposes of this community. As a result, we have decided to move this community to a new home at PowerShell.org. PowerShell.org is already up and running with the new community software and in its new location, so please post any new questions that you have on the forums over there instead of posting them on this site. We've already started getting some great questions from members of the community over there so please, come on over and join us!
While we are going through this transition, this site will remain up for the short term. New posts may no longer be created on these forums, however replies to existing posts are allowed so that users who posted questions don't have to re-post the same question on the new site.
[UPDATE 28/02/2013] New user registration has been disabled and forums have now been switched to read-only, including for existing posts since all threads that were started should now be completed. If you have a question about content on this site or about PowerShell in general, head over to PowerShell.org and ask it there where there are people actively using the site and answering questions.
If you have any questions, please let us know on the PowerShell.org site.
Thank you,
Kirk "Poshoholic" Munro |
|
|
|
|
Extract lastlogon AD attrib from each DC on domain and select Inactive Accounts that have not logged on for 30 days
Last Post 24 Feb 2011 01:54 AM by jvc. 2 Replies.
|
Sort:
|
|
Prev Next |
You are not authorized to post a reply. |
|
get-james
 New Member Posts:59

 |
| 30 Jan 2011 12:32 AM |
|
All,
I have created the following PS script which extracts the following AD attribs from each DC, so you can select inactive accounts:
samaccountname, lastlogon, pwdlastset, lastlogontimestamp
I have tested this on PS2.0 agaist Win2003 & Win2008R2 DCs.
If you can see any improvments or changes that would make it better, I would love to know:
#Get all domain controllers for the current domain $StrDCs = [System.DirectoryServices.ActiveDirectory.Domain]::getcurrentdomain() | foreach{$_.DomainControllers| foreach{$_.Name}} $StrAllResults = $null $StrDCs | foreach-object { $Erroractionpreference = "Silentlycontinue" $strFilter = "(&(objectCategory=person)(objectClass=user))" $StrEachDC = "LDAP://"+$_.split(".")[0] $objDomain = New-Object System.DirectoryServices.DirectoryEntry $StrEachDC $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.PageSize = 100 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = "Subtree" $strProps = "samaccountname,lastlogon,pwdlastset,lastlogontimestamp" $strProps.split(",") | %{[void]$objSearcher.PropertiesToLoad.Add($_);} $StrResults = $objSearcher.findall() $Erroractionpreference = "Continue" $StrAllResults += $StrResults Write-host "Finshed extracting information from $_" } # Group Account per samaccountName $StrAllResultsGrouped = $null $StrAllResultsGrouped = $StrAllResults | Group {$_.Properties.samaccountname}
# Format results at get date values $StrAllResultsMostRecentPerDC = $null $StrAllResultsMostRecentPerDC = $StrAllResultsGrouped | Foreach-object {$_.Group | Foreach-object ` { $_ | Select-Object ` @{n="LogonDomainController";Expression={($_ | select path).path.split("/")[2]}}, @{n="samaccountname";Expression={$_.properties.samaccountname}}, @{n="pwdlastset";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.pwdlastset)))}}, @{n="lastlogon";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.lastlogon)))}}, @{n="Lastlogontimestamp";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.lastlogontimestamp)))}} } } # Only select the most recent pwdlastset,lastlogon & Lastlogontimestamp for each samaccountname $StrAllResultsMostRecent = $null $StrAllResultsMostRecent = $StrAllResultsMostRecentPerDC | Group {$_.samaccountname} | Foreach-object ` { $StrTemp = @();$_.Group | Foreach-object {$StrTemp += $_} "" | Select-Object ` @{n="samaccountname";Expression={ $StrTemp[0].samaccountname}}, @{n="pwdlastset";Expression={($StrTemp | Select-Object pwdlastset | Sort-Object {[datetime]::ParseExact($_.pwdlastset,'dd/MM/yyyy HH:mm',$null)} -Descending | Select-Object -First 1).pwdlastset }}, @{n="lastlogon";Expression={($StrTemp | Select-Object lastlogon | Sort-Object {[datetime]::ParseExact($_.lastlogon,'dd/MM/yyyy HH:mm',$null)} -Descending | Select-Object -First 1).lastlogon }}, @{n="Lastlogontimestamp";Expression={($StrTemp | Select-Object Lastlogontimestamp | Sort-Object {[datetime]::ParseExact($_.Lastlogontimestamp,'dd/MM/yyyy HH:mm',$null)} -Descending | Select-Object -First 1).Lastlogontimestamp}} } Write-Host "Total account in AD: "$StrAllResultsMostRecent.count
#Selecting only accounts that have not logged on for 30 days $StrAllResultsInActiveAccount = $StrAllResultsMostRecent | Where-Object {[datetime]::ParseExact($_.lastlogon,'dd/MM/yyyy HH:mm',$null) -le (date).adddays(-30)} Write-Host "Total accounts that have not logged onto AD in 30 days: "$StrAllResultsInActiveAccount.count # Do something with these AD account: $StrAllResultsInActiveAccount
Cheers James
|
SelectInactiveAccountFromAllDCs.txt
|
|
|
|
get-james
 New Member Posts:59

 |
| 31 Jan 2011 02:56 AM |
|
Also, if you only want to check the results for one user, just run the following commands: #Single user test: $StrAllResults | Where {$_.Properties.samaccountname -eq "User01"} | Foreach-object ` { $_ | Select-Object ` @{n="LogonDomainController";Expression={($_ | select path).path.split("/")[2]}}, @{n="UserName";Expression={$_.properties.samaccountname}}, @{n="pwdlastset";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.pwdlastset)))}}, @{n="lastlogon";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.lastlogon)))}}, @{n="Lastlogontimestamp";Expression={"{0:dd/MM/yyyy HH:mm}" -f ([datetime]::fromfiletime([string]($_.Properties.lastlogontimestamp)))}} | sort lastlogon } | sort-object {[datetime]::ParseExact($_.lastlogon,'dd/MM/yyyy HH:mm',$null)} -des | format-List LogonDomainController : DC01 UserName : User01 pwdlastset : 31/12/2010 10:17 lastlogon : 31/12/2010 10:17 Lastlogontimestamp : 31/12/2010 10:17 LogonDomainController : DC02 UserName : User01 pwdlastset : 31/12/2010 10:17 lastlogon : 31/12/2010 10:17 Lastlogontimestamp : 31/12/2010 10:17 LogonDomainController : DC03 UserName : User01 pwdlastset : 31/12/2010 10:17 lastlogon : 01/01/1601 00:00 Lastlogontimestamp : 31/12/2010 10:17
|
|
|
|
|
jvc
 New Member Posts:1

 |
| 24 Feb 2011 01:54 AM |
|
Hi, James very good script, but i'm having problems when the samaccountname is Null in AD the user doesn't get returned, any other solution to return all the users?
thanks in advance Joe
Update:
Just figured it out, removing the (objectCategory=person) from the $strFilter= does the job ;)
thanks |
|
|
|
|
| You are not authorized to post a reply. |
|
Active Forums 4.3
|
|
 |