-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_dc_healthcheck_07.ps1
116 lines (106 loc) · 3.98 KB
/
check_dc_healthcheck_07.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
function Get-DcDiag {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DomainController
)
$dcdiag_res = New-Object Object
$dcdiag_res | Add-Member -Type NoteProperty -Name "DC" -Value $DomainController
$result = dcdiag /s:$DomainController
$result_all = $result | select-string -pattern '\. (.*) \b(passed|failed)\b test (.*)'
$result_all | foreach {
$dcdiag_res | Add-Member -Type NoteProperty -Name $($_.Matches.Groups[3].Value) -Value $($_.Matches.Groups[2].Value) -force
}
$dcdiag_res
}
function Get-Repadmin {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DomainController
)
$repadmin = @()
$rep = (Invoke-Command $DomainController -ScriptBlock { repadmin /showrepl /repsto /csv | ConvertFrom-Csv })
$rep | ForEach-Object {
# Define current loop to variable
$r = $_
# Adding properties to object
$REPObject = New-Object PSCustomObject
$REPObject | Add-Member -Type NoteProperty -Name "Destination DCA" -Value $r.'destination dsa'
$REPObject | Add-Member -Type NoteProperty -Name "Source DSA" -Value $r.'source dsa'
$REPObject | Add-Member -Type NoteProperty -Name "Source DSA Site" -Value $r."Source DSA Site"
$REPObject | Add-Member -Type NoteProperty -Name "Last Success Time" -Value $r.'last success time'
$REPObject | Add-Member -Type NoteProperty -Name "Last Failure Status" -Value $r.'Last Failure Status'
$REPObject | Add-Member -Type NoteProperty -Name "Last Failure Time" -Value $r.'last failure time'
$REPObject | Add-Member -Type NoteProperty -Name "Number of failures" -Value $r.'number of failures'
# Adding object to array
$repadmin += $REPObject
}
$repadmin #| ft | Out-String
}
function Check-DcExist {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DomainController
)
# Checking if DomainController exist
$DCErrors = @()
Try {
$DC = Get-ADDomainController -Identity $DomainController -ErrorAction Stop
}
Catch {
$ErrorMessage = $_.Exception.Message
}
if ($ErrorMessage) {
Write-Host "Error: " -NoNewline -ForegroundColor Yellow
Write-Host $ErrorMessage
$obj = [PSCustomObject]@{
DCname = $DomainController
ErrorMessage = $ErrorMessage
}
$DCErrors += $obj
}
else {
# Testing connection
If (Test-Connection -ComputerName $DomainController -BufferSize 16 -Count 1 -ea 0 -Quiet) {
$status = $true
}
Else {
$status = $false
}
}
$DCErrors,$status
}
function Invoke-DcHealthcheck {
$AllDCDiags = @()
$repadmin = @()
$AllDcErrors = @()
$DCs = ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).sites
foreach ($DC in $DCs.servers) {
Write-Host "Working on... $($DC.name)" -ForegroundColor Yellow
$DCErrors,$status = Check-DcExist -DomainController $DC.Name
if ($DCErrors) {
$AllDcErrors += $DCErrors
}
if ($status) {
$dcdiag_res = Get-DcDiag -DomainController $DC.Name
$dcdiag_res
$AllDCDiags += $dcdiag_res
$repadmin_res = Get-Repadmin -DomainController $DC.Name
$repadmin_res | ft -AutoSize
$repadmin += $repadmin_res
}
}
$LogFileName = "_output-$(Get-Date -UFormat "%Y-%m-%d_%H-%m-%S").csv"
$AllDCDiags | Export-Csv -Path $PSScriptRoot\dcdiag_$LogFileName -NoTypeInformation
$AllDCDiags | Out-GridView
$repadmin | Export-Csv -Path $PSScriptRoot\repadmin_$LogFileName -NoTypeInformation
$repadmin | Out-GridView
if ($AllDcErrors) {
$AllDcErrors | Export-Csv -Path $PSScriptRoot\AllDcErrors_$LogFileName -NoTypeInformation
$AllDcErrors | ft -AutoSize
$AllDcErrors | Out-GridView
}
}
Invoke-DcHealthcheck