Public/Get-InactiveAccounts.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Function Get-InactiveAccounts
{
    <#
        .SYNOPSIS
            Finds users or computers that are inactive.
 
        .DESCRIPTION
            Finds users or computers that are inactive.
            This will find users or computers who have not
            been active within the last n days, based
            on the parameters used at runtime.
 
        .PARAMETER ReportType
            Select User, Computer or Both for the type of
            inactive accounts to search for.
 
        .PARAMETER DaysInactive
            Select the number of days since last logon
            for an account to be considered inactive.
 
        .PARAMETER Export
            If this switch is enabled, output will be directed to a CSV file.
         
        .EXAMPLE
            Get-InactiveAccounts -ReportType Both -DaysInactive 30 -Export
         
        .EXAMPLE
            Get-InactiveAccounts -ReportType User -DaysInactive 75
    #>


    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$True,Position=0)]
        [validateset("User", "Computer", "Both")]
        [String]
        $ReportType,
        [Parameter(Mandatory=$True,Position=1)]
        [validaterange(1, [Int]::MaxValue)]
        [Int]
        $DaysInactive,
        [Parameter(Mandatory=$False,Position=2,HelpMessage="If used this will export to file instead of console.")]
        [Switch]
        $Export
    )

    #Get date timeframe for the report
    $Time = (Get-Date).Adddays(-($DaysInactive))

    #Generate the appropriate reports.
    If($ReportType -eq "User" -or $ReportType -eq "Both")
    {
        Try 
        {
            Write-Verbose "Finding users that match the parameters"
            $ADusr = Get-ADUser -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | Select-Object Name,DistinguishedName,@{Name="Last Logon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('MM-dd-yyyy')}},Enabled
            If($Export)
            {
                $ADusr | Export-CSV InactiveUsers.csv -notypeinformation
                Write-Output "AgingUsers.csv exported to current directory"
            }
            Else 
            {
                $ADusr | Write-Output | Format-Table
            }
        }
        Catch 
        {
            Throw "Something went wrong"
        }
        
    }
    If($ReportType -eq "Computer" -or $ReportType -eq "Both")
    {
        Try 
        {
            Write-Verbose "Finding users that match the parameters"
            $ADcmp = Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -Properties LastLogonTimeStamp | Select-Object Name,DistinguishedName,@{Name="Last Logon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString('MM-dd-yyyy')}},Enabled
            If($Export)
            {
                $ADcmp | Export-CSV InactiveComputers.csv -notypeinformation
                Write-Output "AgingComputers.csv exported to current directory"
            }
            Else 
            {
                $ADcmp | Write-Output | Format-Table
            }
        }
        Catch 
        {
            Throw "Something went wrong"
        }
    }
}