gmagerr
 New Member Posts:82

 |
| 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 Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
| 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 |
|
|
gmagerr
 New Member Posts:82

 |
| 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 Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
| 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 |
|
|
martpower
 New Member Posts:1

 |
| 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? |
|
|
|
|