Automating Windows Updates in Microsoft Autopilot
As enterprises look to streamline their device management processes, integrating automatic updates within the Microsoft Autopilot setup has become increasingly pivotal. The Autopilot process simplifies the setup and pre-configuration of new devices, making it a cornerstone of modern IT administration. However, one aspect that often remains manually managed is the installation of Windows updates during the initial setup phase. Here, I propose a method that not only automates this process but does so in a robust and efficient manner, leveraging PowerShell and the PSWindowsUpdate module. It's been tested upgrading feature packs like Windows 11 22H2 to 23H2 as well.
Why Automate Windows Updates During Autopilot?
Integrating Windows updates into the Autopilot process ensures that devices are fully patched and secure from the first time they are powered on. This reduces the administrative overhead and enhances security by closing potential vulnerabilities from outdated software.
Despite the extensive discussions by experts like Michael Niehaus and other contributors in the field, a practical, ready-to-deploy PowerShell script that fully automates this process is seldom seen. The script I've developed addresses this gap, providing a comprehensive solution that installs all prerequisites and patches automatically.
The PowerShell Script Explained
Here’s a breakdown of the PowerShell script that can be wrapped into a Win32 application, where the command line is set to:
powershell.exe -ex bypass -file InstallUpdates.ps1.
# Install Windows Updates
# If we are running as a 32-bit process on an x64 system, re-launch as a 64-bit process
if ("$env:PROCESSOR_ARCHITEW6432" -ne "ARM64") {
if (Test-Path "$($env:WINDIR)\SysNative\WindowsPowerShell\v1.0\powershell.exe") {
& "$($env:WINDIR)\SysNative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy bypass -NoProfile -File "$PSCommandPath"
Exit $lastexitcode
}
}
Start-Transcript "C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\$($myinvocation.mycommand.name -replace '\..*$').log"
# Install latest NuGet package provider
$PackageProvider = Install-PackageProvider -Name "NuGet" -Force -ErrorAction Stop -Verbose:$false
# Ensure default PSGallery repository is registered
Register-PSRepository -Default -ErrorAction SilentlyContinue
# Attempt to get the installed PowerShellGet module
$PowerShellGetInstalledModule = Get-InstalledModule -Name "PowerShellGet" -ErrorAction SilentlyContinue -Verbose:$false
if ($PowerShellGetInstalledModule -ne $null) {
# Attempt to locate the latest available version of the PowerShellGet module from repository
$PowerShellGetLatestModule = Find-Module -Name "PowerShellGet" -ErrorAction Stop -Verbose:$false
if ($PowerShellGetLatestModule -ne $null) {
if ($PowerShellGetInstalledModule.Version -lt $PowerShellGetLatestModule.Version) {
Update-Module -Name "PowerShellGet" -Scope "AllUsers" -Force -ErrorAction Stop -Confirm:$false -Verbose:$false
}
}
else {
}
}
else {
# PowerShellGet module was not found, attempt to install from repository
Install-Module -Name "PackageManagement" -Force -Scope AllUsers -AllowClobber -ErrorAction Stop -Verbose:$false
Install-Module -Name "PowerShellGet" -Force -Scope AllUsers -AllowClobber -ErrorAction Stop -Verbose:$false
}
Install-Module -Name PSWindowsUpdate -Force -Scope AllUsers -AllowClobber
Import-Module PSWindowsUpdate -Scope Global
Get-WindowsUpdate
Install-WindowsUpdate -AcceptAll -IgnoreReboot
Stop-Transcript Key Components:
• Environment Check and Re-launch: Ensures the script runs in the correct architecture environment for compatibility.
• Transcript Logging: Starts a log in the specified directory, crucial for tracking and debugging.
• Module Installation and Updates: Ensures that all necessary PowerShell modules and updates are installed and up to date.
• Windows Updates Installation: Fetches and installs available Windows updates, with an option to accept all updates and ignore prompts for reboots.
Adding to Microsoft Intune
To deploy this script via Microsoft Intune:
- Wrap the script into a Win32 app with Microsoft Win32 Content Prep Tool
- Set up the command line for execution as described.
- Use a detection rule to check for the existence of the log file at C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\InstallUpdates.log. This confirms whether the script has run on the device.
- Configure Intune to enforce a mandatory device restart: This ensures that all installed updates are properly applied, securing the device before it is handed over to the end-user.
- Add it to your ESP page as blocking app.
Conclusion
Automating the installation of Windows updates during the Autopilot setup process significantly streamlines device management and enhances security posture. The provided PowerShell script facilitates this automation, ensuring that new devices are ready and secure for deployment with minimal manual intervention. By incorporating these practices into your IT strategy, you can achieve a higher standard of operational efficiency and security.
Comments
Post a Comment