38
38
'Nothing below here is user configurable proceed at your own risk.
40
40
'Variable declaration
41
Dim strConfFolder, strTemplateName, strScriptFullName, strLogFileName, strConfigFileName, strInFileName
42
Dim strLine, strConfig, strTemplate, iArg, strParts, strScriptName, strInput, strArgParts, strHeaderParts, bCont
43
Dim strHostIP, strHostName, strRegPatern, strLineParts, strInFile, strVariable, strPartName, iExtLoc, iPatEndLoc, iloc
44
Dim objLogOut, objTemplate, objConfig, objFileIn, dictHostNames, bLog, bFile, re, Matches, Match, x, fso, iLineNo
41
Dim strConfFolder, strTemplateName, strScriptFullName, strLogFileName, strConfigFileName, strInFileName, bFolder, strHostNameA
42
Dim strLine, strConfig, strTemplate, iArg, strParts, strScriptName, strInput, strArgParts, strHeaderParts, bCont, iHostNo
43
Dim strHostIP, strHostName, strRegPatern, strLineParts, strInFile, strVariable, strPartName, iExtLoc, iPatEndLoc, iloc, iCount
44
Dim objLogOut, objTemplate, objConfig, objFileIn, dictHostNames, bLog, bFile, re, Matches, Match, x, fso, iLineNo, strVar
45
dim dictNotInTemplate, dictNotInCSV, f, fc, f1, dictFiles
46
47
const ForReading = 1
47
48
const ForWriting = 2
115
117
'Attempt to add a .txt extension if infile is not valid
118
'If that's not valid check to see if it is a valid folder
116
119
if not fso.FileExists(strInFile) then
117
strInFile = strInFile & ".txt"
120
while not fso.FileExists(strInFile)
121
strInFile = UserInput("input file " & strInFile & " is not valid. Please provide new one or leave blank to abort:")
122
if strInFile = "" then
123
writelog "No input provided, aborting"
120
strInFileName = strInFile & ".txt"
121
if not fso.FileExists(strInFileName) then
122
if fso.FolderExists(strInFile) then
124
if bLog then writelog "Found a valid folder path " & strInFile
125
if right(strInFile,1)<> "\" then
126
strInFile = strInFile & "\"
131
strInFile = strInFileName
128
select case right(strInFile,4)
130
if bLog then WriteLog "Template file is " & strInFile
131
strTemplateName = strInFile
132
strInFileName = Mid(strInFile, 1, InStrRev(strInFile, ".")) & "csv"
133
while not fso.FileExists(strInFileName)
134
strInFileName = UserInput("CSV file " & strInFileName & " is not valid. Please provide new one or leave blank to abort:")
135
if strInFileName = "" then
136
writelog "No input provided, aborting"
140
if bLog then WriteLog "CSV file is " & strInFileName
142
if bLog then WriteLog "CSV file is " & strInFile
143
strInFileName = strInFile
144
strTemplateName = Mid(strInFile, 1, InStrRev(strInFile, ".")) & "txt"
145
while not fso.FileExists(strTemplateName)
146
strTemplateName = UserInput("template file " & strTemplateName & " is not valid. Please provide new one or leave blank to abort:")
147
if strTemplateName = "" then
148
writelog "No input provided, aborting"
152
if bLog then WriteLog "template file is " & strTemplateName
155
if right(strInFile,3) = "csv" then
159
'parse out default cofiguration folder from template filename
160
strConfFolder = Mid(strTemplateName, 1, InStrRev(strTemplateName, "\")) & DefConfigFolderName
162
iExtLoc = InStrRev(strTemplateName, ".")
163
iPatEndLoc = InStrRev(strTemplateName, "\") + 1
164
strPartName = Mid(strTemplateName, iPatEndLoc, iExtLoc-iPatEndLoc)
136
while not fso.FileExists(strInFile)
137
strInFile = UserInput("input file " & strInFile & " is not valid. Please provide new one or leave blank to abort:")
138
if strInFile = "" then
139
writelog "No input provided, aborting"
144
select case right(strInFile,4)
146
if bLog then WriteLog "Template file is " & strInFile
147
strTemplateName = strInFile
148
strInFileName = Mid(strInFile, 1, InStrRev(strInFile, ".")) & "csv"
149
while not fso.FileExists(strInFileName)
150
strInFileName = UserInput("CSV file " & strInFileName & " is not valid. Please provide new one or leave blank to abort:")
151
if strInFileName = "" then
152
writelog "No input provided, aborting"
156
if bLog then WriteLog "CSV file is " & strInFileName
158
if bLog then WriteLog "CSV file is " & strInFile
159
strInFileName = strInFile
160
strTemplateName = Mid(strInFile, 1, InStrRev(strInFile, ".")) & "txt"
161
while not fso.FileExists(strTemplateName)
162
strTemplateName = UserInput("template file " & strTemplateName & " is not valid. Please provide new one or leave blank to abort:")
163
if strTemplateName = "" then
164
writelog "No input provided, aborting"
168
if bLog then WriteLog "template file is " & strTemplateName
170
strConfFolder = Mid(strTemplateName, 1, InStrRev(strTemplateName, "\")) & DefConfigFolderName
172
strConfFolder = strInFile & DefConfigFolderName
166
176
' Log the full path names for all the relevant files and folders
167
177
if bLog then WriteLog "saving configuations to " & strConfFolder
179
'if Configuration folder doesn't exists create it
169
180
if not fso.FolderExists(strConfFolder) then
170
181
fso.CreateFolder(strConfFolder)
171
182
if bLog then WriteLog strConfFolder & " did not exists so I created it"
174
185
'Initializing Dictionaries, aka ordered arrays
175
set dictHostNames = CreateObject("Scripting.Dictionary")
177
if bLog then writelog "Reding in the template file to memory"
178
'open up the template file and read it all into single variable, then close the file
179
set objTemplate = fso.OpenTextFile(strTemplateName, ForReading, False)
180
strTemplate = objTemplate.readall
182
set objTemplate = Nothing
184
'open up the csv file
185
if bLog then writelog "Opening up the CSV and starting processing"
186
If fso.fileexists(strInFileName) Then
187
Set objFileIn = fso.opentextfile(strInFileName)
189
WriteLog "Can't find " & strInFileName
190
WriteLog "Can't proceed without it. Exiting!!!"
194
if bLog then writelog "reading in header"
195
'read in the first header line and split it into an array
196
strLine = objFileIn.readline
197
strHeaderParts = split (Trim(strLine), ",")
201
if bLog then writelog "starting the loop"
202
' loop through each line in the CSV
203
While not objFileIn.atendofstream
205
strLine = objFileIn.readline
206
strLineParts = split(Trim(strLine), ",")
207
strConfig = strTemplate
208
for x=0 to ubound(strHeaderParts)
209
select case lcase (strHeaderParts(x) )
210
case "primaryipaddress"
211
strHostIP = strLineParts(x)
213
strHostName = strLineParts(x)
214
if dictHostNames.exists(strHostName) then
215
writelog "already processsed " & strHostName
216
writelog "skipping line " & left(strline,40)
221
dictHostNames.add strHostName,""
222
if bLog then writelog "processing " & strHostName
186
set dictHostNames = CreateObject("Scripting.Dictionary")
187
set dictNotInTemplate = CreateObject("Scripting.Dictionary")
188
set dictNotInCSV = CreateObject("Scripting.Dictionary")
189
set dictFiles = CreateObject("Scripting.Dictionary")
192
if bLog then WriteLog "starting folder operations"
193
'Create an array of all the file names in the provided folder
194
Set f = fso.GetFolder(strInFile)
197
'Loop through the array of CIQ's and process them.
199
iExtLoc = InStrRev(f1, ".")
200
iPatEndLoc = InStrRev(f1, "\") + 1
201
strPartName = Mid(f1, iPatEndLoc, iExtLoc-iPatEndLoc)
202
if bLog then WriteLog "evaluating " & f1
203
select case right(f1,4)
205
if not dictFiles.exists(strPartName & "txt") then
206
dictFiles.add strPartName & "txt", "t-template"
207
strInFileName = Mid(f1, 1, InStrRev(f1, ".")) & "csv"
208
if fso.FileExists(strInFileName) then
209
dictFiles.add strPartName & "csv", "t-csv"
210
ProcessFiles strInFileName, f1
212
writelog "Unable to find a match for " & f1
216
if not dictFiles.exists(strPartName & "csv") then
217
dictFiles.add strPartName & "csv", "c-csv"
218
strTemplateName = Mid(f1, 1, InStrRev(f1, ".")) & "txt"
219
if fso.FileExists(strTemplateName) then
220
dictFiles.add strPartName & "txt", "c-template"
221
ProcessFiles f1, strTemplateName
223
writelog "Unable to find a match for " & f1
225
strVariable = VarDelim & strHeaderParts(x) & VarDelim
226
iloc = instr(1,strTemplate,strVariable,vbTextCompare)
228
writelog strHeaderParts(x) & " not found in template file"
230
strConfig = replace(strConfig, strVariable,strLineParts(x),vbTextCompare)
227
if bLog then WriteLog "neither txt nor csv file"
235
strRegPatern = VarDelimReEx & ".+" & VarDelimReEx
237
re.pattern = strRegPatern
240
Set Matches = re.Execute(strConfig)
242
if Matches.count > 0 then
243
writelog "following variables in the template were not replaced"
244
writelog "as they didn't have a matching variable in the CSV"
245
For Each Match in Matches
250
if strHostName = "" then
251
strHostName = "Line" & iLineNo
254
strConfigFileName = strConfFolder & strHostName & "-" & strPartName & ".txt"
255
if bLog then WriteLog "Saving configuration to "
256
if bLog then WriteLog strConfigFileName
257
set objConfig = fso.createtextfile(strConfigFileName, true)
258
objConfig.write strConfig
260
set objConfig = nothing
231
writelog "processed the following files in folder mode:"
232
for each strVar in dictFiles
233
writelog strVar & "::" & dictFiles.item(strVar) & ";"
265
WriteLog "configuations saved to " & strConfFolder
268
'Cleanup, close out files, release resources, etc.
271
set objLogOut = Nothing
275
set objFileIn = Nothing
277
set dictHostNames = Nothing
278
set Matches = Nothing
281
wscript.echo now & vbtab & "All Done, Cleanup complete"
238
ProcessFiles strInFileName, strTemplateName
241
WriteLog "configuations saved to " & strConfFolder
244
iCount = dictNotInCSV.count
246
writelog iCount & " variables in the template were not replaced"
247
writelog "as they didn't have a matching variable in the CSV"
248
for each strVar in dictNotInCSV
249
writelog strVar & " in " & dictNotInCSV.item(strVar)
253
iCount = dictNotInTemplate.count
255
writelog iCount & " variables in the CSV file weren't used "
256
writelog "as they didn't have a matching variable in the template"
257
for each strVar in dictNotInTemplate
258
writelog strVar & " in " & dictNotInTemplate.item(strVar)
262
'Cleanup, close out files, release resources, etc.
265
set objLogOut = Nothing
269
set dictHostNames = Nothing
270
set Matches = Nothing
273
wscript.echo now & vbtab & "All Done, Cleanup complete"
283
275
function CleanStr (strMsg)
284
276
'-------------------------------------------------------------------------------------------------'
396
388
Msgbox strHelpMsg', "Help message"
393
function ProcessFiles (strCSVname, strTName)
394
iExtLoc = InStrRev(strTName, ".")
395
iPatEndLoc = InStrRev(strTName, "\") + 1
396
strPartName = Mid(strTName, iPatEndLoc, iExtLoc-iPatEndLoc)
397
dictHostNames.RemoveAll
399
if bLog then writelog "Reading in the template file to memory"
400
'open up the template file and read it all into single variable, then close the file
401
set objTemplate = fso.OpenTextFile(strTName, ForReading, False)
402
strTemplate = objTemplate.readall
404
set objTemplate = Nothing
406
'open up the csv file
407
if bLog then writelog "Opening up the CSV and starting processing"
408
Set objFileIn = fso.opentextfile(strCSVname, ForReading, False)
410
if bLog then writelog "reading in header"
411
'read in the first header line and split it into an array
412
strLine = objFileIn.readline
413
strHeaderParts = split (Trim(strLine), ",")
417
if bLog then writelog "starting the loop"
418
' loop through each line in the CSV
419
While not objFileIn.atendofstream
421
strLine = objFileIn.readline
422
strLineParts = split(Trim(strLine), ",")
423
strConfig = strTemplate
424
for x=0 to ubound(strHeaderParts)
425
select case lcase (strHeaderParts(x))
426
case "primaryipaddress"
427
strHostIP = strLineParts(x)
429
strHostName = strLineParts(x)
430
strhostNameA = strHostName
431
if dictHostNames.exists(strHostName) then
433
iHostNo = dictHostNames.item(strHostNameA)
434
strhostNameA = strHostName & "-" & iHostNo
435
loop while dictHostNames.exists(strHostNameA)
436
dictHostNames.add strHostNameA, iHostNo+1
437
strHostName = strHostNameA
438
writelog "processing " & strHostName
441
dictHostNames.add strHostName,"2"
442
writelog "processing " & strHostName
445
strVariable = VarDelim & strHeaderParts(x) & VarDelim
446
iloc = instr(1,strTemplate,strVariable,vbTextCompare)
448
if not dictNotInTemplate.exists(strHeaderParts(x)) then
449
dictNotInTemplate.add strHeaderParts(x), strCSVname
451
'writelog strHeaderParts(x) & " not found in template file"
453
strConfig = replace(strConfig, strVariable,strLineParts(x),vbTextCompare)
458
strRegPatern = VarDelimReEx & ".+" & VarDelimReEx
460
re.pattern = strRegPatern
463
Set Matches = re.Execute(strConfig)
465
if Matches.count > 0 then
466
For Each Match in Matches
467
' writelog Match.value
468
if not dictNotInCSV.exists(Match.value) then
469
dictNotInCSV.add Match.value, strTName
474
if strHostName = "" then
475
strHostName = "Line" & iLineNo
478
strConfigFileName = strConfFolder & strHostName & "-" & strPartName & ".txt"
479
if bLog then WriteLog "Saving configuration to "
480
if bLog then WriteLog strConfigFileName
481
set objConfig = fso.createtextfile(strConfigFileName, true)
482
objConfig.write strConfig
484
set objConfig = nothing
490
set objFileIn = Nothing
b'\\ No newline at end of file'