在 VBScript 中,我需要一个代码从 searchResult.Updates.Count 中减去 1,这样 Count = 0 并且 WScript.Quit 将随之运行

在 VBScript 中,我需要一个代码从 searchResult.Updates.Count 中减去 1,这样 Count = 0 并且 WScript.Quit 将随之运行

我已经在 Google 上搜索了几个小时但仍未找到答案。

'ServerSelection values
ssDefault = 0
ssManagedServer   = 1
ssWindowsUpdate   = 2
ssOthers          = 3

'InStr values
intSearchStartChar = 1

dim strTitle

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

updateSearcher.ServerSelection = ssWindowsUpdate
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")

For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)

If update.Title = "Intel Corporation driver update for Intel(R) HD Graphics" Then 
I need a code on this line to subtract 1 from searchResult.Updates.Count, so that Count = 0 and WScript.Quit will be run consequently.
End If

If searchResult.Updates.Count = 0 Then
WScript.Quit
End If

set objShell = createobject("wscript.shell")  
objShell.Run("ms-settings:windowsupdate") , 0

Next
WScript.Quit

上面的 VBScript 检查 Windows 更新。我想从 中删除一些更新(例如驱动程序更新)searchResult.Updates.Count,以便searchResult.Updates.Count = 0WScript.Quit将随之运行。我希望它在找到指定更新时不采取任何措施,而仅在找到其他更新时采取行动。

答案1

希望这可以帮助:

Dim nMyRes : nMyRes = 20

MsgBox "nMyRes before loop: " & nMyRes

For i = 0 To nMyRes-1
    If i = 5 Then nMyRes = nMyRes - 1
    If i = 10 Then nMyRes = nMyRes - 1
Next

MsgBox "nMyRes after loop: " & nMyRes

此代码片段运行良好。就您而言,您有一个对象 (searchResult),但我不确定您是否可以更改它。这就是为什么我要定义一个新变量并像这样使用它:

Dim nMyNewvar : nMyNewvar  = searchResult.Updates.Count - 1
' ...
' ...
For I = 0 To nMyNewvar  

' ...
If update.Title = "bla-bla" Then nMyNewvar = nMyNewvar - 1

[已编辑]

' Add this one row here
 Dim nMyNewvar : nMyNewvar  = searchResult.Updates.Count - 1
 'For I = 0 To searchResult.Updates.Count-1
 For I = 0 To nMyNewvar 
  Set update = searchResult.Updates.Item(I)

  If update.Title = "bla-bla" Then 
   'I need a code on this line to subtract 1 from ....
   nMyNewvar = nMyNewvar - 1
 End If

  If nMyNewvar  = 0 Then
   WScript.Quit
  End If

  set objShell = createobject("wscript.shell")  
  objShell.Run("ms-settings:windowsupdate") , 0

Next

答案2

改变:

If update.Title = "Intel Corporation driver update for Intel(R) HD Graphics" Then 
    subtract 1 from searchResult.Updates.Count,
    so that Count = 0 and WScript.Quit will be run consequently.
    End If

If searchResult.Updates.Count = 0 Then
WScript.Quit
End If

到:

If update.Title = "Intel Corporation driver update for Intel(R) HD Graphics" Then 
WScript.Quit
End If

但是,听起来你打算将一系列事物添加到其中。在这种情况下,你的逻辑是错误的,因为它会在找到列表中的任何一项后停止执行。你需要以某种方式从实际列表中删除该项目。

此外,您也不能简单地更改计数:它是由对象设置的只读属性,并且它不会从索引中删除该项目。您还正在运行一个使用它作为限制的 for 循环。

对一组您要改变的值运行循环的更好方法是使用类似“while count > 0 do”的方法。

微软有一个例子,他们遍历列表,如果他们想下载(在对每个列表运行一些测试之后),他们添加该项目将被传递到更新程序的第二个列表。

答案3

读了duDE的上述回答后,我想到了以下几点:

'ServerSelection values
ssDefault = 0
ssManagedServer   = 1
ssWindowsUpdate   = 2
ssOthers          = 3
'InStr values
intSearchStartChar = 1

Dim strTitle

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

updateSearcher.ServerSelection = ssWindowsUpdate
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")

If searchResult.Updates.Count = None Then 
WScript.Quit 
Else 
Dim value : value = searchResult.Updates.Count 
End If 

For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)

'The following excluded updates have to be hidden via 'Show or hide updates' beforehand.
'Otherwise, they will be downloaded and installed by Windows Update.
If update.Title = "The full update title" Then Value = Value - 1
If update.Title = "The full update title" Then Value = Value - 1
If update.Title = "The full update title" Then Value = Value - 1

An_item = "KB2267602" 
Set Act=CreateObject("wscript.shell")
If instr(update.Title, An_item) <> 0 Then 
Act.Run("""C:\Program Files\Windows Defender\MpCmdRun.exe"" ""-SignatureUpdate"""), 0
value = value - 1 
End If
Next

If Value = 0 Then 
WScript.Quit 
End If

result = MsgBox (_
"The number of available updates is(" & Value & ")." & vbNewLine &_
"Do you want to open【Windows Update】?", vbYesNo + vbQuestion,_
"【There are available updates.】")
Select Case result
Case vbYes
Act.run("ms-settings:windowsupdate")
Case vbNo
WScript.Quit
End Select

在我这边它运行正常。只有当有可用更新时才会出现对话框,以排除不需要的更新。请告诉我它在您这边是否运行正常或可以改进。

相关内容