Advertisement
Guest User

Script to display last password change of computer account.

a guest
May 22nd, 2012
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #written by pan_2.livejournal.com
  2. function Get-DomainComputersLastPasswordChange
  3.  
  4.     {
  5.    
  6.     $hComputerAccounts = @{}
  7.  
  8.     # Use Directory Services object to attach to the domain
  9.     $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"")
  10.     #Leaving the ADSI statement empty = attach to your root domain
  11.  
  12.     # Filter down to computer accounts
  13.     # $searcher.filter = "(&(objectClass=computer))"
  14.     $searcher.filter = "(&(objectCategory=computer)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
  15.  
  16.     # Cache the results
  17.     $searcher.CacheResults = $true
  18.     $searcher.SearchScope = “Subtree”
  19.     $searcher.PageSize = 1000
  20.  
  21.     # Find anything you can that matches the definition of being a computer object
  22.     $accounts = $searcher.FindAll()
  23.  
  24.     # Check to make sure we found some accounts
  25.     if($accounts.Count -gt 0)
  26.         {
  27.         foreach($account in $accounts)
  28.             {
  29.             # Property that contains the last password change in long integer format
  30.             $pwdlastset = $account.Properties["pwdlastset"];
  31.  
  32.             # Convert the long integer to normal DateTime format
  33.             $lastchange = [datetime]::FromFileTimeUTC($pwdlastset[0]);
  34.  
  35.             # Determine the timespan between the two dates
  36.             $datediff = new-TimeSpan $lastchange $(Get-Date);
  37.  
  38.             $hComputerAccounts[$account.Properties["name"][0]] = $lastChange
  39.             }
  40.         }
  41.  
  42. $hComputerAccounts.GetEnumerator() | sort -prop Value | select @{name='ComputerName';Expression={$_.Name}}, @{Name="LastPasswordChange";Expression={$_.value}}
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement