Skip to content

Commit f914827

Browse files
authored
feat: update drivers through windows update (#1350)
### Questions - [x] Did you test your changes or double-check that they work? - [x] Did you read and follow the [Atlas Contribution Guidelines](https://docs.atlasos.net/contributions/)? ### Describe your pull request Completes https://github.com/orgs/Atlas-OS/projects/7?pane=issue&itemId=70790980 This is to me and @TheyCreeper's interpretation of what the feature suggests. Remake because the last one was an absolute mess.
1 parent a904609 commit f914827

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@echo off
2+
set "script=%windir%\AtlasModules\Scripts\ScriptWrappers\UpdateDrivers.ps1"
3+
4+
set "___args="%~f0" %*"
5+
fltmc > nul 2>&1 || (
6+
echo Administrator privileges are required.
7+
powershell -c "Start-Process -Verb RunAs -FilePath 'cmd' -ArgumentList """/c $env:___args"""" 2> nul || (
8+
echo You must run this script as admin.
9+
if "%*"=="" pause
10+
exit /b 1
11+
)
12+
exit /b
13+
)
14+
15+
if not exist "%script%" (
16+
echo Script not found: "%script%"
17+
pause
18+
exit /b 1
19+
)
20+
21+
powershell -ExecutionPolicy Bypass -NoProfile -File "%script%"
22+
23+
echo.
24+
pause > null
25+
exit /b 0
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)