0
votes

So I have been slowly trying to teach myself VBscript, mainly from copying code from the internet, and merging functions and concepts to achieve my desired goal. I feel so silly posting this question, but I am hitting a mental brick wall and it's driving me mad!

SCENARIO: So this is a script which was copied from multiple sources that I tried to merge and edit. Basically I have a batch script which calls this VBS file: the batch has multiple variables. The first set of variables is a bunch of KB #'s separated by spaces (eg: 971033 2952664) and a final variable telling the VBS where to store the LOG file for this script. I have messed around with the coding, and it appears to parse the variables correcttly (EG: Line 17-21 list about 20 variables on individual lines) but for whatever reason, my FOR loop starting at line 35 seems to result in a huge log file with many, many duplicate entries. First, here is the BAT that calls the VBS (edited for example):

set LOG=%~dp0log.log
IF /I "%OSVER%" == "7" (
    FOR /f "skip=8 eol=; delims=    # tokens=1" %%A IN (KB.ini) DO (
        SET KB=!KB! %%A
    )
)
CSCRIPT //NOLOGO Script.vbs%KB% "%LOG%"

That final command looks like so when passed to the vbs file (Example):

CSCRIPT //NOLOGO Script.vbs 2902907 2922324 2976987 "C:\Users\My Owner\Destop\log.log"

Script.VBS

On Error Resume Next
If Wscript.Arguments.Count < 1 Then
    WScript.Quit 1
End If

Dim fso, updateSession, updateSearcher, dt
Set fso = CreateObject("Scripting.FileSystemObject")
Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateUpdateSearcher()

Dim logPath
set logPath = fso.OpenTextFile(Wscript.Arguments(Wscript.Arguments.Count - 1), 8, True)

Dim kbList()
For i = 0 to Wscript.Arguments.Count - 2
    Redim Preserve kbList(i)
    kbList(i) = Wscript.Arguments(i)
Next


For Each hotfixId In kbList
    Wscript.Echo hotfixId
Next

Log (" - Searching for updates (Please wait)"), True
Dim searchResult
Set searchResult = updateSearcher.Search("IsInstalled=0")
Log ("   - " & CStr(searchResult.Updates.Count) & " found"), True
Log (" - Hiding Updates"), True

Dim index, index2, update, kbArticleId
For index = 0 To searchResult.Updates.Count - 1
    Set update = searchResult.Updates.Item(index)
    For index2 = 0 To update.KBArticleIDs.Count - 1
        kbArticleId = update.KBArticleIDs(index2)
        For Each hotfixId in kbList
            If hotfixId = kbArticleId Then
                If update.IsHidden = False Then
                    Log ("   - Hiding KB" & hotfixId), True
                    update.IsHidden = True
                Else
                    Log ("   - KB" & hotfixId & " already hidden"), True
                End If          
            Else
                Log ("   - KB" & hotfixId & " not found"), True
            End If
        Next
    Next
Next

'Function for logging
Function log(logStr, toConsole)
    If toConsole Then
        WScript.Echo logStr
        logPath.WriteLine logStr
    Else
        logPath.WriteLine logStr
    End If
End Function

'Close log file
logPath.Close
'// EOF

The code looks great to me, but the Log file tells a different story. Here is the Log generated:

- Searching for updates (Please wait)
  - 58 found
- Hiding Updates
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB not found
  - KB971033 already hidden
  - KB not found
  - KB not found
  - KB not found
  - KB3021917 already hidden
  - KB not found
  - KB not found
  - KB3068708 already hidden
  - Hiding KB2952664
  - KB not found
  - KB not found
  - KB not found
  - KB3075249 already hidden
  - KB3080149 already hidden
  - KB3083324 already hidden
  - KB not found
  - KB not found

Etc etc...

The Log craps out at 1024 lines (interesting....) and if you look carefully, the "already hidden" message does appear every so often, so it seems the script is slightly working, but it is looping every variable every round (Ex: 20 variables x 20 Loops = 400 Lines)

I HAVE to be missing something, but cannot for the life of me figure it out. Please, what minor error am I making and how do I fix this issue?

1

1 Answers

0
votes

The logic of comparison was wrong: only the first element of kbList was checked whether it's equal to the current kbArticleId.

Solution: use a boolean flag and check it once the entire inner loop is finished:

Dim found
For index2 = 0 To update.KBArticleIDs.Count - 1
    kbArticleId = update.KBArticleIDs(index2)
    found = False
    For Each hotfixId in kbList
        If hotfixId = kbArticleId Then
            found = True
            If update.IsHidden = False Then
                Log ("   - Hiding KB" & hotfixId), True
                update.IsHidden = True
            Else
                Log ("   - KB" & hotfixId & " already hidden"), True
            End If          
        End If          
    Next
    If found = False Then
        Log ("   - KB" & hotfixId & " not found"), True
    End If
Next