I have been tasked to get user logon info (Last logon date & time, DC user Athuenticated to, & anything else is gravy). I need it to focus on using one DC (Not all DC's in the forest) and focus within as specific OU (Not all OU's). Below is what I have made thus far but it is not working as I need it to. Help please....
##################
#--------Config
##################
$domain = "FQDN"
##################
#--------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