Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 分析cscript数组中的变量列表时出现问题_Batch File_Vbscript - Fatal编程技术网

Batch file 分析cscript数组中的变量列表时出现问题

Batch file 分析cscript数组中的变量列表时出现问题,batch-file,vbscript,Batch File,Vbscript,所以我一直在慢慢地自学VBscript,主要是从互联网上复制代码,合并函数和概念,以达到我想要的目标。我觉得把这个问题贴出来很傻,但我的脑子碰到了一堵砖墙,快把我逼疯了 场景:这是一个从我试图合并和编辑的多个源复制的脚本。基本上我有一个批处理脚本来调用这个VBS文件:批处理有多个变量。第一组变量是一组由空格分隔的KB,例如:971033 2952664,最后一个变量告诉VBS在何处存储此脚本的日志文件。我把编码搞得一团糟,它似乎正确地解析了变量,例如:第17-21行列出了每行大约20个变量,但出

所以我一直在慢慢地自学VBscript,主要是从互联网上复制代码,合并函数和概念,以达到我想要的目标。我觉得把这个问题贴出来很傻,但我的脑子碰到了一堵砖墙,快把我逼疯了

场景:这是一个从我试图合并和编辑的多个源复制的脚本。基本上我有一个批处理脚本来调用这个VBS文件:批处理有多个变量。第一组变量是一组由空格分隔的KB,例如:971033 2952664,最后一个变量告诉VBS在何处存储此脚本的日志文件。我把编码搞得一团糟,它似乎正确地解析了变量,例如:第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
等等等等

日志显示1024行有趣的。。。。如果仔细观察,已经隐藏的消息确实每隔一段时间出现一次,因此脚本似乎稍微起作用,但它每轮循环每个变量,例如:20个变量x 20个循环=400行


我必须错过一些东西,但我一辈子都无法找到它。请问,我犯了什么小错误?如何解决这个问题?

比较逻辑错误:只检查了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

感谢wOxxOm,我更改了您的建议,但在“Dim find=True”上出现错误。我删除了Dim并将其放在最上面,但遗憾的是日志的输出要短得多,但仍然有空白条目。我已经用新的结果更新了主帖子中的日志。另外,日志中有20 KB的变量,但有55 KB的行。不要将其放在顶部,只需删除dim,否则不会为每篇文章重置该值。谢谢!这就解决了问题!在某种程度上,它似乎是可行的,但通过几个位置良好的echo命令,我意识到我的逻辑方法也有缺陷。感谢您的帮助,使其功能正常:
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