-
Notifications
You must be signed in to change notification settings - Fork 0
/
addslastlogon.ps1
67 lines (51 loc) · 1.83 KB
/
addslastlogon.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#HTML based report
#Must have RSAT installed or run on a domain controller
#Update the DOMAINNAME with the appropriate domain name
#Update the appropriate path as needed
$Date= Get-date
Import-Module activedirectory
$HTMLHead=@"
<title>Last Logon Report</title>
<Head>
<STYLE>
BODY{background-color :#FFFFF}
TABLE{Border-width:thin;border-style: solid;border-color:Black;border-collapse: collapse;}
TH{border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color: ThreeDShadow}
TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color: Transparent}
</STYLE>
</HEAD>
"@
# Gets time stamps for all User in the domain that have NOT logged in since after specified date
$domain = "DOMAINNAME"
$DaysInactive = 180
$time = (Get-Date).Adddays(-($DaysInactive))
# Get all AD User with lastLogonTimestamp less than our time and set to enable
$lldata = Get-ADUser -Filter {LastLogonTimeStamp -lt $time -and enabled -eq $true} -Properties LastLogonTimeStamp | select-object Name,@{Name="Date of Last Logon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('yyyy-MM-dd_hh:mm:ss')}} | ConvertTo-HTML
$LLCount = $lldata.Count
#HTML Body Content
$HTMLBody = @"
<CENTER>
<Font size=4><B>Last Logon Report</B></font></BR>
<I>Script last run:$date</I><BR />
Objective: Show user accounts that have not logged on in the last 180 days or greater. Service Accounts should be an exception.</BR>
<B>Last Logon in 180 Days Report</B><BR />
<TABLE>
<TR>
<TD>$lldata</TD>
</TR>
</TABLE>
</BR>
<B>Last Logon 180 days Count</B><BR />
<TABLE>
<TR>
<TD>Last Logon</TD>
</TR>
<TR>
<TD>$LLCount</TD>
</TR>
</TABLE>
</CENTER></BR>
</CENTER></BR>
"@
#Export to HTML
$StatusUpdate | ConvertTo-HTML -head $HTMLHead -body $HTMLBody | out-file "C:\temp\llreport.html" -Append