-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupMemberLastLogon - Copy.ps1
53 lines (44 loc) · 1.64 KB
/
GroupMemberLastLogon - Copy.ps1
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$Members = Get-ADGroupMember IS
$results
#$Member.SamAccountName
foreach($Member in $Members){
$lastlogonDate = Get-ADUser $Member -Properties * | select LastLogonDate
$samaccountname = $Member.SamAccountName
Write-Host "$($samaccountname) last logon date was $($lastlogonDate)"
$results = "$($samaccountname) last logon date was $($lastlogonDate)"
}
$results | Export-Csv -Path ".\Documents\GitHub\Reports\IS_LastLogonDate_2020-07-02.csv"
# Import-Csv .\Documents\GitHub\Reports\IS_2020-07-02.csv | ForEach-Object {
#Get-ADUser -Identity $($_.EmployeeID) -Properties “LastLogonDate”
# Write-Host "$($_.Name), whose Employee ID is $($_.SamAccountName),last logon date was $($_.LastLogonDate)."
#}
<#
Import-Module ActiveDirectory
function Get-ADUsersLastLogon()
{
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$users = Import-Csv .\Documents\GitHub\Reports\IS_2020-07-02.csv
$time = 0
$importFilePath = ".\Documents\GitHub\Reports\IS_2020-07-02.csv"
$exportFilePath = ".\Documents\GitHub\Reports\lastlogon.csv"
$columns = "name,username,datetime"
Out-File -filepath $exportFilePath -force -InputObject $columns
foreach($user in $users)
{
foreach($dc in $dcs)
{
$hostname = $dc.HostName
$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon
if($currentUser.LastLogon -gt $time)
{
$time = $currentUser.LastLogon
}
}
$dt = [DateTime]::FromFileTime($time)
$row = $user.Name+","+$user.SamAccountName+","+$dt
Out-File -filepath $exportFilePath -append -noclobber -InputObject $row
$time = 0
}
}
Get-ADUsersLastLogon
#>