-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastLogon-FromAllDCs
More file actions
39 lines (32 loc) · 1.04 KB
/
LastLogon-FromAllDCs
File metadata and controls
39 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Import-Module ActiveDirectory
function Get-ADUsersLastLogon()
{
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$users = @("spa", “csc”)
$time = 0
$exportFilePath = "c:\users\adminslpv\lastLogon.csv"
$columns = "name,username,datetime"
Out-File -filepath $exportFilePath -force -InputObject $columns
foreach($user in $users)
{
# $user = Get-ADUser $user
Write-Host $user
foreach($dc in $dcs)
{
$hostname = $dc.HostName
#Write-Host $hostname
$currentUser = Get-ADUser $user -Properties LastLogonTimestamp -Server $hostname # | Get-ADObject -Server $hostname -Properties LastLogon
# Write-Host $currentUser.LastLogonDate
if($currentUser.LastLogonTimestamp -gt $time)
{
$time = $currentUser.LastLogonTimestamp
}
}
$dt = [DateTime]::FromFileTime($time)
$row = $currentUser.Name+","+$currentUser.SamAccountName+","+$dt
#Write-Host $row
Out-File -filepath $exportFilePath -append -noclobber -InputObject $row
$time = 0
}
}
Get-ADUsersLastLogon