所以我一直在慢慢地尝试自学VBscript,主要是从互联网上复制代码,并合并功能和概念以实现我想要的目标。我觉得发布这个问题太傻了,但我正在打一个心灵砖墙,这让我发疯了!
SCENARIO:所以这是一个从我尝试合并和编辑的多个来源复制的脚本。基本上我有一个调用此VBS文件的批处理脚本:批处理有多个变量。第一组变量是由空格分隔的一堆KB#(例如:971033 2952664)和一个告诉VBS在哪里存储该脚本的LOG文件的最终变量。我已经弄乱了编码,它似乎正确地解析变量(EG:第17-21行列出了各个行上的20个变量)但是无论出于何种原因,从第35行开始的FOR循环似乎导致了一个巨大的日志包含许多重复条目的文件。首先,这是调用VBS的BAT(例如编辑):
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%"
当传递给vbs文件时,最后一个命令看起来是这样的(例子):
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
代码看起来很棒,但日志文件讲述了一个不同的故事。这是生成的日志:
- 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
等等...
日志以1024行(有趣......)缩小,如果你仔细观察,那么&#34;已经隐藏了#34;消息确实经常出现,所以看起来脚本稍微有点工作,但它每周循环每个变量(例如:20个变量x 20个循环= 400个行)
我必须遗漏一些东西,但不能为我的生活弄清楚。请问,我做了什么小错误,如何解决这个问题?
答案 0 :(得分:0)
比较逻辑错误:只检查kbList
的第一个元素是否等于当前kbArticleId
。
解决方案:使用布尔标志并在整个内循环完成后检查它:
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