Created
December 27, 2019 02:15
-
-
Save ocdtrekkie/c4f9fa0e9dd9efb225404f91afe25635 to your computer and use it in GitHub Desktop.
Windows Update and Elevation Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports WUApiLib | |
Imports System.Security.Principal | |
Module Module1 | |
Sub Main() | |
'https://docs.microsoft.com/en-us/windows/win32/wua_sdk/searching--downloading--and-installing-updates | |
Dim winUpdateSession As New UpdateSession() | |
winUpdateSession.ClientApplicationID = "HAController" | |
Dim winUpdateSearcher As IUpdateSearcher = winUpdateSession.CreateUpdateSearcher() | |
Try | |
Console.WriteLine("Checking for updates...") | |
Dim winUpdateResult As ISearchResult = winUpdateSearcher.Search("IsInstalled=0 and Type='Software' And IsHidden=0") | |
Dim winUpdatesToDownload As New UpdateCollection | |
Console.WriteLine("Found " & winUpdateResult.Updates.Count & " updates" & Environment.NewLine) | |
For Each winUpdate As IUpdate In winUpdateResult.Updates | |
Console.WriteLine(winUpdate.Title) | |
If winUpdate.InstallationBehavior.CanRequestUserInput = True Then | |
Console.WriteLine("Skipping " & winUpdate.Title & " because it requires user input") | |
Else | |
If winUpdate.EulaAccepted = False Then | |
Console.WriteLine("Accepting EULA for " & winUpdate.Title) | |
winUpdate.AcceptEula() | |
Else | |
Console.WriteLine(winUpdate.Title) | |
End If | |
winUpdatesToDownload.Add(winUpdate) | |
End If | |
Next | |
'https://stackoverflow.com/a/49115285 | |
Dim principal = New WindowsPrincipal(WindowsIdentity.GetCurrent()) | |
If principal.IsInRole(WindowsBuiltInRole.Administrator) Then | |
Console.WriteLine("I am an admin") | |
Else | |
Console.WriteLine("I am not an admin") | |
End If | |
Console.ReadKey(True) | |
If winUpdatesToDownload.Count > 0 Then | |
Console.WriteLine("Downloading updates...") | |
Dim winUpdateDownloader As UpdateDownloader = winUpdateSession.CreateUpdateDownloader() | |
winUpdateDownloader.Updates = winUpdatesToDownload | |
winUpdateDownloader.Download() | |
Dim winRebootMayBeRequired = False | |
Dim winUpdatesToInstall As New UpdateCollection | |
For Each winUpdate As IUpdate In winUpdatesToDownload | |
If winUpdate.IsDownloaded = True Then | |
winUpdatesToInstall.Add(winUpdate) | |
If winUpdate.InstallationBehavior.RebootBehavior > 0 Then | |
winRebootMayBeRequired = True | |
End If | |
End If | |
Next | |
If winUpdatesToInstall.Count > 0 Then | |
Console.WriteLine("Installing updates...") | |
Dim winUpdateInstaller As UpdateInstaller = winUpdateSession.CreateUpdateInstaller() | |
winUpdateInstaller.Updates = winUpdatesToInstall | |
Dim winUpdateInstallResult As IInstallationResult = winUpdateInstaller.Install() | |
Console.WriteLine("Installation result: " & winUpdateInstallResult.ResultCode) | |
Console.WriteLine("Reboot required: " & winUpdateInstallResult.RebootRequired) | |
End If | |
Console.ReadKey(True) | |
End If | |
Catch ex As Exception | |
Console.WriteLine(ex.Message) | |
End Try | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment