header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Checking domain controllers for last logon
Last Post 25 Feb 2011 03:21 AM by martpower. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
gmagerrUser is Offline
New Member
New Member
Posts:82
Avatar

--
07 Aug 2010 05:58 PM
    Hi guys,

    I've pieced together this script to get the latest login for a user. It searches all the domain controllers and supposedly gets the latest logon time. All I'm getting back are dates. How would I get the username, the latest login time and the domain controller they last logged into?

    Thanks

    $DCS = Get-QADComputer -ComputerRole DomainController | Where-Object {($_.Name -NotMatch "SRGDC1")}
    $USERS = Get-QADUser -SizeLimit 0 #-identity $username -SizeLimit 0

    foreach ($USER in $USERS) {
    $lastlogon = $null
    foreach ($DC in $DCS) {
    $DCLOGON = (Get-QADUser -Service $DC.Name -SamAccountName $USER.NAME).LastLogon
    if ($DCLOGON -ne $null) {
    if ($lastlogon -lt $DCLOGON) {
    $lastlogon = $DCLOGON
    }
    }
    }
    }
    $lastlogon
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    08 Aug 2010 03:16 AM
    $SamAccountName = 'gmagerr'

    Get-QADComputer -ComputerRole DomainController | Where-Object {$_.Name -NotMatch "SRGDC1"} | Foreach-Object{
    $dc = $_.Name
    Get-QADUser -Service $dc  -SamAccountName $SamAccountName
    } | Sort-Object LastLogon -Descending | Select-Object Name,Lastlogon,@{n='DC';e={$dc}} -First 1



    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    gmagerrUser is Offline
    New Member
    New Member
    Posts:82
    Avatar

    --
    08 Aug 2010 06:11 AM
    Thanks shay,

    I'd like to have this script get the lastlogon for everyone in the domain. Can i modify this the same way I had in the original script using Get-QADUser? or would it be done differently?
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    08 Aug 2010 07:01 AM
    Try this:

    $DCs = Get-QADComputer -ComputerRole DomainController

    Get-QADUser -SizeLimit 0 | Foreach-Object{

    $SamAccountName = $user.SamAccountName

    $DCs | Foreach-Object{
    $dc = $_.Name
    Get-QADUser -Service $dc -SamAccountName $SamAccountName
    } | Sort-Object LastLogon -Descending | Select-Object Name,Lastlogon,@{n='DC';e={$dc}} -First 1
    }




    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    martpowerUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    25 Feb 2011 03:21 AM
    Hey Shay ,

    I tried your query It runs but it just goes back to the shell prompt, no results displayed. Any idea?
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer