Public/Get-AduserLockoutInfo.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283


Function Get-AduserLockoutInfo {
# .ExternalHelp .\Get-AduserLockoutInfo-help.xml

    [cmdletbinding(SupportsShouldProcess=$true)]
    param(
        [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true,HelpMessage="search for lockout events in seconds")]
        [int]$Timespan
    )
    
    

    BEGIN{
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Starting: $($MyInvocation.MyCommand)"
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] PSVersion = $($PSVersionTable.PSVersion)"
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] OS = $((Get-CimInstance win32_OperatingSystem).Caption)"
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] User = $($env:userdomain)\$($env:USERNAME)"
        $id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $IsAdmin = [System.Security.Principal.WindowsPrincipal]::new($id).IsInRole('administrators')
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Is Admin = $IsAdmin"
        Import-module ActiveDirectory
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Enumerating PDC Emulator"
        
        $DomainController = Get-ADDomain  | Select-Object PDCEmulator
        $DC = $DomainController.PDCEmulator
        

        try{
            $Session = New-CimSession -computerName $DC -ErrorAction Stop
            $OS = Get-CimInstance -CimSession  $Session -ClassName Win32_operatingSystem | Select-Object Caption
        }#TRY
        Catch{
            $ErrorMessage = $_.Exception.Message
            $ErrorMessage
            exit
        }#CATCH

        Switch -Wildcard($OS.Caption){

            "Microsoft Windows Server 2008 R2*"
            {
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] $($DC) is running $($OS.Caption)"
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] cannot run windows firewall check"
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] TestingPSRemotingStatus on $($DC)"
                $WSMANStatus = TestPSRemotingStatus -ComputerName $DC
                $FirewallStatus = $True
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Cannot Test GPO Settings for $($OS.Caption) on $($DC)"
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Ensure Audit Account Lockout and Audit User Account Management setting are configured"
                $ADAudtingEnabled = $True

            }
            "Microsoft Windows Server 2012 R2*"
            {
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] $($DC) is running $($OS.Caption)"
                $WSMANStatus = TestPSRemotingStatus -ComputerName $DC
                $FirewallStatus = TestFirewallRuleStatus -ComputerName $DC
                $ADAudtingEnabled = Get-ADAccountLockoutAuditPolicy -ComputerName $DC

            }

            "Microsoft Windows Server 2016*"
            {
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] $($DC) is running $($OS.Caption)"
                $WSMANStatus = TestPSRemotingStatus -ComputerName $DC
                $FirewallStatus = TestFirewallRuleStatus -ComputerName $DC
                $ADAudtingEnabled = Get-ADAccountLockoutAuditPolicy -ComputerName $DC

            }


            Default
            {
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] $($DC) is running $($OS.Caption) no condtion set"
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] TestingPSRemotingStatus on $($DC)"
                $WSMANStatus = $True
                $FirewallStatus = $True
                $ADAudtingEnabled = $True

            }
        }#SWITCH
        
    }#BEGIN


    PROCESS{
       if (($FirewallStatus -eq $true) -and ($WSMANStatus -eq $True) -and ($ADAudtingEnabled -eq $true)){ 
        TRY{
            if($pscmdlet.ShouldProcess($DC)){
                    
                    Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Invoking: Examining event logs on $($DC)"
                    $Events = Get-WinEvent -ComputerName $DC  -FilterHashtable @{LOGNAME='Security';ID="4740"} -ErrorAction Stop
                        foreach ($event in $Events){
                            if($event.TimeCreated -ge (Get-Date).AddSeconds(-$Timespan) ){
                                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] user $($event.Properties[0].value) is locked "
                                $Properties = [PSCustomObject][Ordered]@{

                                                    "Username" = $event.Properties[0].value 
                                                    "ReferenceComputer" = $Event.Properties[1].value
                                                    "LockOutTime" = $event.TimeCreated 
                                                    "Domain" = $Event.Properties[5].value
                                                    "DC" = $Event.Properties[4].value -replace "\$" 
                                                    
                                                }
                                $Properties
                            }#IF
                        }#Foreach
                }#IF
        }#TRY
        Catch [System.Exception] {
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] No Account lockout events found on $($DC)."
       }#CATCH
    
    }#IF

    }


    END{
        Remove-CimSession $Session -ErrorAction SilentlyContinue
        
    }





}#Function Get-AduserLockoutInfo (MAIN)

Function TestPSRemotingStatus {
    [cmdletbinding()]
    param(
    [String]$ComputerName
    )

    try{
        $WSMAN = New-PSSession -ComputerName $ComputerName -errorAction Stop
        
        if($WSMAN){
            Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Powershell Remoting enabled on $($ComputerName)"
            Remove-PSSession $WSMAN.ID
            Return $true
        }
       
    }
    
    Catch
    {
        $ErrorMessage = $_.Exception.Message
        $ErrorMessage
        Write-error -message "Powershell remoting test error, enable powershell remoting to continue"
        exit
    }

}# Function TestPSRemotingStatus

Function TestFirewallRuleStatus {
    [cmdletbinding()]
    param(
    [String]$ComputerName
    )
    do{
        try{   
            Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Testing if RemoteEventLogSvc-In-TCP firewall rule is enabled"
            $FirewallStatus = Invoke-Command -ComputerName $ComputerName -ScriptBlock {(Get-NetFirewallRule -Name 'RemoteEventLogSvc-In-TCP').Enabled} -AsJob | Wait-Job | Receive-Job -ErrorAction Stop
                if($($FirewallStatus.value) -eq $false){
                 
                    Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] Attempting to enable RemoteEventLogSvc-In-TCP on $($ComputerName)"
                    $RemoteJob = Invoke-Command -ComputerName $ComputerName -ScriptBlock {(Enable-NetFirewallRule -Name 'RemoteEventLogSvc-In-TCP')} -AsJob | Wait-Job | Receive-Job -ErrorAction Stop
                    
                }#IF
                elseif($($FirewallStatus.value) -eq $true){
                    Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN ] RemoteEventLogSvc-In-TCP firewall Rule Enabled on $($ComputerName). Test Passed"
                    Return $true
                    $Count = 2
                }#ELSEIF
            
            }#TRY
            Catch{
                $ErrorMessage = $_.Exception.Message
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) BEGIN:ERROR ] Error $($FirewallStatus)" 
                write-error -Message $FirewallStatus
                Write-Error -Message $ErrorMessage
                write-error -message $RemoteJob
                exit
            }#CATCH
        }while($Count -lt 2)
        
    
}#Function TestFirewallRuleStatus


Function Get-ADAccountLockoutAuditPolicy {
    [cmdletbinding(SupportsShouldProcess)]
    param(
        [parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true,HelpMessage="DomainContoller (FQDN)")]
        [String]$ComputerName
    )

   
    try{
        if($PSCmdlet.ShouldProcess($ComputerName)){
            Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Getting domain information"
            $Domain = $ComputerName.Substring($ComputerName.IndexOf(".") + 1)
            [xml]$DDCP = Get-GPOReport -name "Default Domain Controllers Policy" -Domain $Domain -Server $ComputerName -ReportType xml -ErrorAction Stop
        
            Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Checking Default Domain Controllers Policy for Audit Settings"
            $AAL = $DDCP.ChildNodes.Computer.Extensiondata.extension.AuditSetting | Where-Object {$_.SubcategoryName -eq "Audit Account Lockout"} | Select-Object SubcategoryName, SettingValue -ErrorAction stop
            $AUAM = $DDCP.ChildNodes.Computer.Extensiondata.extension.AuditSetting | Where-Object {$_.SubcategoryName -eq "Audit User Account Management"} | Select-Object SubcategoryName, SettingValue -ErrorAction stop
        
        
            if($AAL.SettingValue -eq 3 -and $AUAM.SettingValue -eq 3){
                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Audit Settings enabled. Test Passed"
                Return $true
            }#IF
            else{

                Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Audit Settings not configured. enable Audit Account Lockout and Audit User Account Management."
                Write-host  "Audit settings required for ADAccountLockoutInfo not configured $($ComputerName) for $($Domain). Script terminated" -ForegroundColor Yellow
                Return $false
            }#Else
        }#IF
    }#TRY
    Catch{
        $ErrorMessage = $_.Exception.Message
        $ErrorMessage

    }#CATCH
   


}#Function Get-ADAccountLockoutAuditPolicy



# SIG # Begin signature block
# MIIIWAYJKoZIhvcNAQcCoIIISTCCCEUCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQ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