Created
October 28, 2019 21:16
-
-
Save spy86/069a73dae08b5017be3e30aa739564e5 to your computer and use it in GitHub Desktop.
Powershell script to List User Last Logon
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 characters
| param( | |
| $domain, | |
| $users ) #end param | |
| # Begin Functions | |
| Function funWhatIf() | |
| { | |
| "what if: Perform operation obtain lastlogon time for user $user from | |
| the $domain domain" | |
| exit | |
| } #end funWhatIf | |
| function funGetAllUsers() | |
| { | |
| $attribute = "lastlogon" | |
| $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$domain") | |
| $searcher.filter = "(&(objectClass=user)(objectCategory=person))" | |
| $users = $searcher.findall() | |
| foreach($user in $users) | |
| { | |
| if($user.properties.item("$attribute") -ne 0) | |
| { | |
| $attributeValue = [datetime]::FromFileTime([int64]::Parse($user.properties.item($attribute))) | |
| "$($user.properties.item(`"name`")) $attributeValue" | |
| } #end if user | |
| } #end foreach user | |
| } #end funGetAllUsers | |
| function funGetUsers() | |
| { | |
| $attribute = "lastlogon" | |
| $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$domain") | |
| ForEach($suser in $users) | |
| { | |
| $searcher.filter = "(&(objectClass=user)(sAMAccountName=$suser))" | |
| $colusers = $searcher.findall() | |
| foreach($user in $colUsers) | |
| { | |
| if($user.properties.item("$attribute") -ne 0) | |
| { | |
| $attributeValue = [datetime]::FromFileTime([int64]::Parse($user.properties.item($attribute))) | |
| "$($user.properties.item(`"name`")) $attributeValue" | |
| } #end if user | |
| } #end foreach user | |
| } #end foreach suser | |
| } #end funGetUsers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment