|
| 1 | +[CmdletBinding()] |
| 2 | +param ( |
| 3 | + [switch]$RestartAfterUpdate |
| 4 | +) |
| 5 | + |
| 6 | +$script:SelectedUpdates = @() |
| 7 | + |
| 8 | +function Test-Admin { |
| 9 | + $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() |
| 10 | + $principal = New-Object System.Security.Principal.WindowsPrincipal($currentUser) |
| 11 | + if (-not $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) { |
| 12 | + Write-Host "Restarting script with administrator privileges..." |
| 13 | + Start-Process -FilePath "powershell" -ArgumentList "-ExecutionPolicy Bypass -NoProfile -File `"$PSCommandPath`"" -Verb RunAs |
| 14 | + exit |
| 15 | + } |
| 16 | +} |
| 17 | +Test-Admin |
| 18 | + |
| 19 | +function Install-PSWindowsUpdateModule { |
| 20 | + if (-not (Get-PackageProvider -ListAvailable | Where-Object Name -eq "NuGet")) { |
| 21 | + Install-PackageProvider -Name NuGet -Force -Confirm:$false |
| 22 | + } |
| 23 | + if ((Get-PSRepository -Name PSGallery).InstallationPolicy -ne "Trusted") { |
| 24 | + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted |
| 25 | + } |
| 26 | + if (-not (Get-Module -ListAvailable -Name PSWindowsUpdate)) { |
| 27 | + Install-Module -Name PSWindowsUpdate -Force -Scope CurrentUser -Confirm:$false |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function Enable-MicrosoftUpdate { |
| 32 | + Write-Host "Enabling Microsoft Update for driver updates..." |
| 33 | + Add-WUServiceManager -ServiceID "7971f918-a847-4430-9279-4a52d1efe18d" -AddServiceFlag 7 -Confirm:$false | Out-Null |
| 34 | +} |
| 35 | + |
| 36 | +function Show-DriverSelection { |
| 37 | + param ( |
| 38 | + [array]$Updates |
| 39 | + ) |
| 40 | + if ($Updates.Count -eq 0) { |
| 41 | + return @() |
| 42 | + } |
| 43 | + |
| 44 | + Add-Type -AssemblyName PresentationFramework |
| 45 | + |
| 46 | + $Window = New-Object System.Windows.Window |
| 47 | + $Window.Title = "Select drivers to install" |
| 48 | + $Window.Width = 500 |
| 49 | + $Window.Height = 400 |
| 50 | + $Window.WindowStartupLocation = "CenterScreen" |
| 51 | + |
| 52 | + $StackPanel = New-Object System.Windows.Controls.StackPanel |
| 53 | + |
| 54 | + $ListBox = New-Object System.Windows.Controls.ListBox |
| 55 | + $ListBox.SelectionMode = "Extended" |
| 56 | + foreach ($update in $Updates) { |
| 57 | + $item = New-Object System.Windows.Controls.ListBoxItem |
| 58 | + $item.Content = $update.Title.ToString().Trim() |
| 59 | + $ListBox.Items.Add($item) | Out-Null |
| 60 | + } |
| 61 | + $StackPanel.Children.Add($ListBox) | Out-Null |
| 62 | + |
| 63 | + $OKButton = New-Object System.Windows.Controls.Button |
| 64 | + $OKButton.Content = "OK" |
| 65 | + $OKButton.Margin = "10,10,10,10" |
| 66 | + $OKButton.Add_Click({ |
| 67 | + $Window.Tag = $ListBox.SelectedItems |
| 68 | + $Window.Close() |
| 69 | + }) |
| 70 | + $StackPanel.Children.Add($OKButton) | Out-Null |
| 71 | + |
| 72 | + $Window.Content = $StackPanel |
| 73 | + $Window.ShowDialog() | Out-Null |
| 74 | + |
| 75 | + $selectedUpdates = @() |
| 76 | + foreach ($selected in $Window.Tag) { |
| 77 | + $title = $selected.Content |
| 78 | + $update = $Updates | Where-Object { $_.Title.ToString().Trim() -eq $title } |
| 79 | + if ($update) { |
| 80 | + $selectedUpdates += $update |
| 81 | + } |
| 82 | + } |
| 83 | + return $selectedUpdates |
| 84 | +} |
| 85 | + |
| 86 | +function Update-Drivers { |
| 87 | + Write-Host "Checking for driver updates..." |
| 88 | + $Updates = Get-WUList -MicrosoftUpdate -Category "Drivers" |
| 89 | + |
| 90 | + if ($Updates.Count -gt 0) { |
| 91 | + Write-Host "Available driver updates:" |
| 92 | + $selection = Show-DriverSelection -Updates $Updates |
| 93 | + |
| 94 | + $selection = @($selection) |
| 95 | + |
| 96 | + if ($selection.Count -gt 0) { |
| 97 | + Write-Host "Installing selected driver updates..." |
| 98 | + $selection | Format-Table ComputerName, Status, KB, Size, Title -AutoSize |
| 99 | + $selection | Get-WUInstall -AcceptAll -IgnoreReboot -Confirm:$false | Out-Null |
| 100 | + Write-Host "Driver updates installed successfully!" |
| 101 | + |
| 102 | + $restartChoice = Read-Host "Do you want to restart now? (Y/N)" |
| 103 | + if ($restartChoice -match "^[Yy]$") { |
| 104 | + Write-Host "Restarting the system in 10 seconds..." |
| 105 | + Start-Sleep -Seconds 10 |
| 106 | + Restart-Computer -Force |
| 107 | + } |
| 108 | + } |
| 109 | + else { |
| 110 | + Write-Host "No drivers were selected for update." |
| 111 | + } |
| 112 | + } |
| 113 | + else { |
| 114 | + Write-Host "No driver updates found." |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +Install-PSWindowsUpdateModule |
| 119 | +Enable-MicrosoftUpdate |
| 120 | +Update-Drivers |
| 121 | + |
| 122 | +if (-not $PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Silent')) { |
| 123 | + Write-Host "Press any key to exit..." |
| 124 | + $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |
| 125 | +} |
0 commit comments