Answered by:
lastlogintimestamp from active directory

Question
-
Hi
need a cmdlet to get lastlogintimestamp of currently logged in user in system from AD.
get-aduser does not work , it gives below error
The term 'get-aduser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that th
e path is correct and try again.
At line:1 char:11
+ get-aduser <<<< $userName -properties lastlogon
+ CategoryInfo : ObjectNotFound: (get-aduser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Regards Sushain KApoor
Friday, November 20, 2015 6:14 AM
Answers
-
Start here:
Import-Module Activedirectory
Get-aduser ($env:UserName) -properties LastLogonDate | Select lastLogonDateStart by learning the basics. Everything will be easier after that.
\_(ツ)_/
- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 9:44 AM -
Sushain,
Use LastLogonDate instead of LastLogonTimeStamp as I mentioned in my previous post.
$x = $env:UserName
get-aduser -filter "SamAccountName -like '*$x*'" -properties Name, LastLogonDate| sort-object name | select Name, LastLogonDate
Naveen Basati
- Edited by BASATI Friday, November 20, 2015 10:06 AM Modification
- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 10:06 AM -
Use LastLogonData like Basati states and use 'get-date' for the totalseconds.
#OLD Import-Module Activedirectory $x = [Environment]::UserName $lts = get-aduser -filter "SamAccountName -like '$x'" -properties LastLogonTimeStamp | Select lastLogonTimeStamp $lts $ln = [datetime]::FromFileTime($lts) $ln $r = Get-Date $r | Out-File c:\test\r12.txt $time = $r.TimeOfDay.TotalSeconds - $ln.TimeOfDay.TotalSeconds $time | Out-File c:\test\time.txt #NEW Import-Module Activedirectory $x = [Environment]::UserName $aduser = get-aduser -filter "SamAccountName -like '$x'" -properties LastLogonDate $currentdate = get-date $lastlogontotalseconds = ($currentdate - $aduser.lastlogondate).totalseconds
Cheers,
Ruud
Twitter:Blog: www.ruudborst.nl LinkedIn:
Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 10:13 AM
All replies
-
Hi Sushan,
Whether you are importing Active directory module in your script. I am providing example to search the user and last logon date using samaccountname
import-module activedirectory
$x = 'username'
get-aduser -filter "SamAccountName -like '*$x*'" -properties Name, LastLogonDate| sort-object name | select Name, LastLogonDate
Naveen Basati
Friday, November 20, 2015 6:59 AM -
Hi
I have used your syntax, individually it works good ! Thanks !
i have added few cmdlets of mine as well, but getting the error, have you seen
Import-Module Activedirectory
$x = [Environment]::UserName
$lts =
get-aduser -filter "SamAccountName -like '$x'" -properties LastLogonTimeStamp | Select lastLogonTimeStamp
$lts
$ln = [datetime]::FromFileTime($lts)
$ln
$r = Get-Date
$r | Out-File c:\test\r12.txt
$time = $r.TimeOfDay.TotalSeconds - $ln.TimeOfDay.TotalSeconds
$time | Out-File c:\test\time.txttill $ln i am getting the below error
Cannot convert argument "fileTime", with value: "@{lastLogonTimeStamp=130919884537722368}", for "FromFileTime" to type
"System.Int64": "Cannot convert value "@{lastLogonTimeStamp=130919884537722368}" to type "System.Int64". Error: "Cannot convert the
"@{lastLogonTimeStamp=130919884537722368}" value of type "Selected.Microsoft.ActiveDirectory.Management.ADUser" to type
"System.Int64".""
At C:\Users\admska327\AppData\Local\Temp\2\21a2f732-2d44-4080-aa2e-de47dd64ac88.ps1:6 char:1
+ $ln = [datetime]::FromFileTime($lts)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Regards Sushain KApoor
- Edited by Sushain_Kapoor Friday, November 20, 2015 9:00 AM
Friday, November 20, 2015 8:59 AM -
Start here:
Import-Module Activedirectory
Get-aduser ($env:UserName) -properties LastLogonDate | Select lastLogonDateStart by learning the basics. Everything will be easier after that.
\_(ツ)_/
- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 9:44 AM -
Sushain,
Use LastLogonDate instead of LastLogonTimeStamp as I mentioned in my previous post.
$x = $env:UserName
get-aduser -filter "SamAccountName -like '*$x*'" -properties Name, LastLogonDate| sort-object name | select Name, LastLogonDate
Naveen Basati
- Edited by BASATI Friday, November 20, 2015 10:06 AM Modification
- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 10:06 AM -
Use LastLogonData like Basati states and use 'get-date' for the totalseconds.
#OLD Import-Module Activedirectory $x = [Environment]::UserName $lts = get-aduser -filter "SamAccountName -like '$x'" -properties LastLogonTimeStamp | Select lastLogonTimeStamp $lts $ln = [datetime]::FromFileTime($lts) $ln $r = Get-Date $r | Out-File c:\test\r12.txt $time = $r.TimeOfDay.TotalSeconds - $ln.TimeOfDay.TotalSeconds $time | Out-File c:\test\time.txt #NEW Import-Module Activedirectory $x = [Environment]::UserName $aduser = get-aduser -filter "SamAccountName -like '$x'" -properties LastLogonDate $currentdate = get-date $lastlogontotalseconds = ($currentdate - $aduser.lastlogondate).totalseconds
Cheers,
Ruud
Twitter:Blog: www.ruudborst.nl LinkedIn:
Note: Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.- Marked as answer by Sushain_Kapoor Friday, November 20, 2015 2:54 PM
Friday, November 20, 2015 10:13 AM -
Thanks !!
Regards Sushain KApoor
Friday, November 20, 2015 2:54 PM