Solved

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

Posted on 2014-10-01
4
356 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

867 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

15 Experts available now in Live!

Get 1:1 Help Now