-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ComputersNotLoggedOnXDays.ps1
More file actions
34 lines (28 loc) · 1.24 KB
/
Get-ComputersNotLoggedOnXDays.ps1
File metadata and controls
34 lines (28 loc) · 1.24 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
param(
[int]$Days = 60
)
try {
Import-Module ActiveDirectory -ErrorAction Stop
$CutoffDate = (Get-Date).AddDays(-$Days)
# Fetch all computers not logged on in X days (no limit)
$Computers = Get-ADComputer -Filter { LastLogonDate -lt $CutoffDate -and Enabled -eq $true } -Properties Name, DNSHostName, OperatingSystem, OperatingSystemVersion, Enabled, LastLogonDate, WhenCreated |
Sort-Object LastLogonDate
$Results = foreach ($Computer in $Computers) {
$DaysSinceLogon = if ($Computer.LastLogonDate) { ((Get-Date) - $Computer.LastLogonDate).Days } else { "N/A" }
[PSCustomObject]@{
Name = $Computer.Name
DNSHostName = $Computer.DNSHostName
OperatingSystem = $Computer.OperatingSystem
Enabled = $Computer.Enabled
LastLogon = $Computer.LastLogonDate
DaysSinceLogon = $DaysSinceLogon
Created = $Computer.WhenCreated
DistinguishedName = $Computer.DistinguishedName
}
}
@($Results) | ConvertTo-Json -Depth 3
}
catch {
@{ Error = $_.Exception.Message } | ConvertTo-Json
exit 1
}