~siggi-bjarnason/siggivbscript/vbscript

« back to all changes in this revision

Viewing changes to HPNAValidate.vbs

  • Committer: Siggi Bjarnason
  • Date: 2015-03-01 10:21:25 UTC
  • Revision ID: siggi@bjarnason.us-20150301102125-mwb7g8wbp5t7newa
Added folder processing to HPNAValidate.vbs

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
'Nothing below here is user configurable proceed at your own risk.
39
39
 
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
45
46
 
46
47
const ForReading   = 1
47
48
const ForWriting   = 2
49
50
 
50
51
        bLog  = false
51
52
        bFile = false
 
53
        bFolder = false
52
54
        strScriptFullName = wscript.ScriptFullName
53
55
        strParts = split (strScriptFullName, "\")
54
56
        strScriptName = strParts(ubound(strParts))
113
115
        end if
114
116
 
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"
118
 
        end if
119
 
 
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"
124
 
                        wscript.quit
 
120
                strInFileName = strInFile & ".txt"
 
121
                if not fso.FileExists(strInFileName) then
 
122
                        if fso.FolderExists(strInFile) then
 
123
                                bFolder = true
 
124
                                if bLog then writelog "Found a valid folder path " & strInFile
 
125
                                if right(strInFile,1)<> "\" then
 
126
                                        strInFile = strInFile & "\"
 
127
                                end if
 
128
                        end if
 
129
                else
 
130
                                bFolder = false
 
131
                                strInFile = strInFileName
125
132
                end if
126
 
        wend
127
 
 
128
 
        select case right(strInFile,4)
129
 
                case ".txt"
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"
137
 
                                        wscript.quit
138
 
                                end if
139
 
                        wend
140
 
                        if bLog then WriteLog "CSV file is " & strInFileName
141
 
                case ".csv"
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"
149
 
                                        wscript.quit
150
 
                                end if
151
 
                        wend
152
 
                        if bLog then WriteLog "template file is " & strTemplateName
153
 
        end select
154
 
 
155
 
        if right(strInFile,3) = "csv" then
156
 
 
157
 
        end if
158
 
 
159
 
        'parse out default cofiguration folder from template filename
160
 
        strConfFolder = Mid(strTemplateName, 1, InStrRev(strTemplateName, "\")) & DefConfigFolderName
161
 
 
162
 
        iExtLoc = InStrRev(strTemplateName, ".")
163
 
        iPatEndLoc = InStrRev(strTemplateName, "\") + 1
164
 
        strPartName = Mid(strTemplateName, iPatEndLoc, iExtLoc-iPatEndLoc)
 
133
        end if
 
134
 
 
135
        if not bFolder then
 
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"
 
140
                                wscript.quit
 
141
                        end if
 
142
                wend
 
143
 
 
144
                select case right(strInFile,4)
 
145
                        case ".txt"
 
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"
 
153
                                                wscript.quit
 
154
                                        end if
 
155
                                wend
 
156
                                if bLog then WriteLog "CSV file is " & strInFileName
 
157
                        case ".csv"
 
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"
 
165
                                                wscript.quit
 
166
                                        end if
 
167
                                wend
 
168
                                if bLog then WriteLog "template file is " & strTemplateName
 
169
                end select
 
170
                strConfFolder = Mid(strTemplateName, 1, InStrRev(strTemplateName, "\")) & DefConfigFolderName
 
171
        else
 
172
                strConfFolder = strInFile & DefConfigFolderName
 
173
        end if
 
174
 
165
175
 
166
176
        ' Log the full path names for all the relevant files and folders
167
177
        if bLog then WriteLog "saving configuations to " & strConfFolder
168
178
 
 
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"
172
183
        end if
173
184
 
174
185
        'Initializing Dictionaries, aka ordered arrays
175
 
        set dictHostNames  = CreateObject("Scripting.Dictionary")
176
 
 
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
181
 
        objTemplate.close
182
 
        set objTemplate = Nothing
183
 
 
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)
188
 
        Else
189
 
                WriteLog "Can't find " & strInFileName
190
 
                WriteLog "Can't proceed without it. Exiting!!!"
191
 
                wscript.quit
192
 
        End If
193
 
 
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), ",")
198
 
        bCont = True
199
 
        iLineNo = 1
200
 
 
201
 
        if bLog then writelog "starting the loop"
202
 
 ' loop through each line in the CSV
203
 
        While not objFileIn.atendofstream
204
 
                strHostName = ""
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)
212
 
                                case "hostname"
213
 
                                        strHostName = strLineParts(x)
214
 
                                        if dictHostNames.exists(strHostName) then
215
 
                                                writelog "already processsed " & strHostName
216
 
                                                writelog "skipping line " & left(strline,40)
217
 
                                                bCont = false
218
 
                                                exit for
219
 
                                        else
220
 
                                                bCont = True
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")
 
190
 
 
191
        if bFolder then
 
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)
 
195
                Set fc = f.Files
 
196
 
 
197
                'Loop through the array of CIQ's and process them.
 
198
                For Each f1 in fc
 
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)
 
