-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanInetpubLogs.vbs
More file actions
224 lines (186 loc) · 7.92 KB
/
Copy pathCleanInetpubLogs.vbs
File metadata and controls
224 lines (186 loc) · 7.92 KB
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
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Author: Lucas Halbert <https://www.lhalbert.xyz>
'/ Date: 12.15.2017
'/ Last Edited: 03.15.2018
'/ Version: 2018.03.15
'/ Purpose: Cleans logs in the inetpub directory
'/ Description:
'/ License: BSD 3-Clause License
'/
'/ Copyright (c) 2017, Lucas Halbert
'/ All rights reserved.
'/
'/ Redistribution and use in source and binary forms, with or without
'/ modification, are permitted provided that the following conditions are met:
'/
'/ * Redistributions of source code must retain the above copyright notice, this
'/ list of conditions and the following disclaimer.
'/
'/ * Redistributions in binary form must reproduce the above copyright notice,
'/ this list of conditions and the following disclaimer in the documentation
'/ and/or other materials provided with the distribution.
'/
'/ * Neither the name of the copyright holder nor the names of its
'/ contributors may be used to endorse or promote products derived from
'/ this software without specific prior written permission.
'/
'/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
'/ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
'/ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
'/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
'/ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
'/ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
'/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
'/ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'/ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
'/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'/
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Revisions: 03.15.2018 Add options and comments to script
'/
'/ 10.05.2017 Initial Draft
'/
'////////////////////////////////////////////////////////////////////////////////////
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Initialize Variables
'/
'////////////////////////////////////////////////////////////////////////////////////
'/ Declare Variables
Const HKEY_LOCAL_MACHINE = &H80000002
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const strComputer = "." '/ String
Const strTempDir = "c:\Temp" '/ String
strVerboseLogging=False '/ Boolean
strLogFile = strTempDir & "\inetpublog_automation.log" '/ String
'/ Access necessary interfaces
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Configure logging to file
'/
'////////////////////////////////////////////////////////////////////////////////////
'/ Create Temp directory if it doesn't exist
If Not objFSO.FolderExists(strTempDir) Then
Set objFldr = objFSO.CreateFolder(strTempDir)
End If
'/ Create Log File if it doesn't exist
If Not objFSO.FileExists(strLogFile) Then
Set objLog = objFSO.CreateTextFile(strLogFile,True)
objLog.Close
End If
'/ Open Log file
Set objLog = objFSO.OpenTextFile(strLogFile,ForAppending,True)
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to exit and cleanup
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub AppExit(exitCode)
Call WriteLogData(Now & ": INFO: Exiting app with exit code (" & exitCode & ")", False)
Set objNetwork = Nothing
Set objFSO = Nothing
Set objWMIService = Nothing
Set colItems = Nothing
Set objSysInfo=Nothing
Set Shell = Nothing
Wscript.Quit(exitCode)
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to write log data with verbose option
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub WriteLogData(str, verbose)
WriteToLog(str)
if strVerboseLogging OR verbose Then
WriteToTerminal(str)
End If
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to write string to terminal
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub WriteToTerminal(str)
WScript.StdOut.WriteLine(str)
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to write string to log file
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub WriteToLog(str)
objLog.WriteLine(str)
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to display script usage
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub DisplayUsage()
Call WriteLogData("Script expects either two or three arguments", True)
Call WriteLogData("cscript CleanInetpubLogs.vbs <inetpub-log-path> <max-log-age[in days]> <verbose logging>", True)
Call WriteLogData("cscript CleanInetpublogs.vbs C:\inetpub\logs\LogFiles 30 [True/False]", True)
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Subroutine to sleep for specified number of milliseconds
'/
'////////////////////////////////////////////////////////////////////////////////////
Sub Sleep(time)
WScript.Sleep(time)
End Sub
'////////////////////////////////////////////////////////////////////////////////////
'/
'/ Main Script
'/
'////////////////////////////////////////////////////////////////////////////////////
Call WriteLogData(vbCrLf & vbCrLf & _
"/**********************************/" & vbCrLf & _
"/* Starting inetpub log cleanup */" & vbCrLf & _
"/**********************************/" & vbCrLf, False)
'/ Parse and collect Command line Arguments
If (WScript.Arguments.Count < 2) Then
Call WriteLogData(Now & ": ERROR: Too few arguments provided. Exiting...", True)
Call DisplayUsage()
AppExit(1)
ElseIf (WScript.Arguments.Count = 2) Then
strLogPath=WScript.Arguments(0)
intMaxAge=WScript.Arguments(1)
ElseIf (WScript.Arguments.Count = 3) Then
strLogPath=WScript.Arguments(0)
intMaxAge=WScript.Arguments(1)
strVerboseLogging=WScript.Arguments(2)
Else
Call WriteLogData(Now & ": ERROR: Too many arguments provided. Exiting...", True)
CCall DisplayUsage()
AppExit(1)
End If
'/ Set log file directrion
strLogFolder = "c:\inetpub\logs\LogFiles"
'/ Set Max age of log files in days
intMaxAge = 30
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFolder = objFSO.GetFolder(sLogFolder)
WScript.StdOut.Write("Checking logs in " & sLogFolder & vbCrLf)
WScript.StdOut.Write("Folder: " & colFolder.Path & vbCrLf)
For Each colSubfolder in colFolder.SubFolders
Set objFolder = objFSO.GetFolder(colSubfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
iFileAge = now-objFile.DateCreated
if iFileAge > (iMaxAge+1) then
WScript.StdOut.Write(objFile.Name & " is " & iFileAge & " days old and will be deleted" & vbCrLf)
objFSO.deletefile objFile, True
else
WScript.StdOut.Write(objFile.Name & " is " & iFileAge & " days old and will NOT be deleted" & vbCrLf)
end if
Next
Next
Set objFSO = Nothing