1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| 'New session
Dim updateSession As New WUApiLib.UpdateSession
'Searcher and search string
Dim usearch As New WUApiLib.UpdateSearcher
ecrire("Searching for Updates...")
Dim searchResult As WUApiLib.ISearchResult = usearch.Search("IsInstalled = 0 and IsAssigned = 1 and Type='Software'")
If searchResult.Updates.Count > 0 Then
ecrire("Updates Found:")
For a As Integer = 0 To searchResult.Updates.Count - 1
ecrire("Update Found:" & searchResult.Updates(a).Title.ToString)
Next
Dim updatesToDownload As WUApiLib.UpdateCollection = New WUApiLib.UpdateCollection
updatesToDownload = searchResult.Updates
'Download updates
Dim downloader As WUApiLib.UpdateDownloader = updateSession.CreateUpdateDownloader
downloader.Updates = updatesToDownload
downloader.Download()
ecrire("List of Downloaded updates...")
'create update object
For a As Integer = 0 To searchResult.Updates.Count - 1
If searchResult.Updates(a).IsDownloaded Then
ecrire(searchResult.Updates(a).Title.ToString)
End If
Next
Else
ecrire("No Updates Found:")
End If
'Create collection
ecrire("Creating update collection...")
'Colection for installation
ecrire("Creating collection with downloaded and installable updates:")
Dim updatesToInstall = New WUApiLib.UpdateCollection
For a As Integer = 0 To searchResult.Updates.Count - 1
If searchResult.Updates(a).IsDownloaded Then
Dim update As WUApiLib.IUpdate = searchResult.Updates(a)
updatesToInstall.Add(update)
ecrire(searchResult.Updates(a).Title.ToString & "added")
End If
Next
If updatesToInstall.Count < 1 Then
ecrire("No updates to install")
Exit Sub
End If
'Create installer
Dim installer As WUApiLib.UpdateInstaller = updateSession.CreateUpdateInstaller
installer.Updates = updatesToInstall
installer.ForceQuiet = True
ecrire("Installing updates")
Dim result As WUApiLib.IInstallationResult = installer.Install()
result.GetUpdateResult()
ecrire(result.ResultCode.ToString())
For i As Integer = 0 To updatesToInstall.Count - 1
ecrire("Installation result for : " & updatesToInstall(i).Title.ToString & " = " & result.GetUpdateResult(i).ToString & " Code result = " & result.GetUpdateResult(i).ResultCode.ToString & " Reboot required = " & result.GetUpdateResult(i).RebootRequired.ToString)
' Console.WriteLine("Installation result for : " & updatesToInstall(i).Title.ToString & " = " & result.GetUpdateResult(i).ToString & " Code result = " & result.GetUpdateResult(i).ResultCode.ToString & " Reboot required = " & result.GetUpdateResult(i).RebootRequired.ToString)
Next
ecrire("-------------------------------")
ecrire("Result for the whole update (ResultCode)" & result.ResultCode)
ecrire("Result for the whole update (RebootRequired)" & result.RebootRequired.ToString)
ecrire("Result for the whole update (HResult)" & result.HResult)
Catch ex As Exception
ecrire("An error happened" & ex.ToString)
End Try
End Sub |
Partager