204
                                case ".txt"
 
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
 
211
                                                else
 
212
                                                        writelog "Unable to find a match for " & f1
 
213
                                                end if
 
214
                                        end if
 
215
                                case ".csv"
 
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
 
222
                                                else
 
223
                                                        writelog "Unable to find a match for " & f1
 
224
                                                end if
223
225
                                        end if
224
226
                                case else
225
 
                                        strVariable =  VarDelim & strHeaderParts(x) & VarDelim
226
 
                                        iloc = instr(1,strTemplate,strVariable,vbTextCompare)
227
 
                                        if iloc = 0 then
228
 
                                                writelog strHeaderParts(x) & " not found in template file"
229
 
                                        end if
230
 
                                        strConfig = replace(strConfig, strVariable,strLineParts(x),vbTextCompare)
231
 
                        end select
 
227
                                        if bLog then WriteLog "neither txt nor csv file"
 
228
                                end select
232
229
                next
233
 
 
234
 
                if bCont then
235
 
                        strRegPatern = VarDelimReEx & ".+" & VarDelimReEx
236
 
                        set re = new regexp
237
 
                        re.pattern = strRegPatern
238
 
                        re.IgnoreCase = true
239
 
                re.Global = True
240
 
                Set Matches = re.Execute(strConfig)
241
 
 
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
246
 
                                writelog Match.value
247
 
                        next
248
 
                    end if
249
 
 
250
 
                    if strHostName = "" then
251
 
                        strHostName = "Line" & iLineNo
252
 
                    end if
253
 
 
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
259
 
                        objConfig.close
260
 
                        set objConfig = nothing
261
 
                        iLineNo=iLineNo + 1
 
230
                if bLog then
 
231
                        writelog "processed the following files in folder mode:"
 
232
                        for each strVar in dictFiles
 
233
                                writelog strVar & "::" & dictFiles.item(strVar) & ";"
 
234
                        next
262
235
                end if
263
 
        wend
264
 
 
265
 
WriteLog "configuations saved to " & strConfFolder
266
 
WriteLog "Done. "
267
 
 
268
 
'Cleanup, close out files, release resources, etc.
269
 
if bFile then
270
 
        objLogOut.close
271
 
        set objLogOut = Nothing
272
 
end if
273
 
 
274
 
objFileIn.close
275
 
set objFileIn = Nothing
276
 
Set fso = Nothing
277
 
set dictHostNames  = Nothing
278
 
set Matches = Nothing
279
 
 
280
 
 
281
 
wscript.echo now & vbtab & "All Done, Cleanup complete"
 
236
 
 
237
        else
 
238
                ProcessFiles strInFileName, strTemplateName
 
239
        end if
 
240
 
 
241
        WriteLog "configuations saved to " & strConfFolder
 
