Solved

powershell command to identify when a computer account last logged onto the domain

Posted on 2014-10-01
4
352 Views
Last Modified: 2014-10-16
Hi all,
Using powershell anyone know the command to see when a computer or server that is currently powered on last logged onto the domain.

many thanks.
0
Comment
Question by:Jason Thomas
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
David Paris Vicente earned 334 total points
ID: 40354435
Hi,

I used this script in the past to check what you want.
And it work great if you several DC´s

$domain = "local" 
 
################## 
#--------Main 
################## 
 
import-module activedirectory 
cls 
"The domain is " + $domain 
$samaccountname = Read-Host 'What is the User samaccountname?' 
"Processing the checks ..." 
$myForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() 
$domaincontrollers = $myforest.Sites | % { $_.Servers } | Select Name 
$RealUserLastLogon = $null 
$LastusedDC = $null 
$domainsuffix = "*."+$domain 
foreach ($DomainController in $DomainControllers)  
{ 
    if ($DomainController.Name -like $domainsuffix ) 
    { 
        $UserLastlogon = Get-ADUser -Identity $samaccountname -Properties LastLogon -Server $DomainController.Name 
        if ($RealUserLastLogon -le [DateTime]::FromFileTime($UserLastlogon.LastLogon)) 
        { 
            $RealUserLastLogon = [DateTime]::FromFileTime($UserLastlogon.LastLogon) 
            $LastusedDC =  $DomainController.Name 
        } 
    } 
} 
"The last logon occured the " + $RealUserLastLogon + "" 
"It was done against " + $LastusedDC + "" 
$mesage = "............." 
$exit = Read-Host $mesage

Open in new window


Hope it helps.
0
 
LVL 1

Author Comment

by:Jason Thomas
ID: 40354848
Many thanks however I am not sure that is what I am looking for. I want to use power shell a use a command against one server at a time to see when it last logged on?
0
 
LVL 12

Assisted Solution

by:David Paris Vicente
David Paris Vicente earned 334 total points
ID: 40354912
OK.

The script that I provided is more simple because the you will need to convert the last logon date and time to a readable state.
Get-ADUser -Identity <samAccountName> -Properties LastLogon -Server <DCName> 

Open in new window


Then the conversion.
Copy the value from Last logon and inserted inside the quotes:
[datetime]::FromFileTime("128752344000000000")

Open in new window

Both command are to be used in PowerShell

Let us know if helped
0
 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 166 total points
ID: 40362620
$UserName = Read-Host "User SamAccountName that you are looking for"
import-module ActiveDirectory
$DCs = Get-AdDomainController -filter *
foreach ($DC in $DCs)
{

Get-ADUser -Identity $UserName -Properties LastLogon -Server $DC.Name | select Name,@{N="LastLogon";E={[datetime]::FromFileTime($_.lastlogon)}};@{N="LogonDC";E={$DC.Name}}

}

Open in new window


you can use expression directly convert the code to a readable format and doesn't need to run a 2nd command to convert it.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Group policy not applying 5 29
Script for Password Expire Notifications 7 65
Powershell 16 32
Raid 6 or Raid 10? 19 54
Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now