-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-ComputerLastActive.ps1
66 lines (47 loc) · 2.25 KB
/
Get-ComputerLastActive.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
<#PSScriptInfo
.VERSION 1.0
.GUID c45667a6-6995-4ecc-9cf5-4fa93b195d38
.AUTHOR Anthony Yates
.COMPANYNAME Airdesk Services
.COPYRIGHT 2024 Anthony Yates
.TAGS Active Directory
.LICENSEURI https://github.com/Air-Git/ad-remediation/blob/main/LICENSE
.PROJECTURI https://github.com/Air-Git/ad-remediation/tree/main
.ICONURI
.EXTERNALMODULEDEPENDENCIES Active Directory
.REQUIREDSCRIPTS None
.EXTERNALSCRIPTDEPENDENCIES None
.RELEASENOTES
.PRIVATEDATA
#>
<#
.DESCRIPTION
A script to report on all computer accounts in AD and discover which ones are inactive.
The script produces an Excel report, which you can filter to show inactive user accounts.
The name of the output report is hard-coded in the script. Edit it before running.
#>
$date = Get-Date
$fileDate = Get-Date -Format ddMMyy
$computers = Get-ADComputer -Filter * -Properties CanonicalName, Created, Description, DisplayName, LastLogonDate, LastLogonTimestamp, Modified, OperatingSystem, PwdLastSet | Sort-Object CanonicalName
# Do each one
$computers |
ForEach-Object {
$pwdLastSet = ([datetime]::FromFileTime($_.PwdLastSet))
$lastLogonTimestamp = ([datetime]::FromFileTime($_.LastLogonTimestamp))
$OU = try { $_.CanonicalName | Split-Path -Parent } catch [System.Management.Automation.ParameterBindingValidationException] {}
[pscustomobject]@{
'sAMAccountName' = $_.sAMAccountName
'GUID' = $_.ObjectGUID
'Created' = Get-Date -Date $_.Created -Format dd/MM/yyyy
'Last Modified' = Get-Date -Date $_.Modified -Format dd/MM/yyyy
'Display Name' = $_.DisplayName
'Enabled' = $_.Enabled
'Pwd Last Set' = Get-Date $pwdLastSet -Format dd/MM/yyy
'Pwd Gap' = (New-TimeSpan -Start $pwdLastSet -End $date).Days
'Last Logon Timestamp' = Get-Date $lastLogonTimestamp -Format dd/MM/yyyy
'Last Logon Gap' = (New-TimeSpan -Start $lastLogonTimestamp -End $date).Days
'Description' = $_.Description
'Operating System' = $_.OperatingSystem
'OU' = $OU
}
} | Export-Csv "C:\Temp\ComputerLastActive_$fileDate.csv" -NoTypeInformation