242
        WriteLog "Done. "
 
243
 
 
244
        iCount = dictNotInCSV.count
 
245
        if iCount > 0 then
 
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)
 
250
                next
 
251
        end if
 
252
 
 
253
        iCount = dictNotInTemplate.count
 
254
        if iCount > 0 then
 
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)
 
259
                next
 
260
        end if
 
261
 
 
262
        'Cleanup, close out files, release resources, etc.
 
263
        if bFile then
 
264
                objLogOut.close
 
265
                set objLogOut = Nothing
 
266
        end if
 
267
 
 
268
        Set fso = Nothing
 
269
        set dictHostNames  = Nothing
 
270
        set Matches = Nothing
 
271
 
 
272
 
 
273
        wscript.echo now & vbtab & "All Done, Cleanup complete"
282
274
 
283
275
function CleanStr (strMsg)
284
276
'-------------------------------------------------------------------------------------------------'
396
388
 Msgbox strHelpMsg', "Help message"
397
389
end if
398
390
 
399
 
 
400
391
end sub
 
392
 
 
393
function ProcessFiles (strCSVname, strTName)
 
394
        iExtLoc = InStrRev(strTName, ".")
 
395
        iPatEndLoc = InStrRev(strTName, "\") + 1
 
396
        strPartName = Mid(strTName, iPatEndLoc, iExtLoc-iPatEndLoc)
 
397
        dictHostNames.RemoveAll
 
398
        
 
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
 
403
        objTemplate.close
 
404
        set objTemplate = Nothing
 
405
 
 
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)
 
409
 
 
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), ",")
 
414
        bCont = True
 
415
        iLineNo = 1
 
416
 
 
417
        if bLog then writelog "starting the loop"
 
418
 ' loop through each line in the CSV
 
419
        While not objFileIn.atendofstream
 
420
                strHostName = ""
 
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)
 
428
                                case "hostname"
 
429
                                        strHostName = strLineParts(x)
 
430
                                        strhostNameA = strHostName
 
431
                                        if dictHostNames.exists(strHostName) then
 
432
                                                do
 
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
 
439
                                        else
 
440
                                                bCont = True
 
441
                                                dictHostNames.add strHostName,"2"
 
442
                                                writelog "processing " & strHostName
 
443
                                        end if
 
444
                                case else
 
445
                                        strVariable =  VarDelim & strHeaderParts(x) & VarDelim
 
446
                                        iloc = instr(1,strTemplate,strVariable,vbTextCompare)
 
447
                                        if iloc = 0 then
 
448
                                                if not dictNotInTemplate.exists(strHeaderParts(x)) then
 
449
                                                        dictNotInTemplate.add strHeaderParts(x), strCSVname
 
450
                                                end if
 
451
                                                'writelog strHeaderParts(x) & " not found in template file"
 
452
                                        end if
 
453
                                        strConfig = replace(strConfig, strVariable,strLineParts(x),vbTextCompare)
 
454
                        end select
 
455
                next
 
456
 
 
457
                if bCont then
 
458
                        strRegPatern = VarDelimReEx & ".+" & VarDelimReEx
 
459
                        set re = new regexp
 
460
                        re.pattern = strRegPatern
 
461
                        re.IgnoreCase = true
 
462
                re.Global = True
 
463
                Set Matches = re.Execute(strConfig)
 
464
 
 
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
 
470
                                                end if
 
471
                        next
 
472
                    end if
 
473
 
 
474
                    if strHostName = "" then
 
475
                        strHostName = "Line" & iLineNo
 
476
                    end if
 
477
 
 
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
 
483
                        objConfig.close
 
484
                        set objConfig = nothing
 
485
                        iLineNo=iLineNo + 1
 
486
                end if
 
487
        wend
 
488
 
 
489
        objFileIn.close
 
490
        set objFileIn = Nothing
 
491
 
 
492
end function
 
 
b'\\ No newline at end of file'