-
Notifications
You must be signed in to change notification settings - Fork 27
/
vbs-ad-health-report.vbs
355 lines (329 loc) · 15.6 KB
/
vbs-ad-health-report.vbs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
'###################################################
' Author: AikonCWD
' Source: https://github.com/aikoncwd/vbs-ad-health-report
' Ver: 3.5.2
'###################################################
' IMPORTANT VARIABLES TO EDIT
'###################################################
'This script will find automatically all your DC servers stored in the default OU 'Domain Controllers'
'If you have a custom OU name for your DC server, set it on organizationUnitDC variable.
'
'If you have child domains or some complex Domain structure, just set usingOU = False and write
'manually DNS names of every DC server you want to monitor/report (oDC Array variable)
usingOU = True
oDC = Array("SRV-DC1","SRV-DC2","SRV-AUX") 'Ignored if usingOU is True
serviceState = "Running" 'Translate this into your System Language ONLY if the script is not working
organizationUnitDC = "Domain Controllers" 'Default OU where DC's are stored, edit if you use a custom OU
hardwareReport = True 'Additional table with hardware related information for each server
minHDDfree = 30 'Minimun % HDD FreeSpace to mark as red/error
minRAMfree = 20 'Minimun % RAM FreeSpace to mark as red/error
includeRepadmin = True 'Include Replication Summary Report below the table. Nice to have, default: True
emailReport = True 'Send the report using e-mail. Nice to have, default: True
errorOnlyReport = False 'Send e-mail ONLY if an error occours
attachCSV = True 'Attach a CSV version of the replication report/summary
saveReport = True 'Save a file with the report summary
pathReportOutput = "AD-health-summary.html" 'Path and name for the output file
'E-mail settings, edit by your own
emailSubject = "Active Directory Health Summary"
emailFrom = "[email protected]"
emailTo = "[email protected]"
emailCc = ""
emailPort = 25
emailAuth = 1
emailSenderUser = "[email protected]"
emailSenderPassword = "correcthorsebatterystaple"
emailServer = "mail.mydomain.com"
emailSSL = False
'###################################################
' END OF IMPOTANT VARIABLES TO EDIT
'###################################################
' BEGIN OF CODE - DON'T EDIT
'###################################################
GB = 1024 * 1024 * 1024
TimeStart = Timer()
isError = False
If usingOU Then
Set oRD = GetObject("LDAP://RootDSE")
Set getoDC = GetObject("LDAP://ou=" & organizationUnitDC & ", " & oRD.Get("defaultNamingContext"))
computers=Enumerate(getoDC)
oDC = computers
End If
sHTML = ""
sHTML = sHTML & "<html>" & vbCrLf
sHTML = sHTML & "<head>" & vbCrLf
sHTML = sHTML & "<title>" & emailSubject & "</title>" & vbCrLf
sHTML = sHTML & "<style type='text/css'>" & vbCrLf
sHTML = sHTML & "<!--" & vbCrLf
sHTML = sHTML & "td {" & vbCrLf
sHTML = sHTML & "font-family: Tahoma;" & vbCrLf
sHTML = sHTML & "font-size: 11px;" & vbCrLf
sHTML = sHTML & "border-top: 1px solid #999999;" & vbCrLf
sHTML = sHTML & "border-right: 1px solid #999999;" & vbCrLf
sHTML = sHTML & "border-bottom: 1px solid #999999;" & vbCrLf
sHTML = sHTML & "border-left: 1px solid #999999;" & vbCrLf
sHTML = sHTML & "padding-top: 0px;" & vbCrLf
sHTML = sHTML & "padding-right: 0px;" & vbCrLf
sHTML = sHTML & "padding-bottom: 0px;" & vbCrLf
sHTML = sHTML & "padding-left: 0px;" & vbCrLf
sHTML = sHTML & "}" & vbCrLf
sHTML = sHTML & "body {" & vbCrLf
sHTML = sHTML & "margin-left: 3px;" & vbCrLf
sHTML = sHTML & "margin-top: 3px;" & vbCrLf
sHTML = sHTML & "margin-right: 3px;" & vbCrLf
sHTML = sHTML & "margin-bottom: 3px;" & vbCrLf
sHTML = sHTML & "table {" & vbCrLf
sHTML = sHTML & "border: thin solid #000000;" & vbCrLf
sHTML = sHTML & "}" & vbCrLf
sHTML = sHTML & "-->" & vbCrLf
sHTML = sHTML & "</style>" & vbCrLf
sHTML = sHTML & "</head>" & vbCrLf
sHTML = sHTML & "<body>" & vbCrLf
If hardwareReport Then
sHTML = sHTML & "<table width='100%'>" & vbCrLf
sHTML = sHTML & "<tr bgcolor='Lavender'>" & vbCrLf
sHTML = sHTML & "<td colspan='7' height='25' align='center'>" & vbCrLf
sHTML = sHTML & "<font face='tahoma' color='DarkBlue' size='4'><strong>Hardware Status Report</strong></font>" & vbCrLf
sHTML = sHTML & "</td></tr></table>" & vbCrLf
sHTML = sHTML & "<table width='100%'>" & vbCrLf
sHTML = sHTML & "<tr bgcolor='FireBrick'>" & vbCrLf
sHTML = sHTML & "<td align='center' rowspan=2><b><font color='white'>Server</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center' colspan=3><b><font color='white'>Disk Status</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center' colspan=3><b><font color='white'>RAM Status</font></b></td>" & vbCrLf
sHTML = sHTML & "</tr>" & vbCrLf
sHTML = sHTML & "<tr bgcolor='Indigo'>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Total Size</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Free Space</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>% Free</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Total RAM</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Free RAM</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>% Free</font></b></td>" & vbCrLf
sHTML = sHTML & "</tr>" & vbCrLf
For Each oComputer In oDC
remoteComputer = oComputer
sHTML = sHTML & "<tr>" & vbCrLf
sHTML = sHTML & "<td bgcolor='DarkGray' align=center><b>" & remoteComputer & "</b></td>" & vbCrLf
If checkPing(oComputer) Then
Call checkDiskUsage(remoteComputer)
Call checkRAMUsage(remoteComputer)
sHTML = sHTML & "</tr>" & vbCrLf
Else
isError = True
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>NO CONTACT</b></td>" & vbCrLf
sHTML = sHTML & "</tr>" & vbCrLf
End If
Next
sHTML = sHTML & "</table><br>"
End If
sHTML = sHTML & "<table width='100%'>" & vbCrLf
sHTML = sHTML & "<tr bgcolor='Lavender'>" & vbCrLf
sHTML = sHTML & "<td colspan='7' height='25' align='center'>" & vbCrLf
sHTML = sHTML & "<font face='tahoma' color='DarkBlue' size='4'><strong>" & emailSubject & "</strong></font>" & vbCrLf
sHTML = sHTML & "</td></tr></table>" & vbCrLf
sHTML = sHTML & "<table width='100%'>" & vbCrLf
sHTML = sHTML & "<tr bgcolor='FireBrick'>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Server</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Ping Status</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Service DNS</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Service NTDS</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Service Netlogon</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Connectivity</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Advertising</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>NetLogon</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Services</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Replication</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>FSMO</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>SysVol</font></b></td>" & vbCrLf
sHTML = sHTML & "<td align='center'><b><font color='white'>Topology</font></b></td>" & vbCrLf
sHTML = sHTML & "</tr>" & vbCrLf
For Each oComputer In oDC
remoteComputer = oComputer
sHTML = sHTML & "<tr>" & vbCrLf
sHTML = sHTML & "<td bgcolor='DarkGray' align=center><b>" & remoteComputer & "</b></td>" & vbCrLf
If checkPing(remoteComputer) Then
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>Success</b></td>" & vbCrLf
Call checkService(remoteComputer, "DNS")
Call checkService(remoteComputer, "NTDS")
Call checkService(remoteComputer, "Netlogon")
Call checkDcDiag(remoteComputer, "Connectivity")
Call checkDcDiag(remoteComputer, "Advertising")
Call checkDcDiag(remoteComputer, "NetLogons")
Call checkDcDiag(remoteComputer, "Services")
Call checkDcDiag(remoteComputer, "Replications")
Call checkDcDiag(remoteComputer, "FsmoCheck")
Call checkDcDiag(remoteComputer, "SysVolCheck")
Call checkDcDiag(remoteComputer, "Topology")
Else
isError = True
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
End If
sHTML = sHTML & "</tr>" & vbCrLf
Next
sHTML = sHTML & "</table>"
If includeRepadmin Then sHTML = sHTML & checkRepadmin()
sHTML = sHTML & "<font face='tahoma' size='2'>Execution Time: <b>" & Round(Timer() - TimeStart, 2) & "s</b></font><br />"
sHTML = sHTML & "<font face='tahoma' size='2'>Domain Controller: <b>" & CreateObject("WScript.Network").ComputerName & "s</b></font>"
sHTML = sHTML & "</body>"
sHTML = sHTML & "</html>"
If attachCSV Then
Set oEXE = CreateObject("WScript.Shell").Exec("repadmin.exe /showrepl * /csv")
Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile("ad-health-summary.csv")
F.Write oEXE.StdOut.ReadAll
F.Close
End If
If saveReport Then
Set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(pathReportOutput)
F.Write sHTML
F.Close
End If
If emailReport Then Call sendMail(sHTML)
'###################################################
' END OF CODE - DON'T EDIT
'###################################################
' INTERNAL FUNCTIONS - DON'T EDIT
'###################################################
Function checkDiskUsage(RemoteComputer)
Set oWMI = GetObject("winmgmts:\\" & RemoteComputer & "\root\CIMV2")
Set colHDD = oWMI.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE Caption = 'C:'", "WQL", &H10 + &H20)
For Each objHDD in colHDD
HDDsize = Round(objHDD.Size / 1073741824, 2)
HDDfree = Round(objHDD.FreeSpace / 1073741824, 2)
HDDperc = Round((HDDfree * 100) / HDDsize, 2)
Next
If HDDperc < minHDDfree Then
isError = True
celColor = "Red"
Else
celColor = "LightGreen"
End If
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>" & HDDsize & " GB</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>" & HDDfree & " GB</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='" & celColor & "' align=center><b>" & HDDperc & " %</b></td>" & vbCrLf
End Function
Function checkRAMUsage(RemoteComputer)
Set oWMI = GetObject("winmgmts:\\" & RemoteComputer & "\root\CIMV2")
Set colRAM = oWMI.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory", "WQL", &H10 + &H20)
For Each objRAM in colRAM
RAMfree = Round(objRAM.AvailableBytes / 1048576, 2)
Next
Set colRAM = oWMI.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", &H10 + &H20)
For Each objRAM in colRAM
RAMsize = Round(objRAM.TotalPhysicalMemory / 1048576, 2)
Next
RAMperc = Round((RAMfree * 100) / RAMsize, 2)
If RAMperc < minRAMfree Then
isError = True
celColor = "Red"
Else
celColor = "LightGreen"
End If
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>" & RAMsize & " MB</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>" & RAMfree & " MB</b></td>" & vbCrLf
sHTML = sHTML & "<td bgcolor='" & celColor & "' align=center><b>" & RAMperc & " %</b></td>" & vbCrLf
End Function
Function checkPing(RemoteComputer)
Set oWMI = GetObject("winmgmts:\\.\root\CIMV2")
If oWMI.Get("Win32_PingStatus.Address='" & RemoteComputer & "'").StatusCode = 0 Then
checkPing = True
Else
checkPing = False
End if
End Function
Function checkService(RemoteComputer, ServiceName)
Set oWMI = GetObject("winmgmts:\\" & RemoteComputer & "\root\CIMV2")
If oWMI.Get("Win32_Service.Name='" & serviceName & "'").State = serviceState Then
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>Success</b></td>" & vbCrLf
Else
isError = True
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
End If
End Function
Function checkDcDiag(RemoteComputer, sTest)
Set oEXE = CreateObject("WScript.Shell").Exec("dcdiag.exe /test:" & sTest & " /s:" & RemoteComputer)
If InStr(oEXE.StdOut.ReadAll, "passed test " & sTest) > 0 Then
sHTML = sHTML & "<td bgcolor='LightGreen' align=center><b>Success</b></td>" & vbCrLf
Else
isError = True
sHTML = sHTML & "<td bgcolor='Red' align=center><b>Failed</b></td>" & vbCrLf
End If
End Function
Function checkRepadmin()
Set oEXE = CreateObject("WScript.Shell").Exec("repadmin.exe /replsummary * /bysrc /bydest")
checkRepadmin = "<br><pre>" & oEXE.StdOut.ReadAll & "</pre><br>" & vbCrLf
End Function
Function Enumerate(OU)
Dim subOUcomputers
computers = Array()
OU.Filter = Array("computer")
For Each oComputer in OU
computers = AddComputerItem(computers,oComputer.cn)
Next
OU.Filter = Array("container", "organizationalUnit")
For Each subou in OU
subOUcomputers = Enumerate(subou)
computers = CombineArrays(computers,subOUcomputers)
Next
Enumerate = computers
End Function
Function AddComputerItem(arr, val)
If IsArray(arr) then
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = val
AddComputerItem = arr
Else
arr = Array(1)
arr(0) = val
AddComputerItem = arr
End If
End Function
Function CombineArrays(array1,array2)
Dim combinedarray
combinedarray = Array()
For each x in array1
combinedarray=AddComputerItem(combinedarray,x)
Next
For each y in array2
combinedarray=AddComputerItem(combinedarray,y)
Next
CombineArrays = combinedarray
End Function
Function sendMail(txtBody)
If errorOnlyReport Then
If isError = False Then Exit Function
End If
oCDO = "http://schemas.microsoft.com/cdo/configuration/"
Set oMSG = CreateObject("CDO.Message")
oMSG.Subject = emailSubject
oMSG.From = emailFrom
oMSG.To = emailTo
If emailCc <> "" Then oMSG.Cc = emailCc
oMSG.HTMLBody = txtBody
If attachCSV Then oMSG.AddAttachment CreateObject("WScript.Shell").CurrentDirectory & "\ad-health-summary.csv"
With oMSG.Configuration.Fields
.Item(oCDO & "sendusing") = 2
.Item(oCDO & "smtpserverport") = emailPort
.Item(oCDO & "smtpauthenticate") = emailAuth
.Item(oCDO & "smtpconnectiontimeout") = 60
.Item(oCDO & "sendusername") = emailSenderUser
.Item(oCDO & "sendpassword") = emailSenderPassword
.Item(oCDO & "smtpserver") = emailServer
.Item(oCDO & "smtpusessl") = emailSSL
.Update
End With
oMSG.Send
End Function