用于检查,下载和安装Microsoft的Windows更新的VB脚本,其中包含文本文件中的排除项

时间:2013-12-27 22:43:43

标签: vbscript

我有一个脚本可以检查,下载和安装Windows更新。从http://gallery.technet.microsoft.com/scriptcenter/VB-Script-to-Check-and-620579cd#content获得了基本脚本。

我正在尝试通过读取包含更新的完全限定名称的文本文件来排除特定更新。如果我的文本文件中只有一行,则效果很好。当我有多行时,脚本会显示排除并添加更新的多个结果。它可能只是我的For Next循环的位置,但我正在敲打我的脑袋。这是我的脚本部分,我遇到了问题:

'Create collection of updates to download
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("C:\Source\Scripts\CheckUpdates\CheckUpdates\ExcludedUpdates.txt", ForReading) 
Const ForReading = 1 
Dim arrExclusions() 
i = 0 
Do Until objFile.AtEndOfStream 
Redim Preserve arrExclusions(i) 
arrExclusions(i) = objFile.ReadLine 
i = i + 1 
Loop 
objFile.Close
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
For each strExcludedUpdate in arrExclusions
    If update.Title = strExcludedUpdate Then
        Wscript.Echo I + 1 & "> EXCLUDED: " & update.Title 
        addThisUpdate = false
    Else
        If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> Skipping: " & update.Title & _
        " because it requires user input"
        Else
            If update.EulaAccepted = false Then
                WScript.Echo vbCrLf & I + 1 & "> NOTE: " & update.Title & _
                " has a license agreement that must be accepted:"
                WScript.Echo update.EulaText
                WScript.Echo vbCrLF & "Do you accept this license agreement? (Y/N)"
                strInput = WScript.StdIn.ReadLine
                'strInput = "Y"     'remove remark to auto answer Y (Yes) without user input.
                WScript.Echo 
                If (strInput = "Y" or strInput = "y") Then
                    update.AcceptEula()
                    addThisUpdate = true
                Else
                    WScript.Echo I + 1 & "> ----- Skipping: " & update.Title & _
                    " because the license agreement was declined"
                End If
            Else
                addThisUpdate = true
            End If
        End If
    End If
Next
If addThisUpdate = true Then
    WScript.Echo I + 1 & "> ADDING: " & update.Title 
    updatesToDownload.Add(update)
End If
    Next

    If updatesToDownload.Count = 0 Then
      WScript.Echo vbCrLf & "No applicable updates to download. Closing in 5 seconds..."
WScript.Sleep 5000
      WScript.Quit
    End If

1 个答案:

答案 0 :(得分:0)

有几件事:

  1. 您阅读的Const应该高于打开文件的行。

  2. 我不是100%肯定你想要做的那个for循环,所以我创建了一个似乎做你想要的替代版本;我已将扫描文件拆分为已排除的文件,然后重新测试addThisUpdate并执行其余操作。不幸的是,在我测试的机器上,没有可用于测试EULA内容的更新!它运行,不会产生重复。我希望这有帮助。 我没有粘贴整个脚本


  3. Const ForReading = 1 
    Set objFile = objFSO.OpenTextFile("C:\Source\Scripts\CheckUpdates\CheckUpdates\ExcludedUpdates.txt", ForReading) 
    
    Dim arrExclusions() 
    i = 0 
    Do Until objFile.AtEndOfStream 
        Redim Preserve arrExclusions(i) 
        arrExclusions(i) = objFile.ReadLine 
        i = i + 1 
    Loop 
    objFile.Close
    For I = 0 to searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        addThisUpdate = true
        For each strExcludedUpdate in arrExclusions
            If update.Title = strExcludedUpdate Then
                Wscript.Echo I + 1 & "> EXCLUDED: " & update.Title 
                addThisUpdate = false
            End If
        Next
        If addThisUpdate = True then
            If update.InstallationBehavior.CanRequestUserInput = true Then
                WScript.Echo I + 1 & "> Skipping: " & update.Title & _
                " because it requires user input"
            Else
                If update.EulaAccepted = false Then
                    WScript.Echo vbCrLf & I + 1 & "> NOTE: " & update.Title & _
                    " has a license agreement that must be accepted:"
                    WScript.Echo update.EulaText
                    WScript.Echo vbCrLF & "Do you accept this license agreement? (Y/N)"
                    strInput = WScript.StdIn.ReadLine
                    'strInput = "Y"     'remove remark to auto answer Y (Yes) without user input.
                    WScript.Echo 
                    If (strInput = "Y" or strInput = "y") Then
                        update.AcceptEula()
                        addThisUpdate = true
                    Else
                        WScript.Echo I + 1 & "> ----- Skipping: " & update.Title & _
                        " because the license agreement was declined"
                    End If
                End If
            End If
        End If
    
        If addThisUpdate = true Then
            WScript.Echo I + 1 & "> ADDING: " & update.Title 
            updatesToDownload.Add(update)
        End If
    Next
    
    If updatesToDownload.Count = 0 Then
        WScript.Echo vbCrLf & "No applicable updates to download. Closing in 5 seconds..."
        WScript.Sleep 5000
        WScript.Quit
    End If