2

I am trying to adjust this code so it forces the Windows Update search to check online from Microsoft rather than the local WSUS server. This code works fine if I run it when I am outside my company network, but I want to run from the company network and bypass the WSUS server.

    Private Sub CheckForUpdates()

    Dim objUpdateSession As WUApiLib.UpdateSession
    Dim objUpdateSearcher As WUApiLib.UpdateSearcher
    Dim objSearchResults As WUApiLib.ISearchResult
    Dim objUpdateDownloader As WUApiLib.UpdateDownloader
    Dim NowInstallThem As WUApiLib.UpdateInstaller
    Dim NumPatches As Integer = -1
    Dim Updates As New WUApiLib.UpdateCollection

    Try
        objUpdateSession = New WUApiLib.UpdateSession
        objUpdateSearcher = objUpdateSession.CreateUpdateSearcher()
        objSearchResults = objUpdateSearcher.Search("IsInstalled=0 and Type='Software'")
        NumPatches = objSearchResults.Updates.Count

        MessageBox.Show("Number of patches: " & NumPatches.ToString)
        Dim patch As WUApiLib.IUpdate

        For a = 0 To NumPatches - 1
            patch = objSearchResults.Updates.Item(a)
            Updates.Add(patch)
        Next

        If NumPatches > 0 Then
            objUpdateDownloader.Updates = Updates
            objUpdateDownloader.Download()
            MessageBox.Show("patches downloaded")

            NowInstallThem.Updates = Updates
            NowInstallThem.Install()
            MessageBox.Show("patches installed")
        End If

    Catch ex As Exception
    End Try
End Sub
Steve Dorr
  • 136
  • 9
  • Group policy currently set in the computer ? – aybe Mar 25 '15 at 14:59
  • It is not that, because I can run Windows Update from the Control Panel and choose the option to go directly to MS. I'm just trying to figure out how to do it via the API – Steve Dorr Mar 26 '15 at 02:28

2 Answers2

0

This is managed by

UseWUServer

on the clients registry key

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
  1. Save the value of the key
  2. Set it to "0"
  3. Install a timer which resets the value after 10 seconds
  4. Immediatelly run your search searchResult = updateSearcher.Search(...)

If the key or the value isn't present then it uses already the Microsoft Update Servers,.

This is a little bit hacky, but the only way I found.

marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
0

Realizing this is an old question (with a fairly new answer), the way to do this is by setting the ServerSelection property of IUpdateSearcher (in the initial example, the objUpdateSearcher instance) to ssWindowsUpdate (2).

Stroniax
  • 718
  • 5
  • 15