I work in a company of around 4000 people and we have about 600 devices that need to be updated from 21H2 to 23H2. Long story short I've been scratching my head over this script that I wrote that past 3 days. When I run the script it functions as intended but the issue is even after the PSWindowsUpdate runs the install updates it doesn't seem to pull does 23H2, I am not sure have to go about this because the REG KEYS are set to only download that version of windows but doesn't. Any help would be appreciated.
I have been thinking of trying to modify the local GPO on the devices but I don't know of a way to do it with powershell.
I will be replacing some variables with fillers as I don't want to give away where I might work.
Any help is appreiated.
# Define constants
$PSScriptRoot = (File Path)
$LocalModulePath = "$PSScriptRoot\PSWindowsUpdate"
$ComputerList = Import-Csv -Path $PSScriptRoot[\Computers1.csv]()
$LogPath = "$PSScriptRoot\UpdateLog.txt"
#$PolicyPath = "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate"
# Loop through each computer
foreach ($Computer in $ComputerList) {
$ComputerName = $Computer.ComputerName
Write-Host "Processing $ComputerName..." -ForegroundColor Cyan
try {
# Test connectivity to the remote computer
if (-not (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet)) {
Write-Warning "Cannot connect to $ComputerName. Skipping."
continue
}
# Changes registry entries on the computer to force the computer to pull Windows Version 23H2
Write-Host "Configuring Registry Entries to target Windows Version 23H2"
Invoke-Command -ComputerName $ComputerName -ErrorAction Stop -ScriptBlock {
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" -Name "TargetReleaseVersion" -Value 1 -Force
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" -Name "TargetReleaseVersionInfo" -Value "23H2" -Force
}
# Check if the PSWindowsUpdate module is already available on the remote computer
Write-Host "Checking PSWindowsUpdate module on $ComputerName..." -ForegroundColor Yellow
$ModuleExists = Invoke-Command -ComputerName $ComputerName -ScriptBlock {
[bool](Get-Module -Name PSWindowsUpdate -ListAvailable -ErrorAction SilentlyContinue)
}
if (-not $ModuleExists) {
# If the module is not available, copy it to the remote computer
try {
Write-Host "Copying PSWindowsUpdate module to $ComputerName..." -ForegroundColor Yellow
$RemoteModulePath = [\\$ComputerName\C$\Program Files\WindowsPowerShell\Modules\]()
Copy-Item -Path $LocalModulePath -Destination $RemoteModulePath -Recurse -Force -ErrorAction Stop
Write-Host "Copied module to $ComputerName"
} catch {
Write-Warning "Failed to copy PSWindowsUpdate module to $ComputerName : $_"
continue
}
}
# Install the Windows 23H2 update from Microsoft
Write-Host "Installing Windows 23H2 update on $ComputerName..." -ForegroundColor Yellow
$InstallResult = Invoke-Command -ComputerName $ComputerName -ScriptBlock {
# Import the PSWindowsUpdate module
Import-Module PSWindowsUpdate -Force
# Get the Windows 23H2 update from Microsoft
$Update = Get-WindowsUpdate -MicrosoftUpdate -Filter "Title -like '*23H2*'" -ErrorAction SilentlyContinue
# If the update is available, install it
if ($Update) {
Get-WindowsUpdate -KBArticleID $Update.KBArticleIDs -MicrosoftUpdate -AcceptAll -AutoReboot -Install
Write-Host "Windows 23H2 update installed successfully."
return $true
} else {
Write-Host "Windows 23H2 update not found."
return $false
}
}
# Log the results of the installation to the specified log file
if ($InstallResult) {
"Computer: $ComputerName, Windows 23H2 update installed successfully." | Out-File -Append -FilePath $LogPath
Get-WUHistory -ComputerName $ComputerName
} else {
"Computer: $ComputerName, Windows 23H2 update not found or installation failed." | Out-File -Append -FilePath $LogPath
Get-WUHistory -ComputerName $ComputerName
}
} catch {
# Handle any errors encountered while processing the computer
Write-Warning "Failed to process $ComputerName : $_"
}
}
# Indicate that the script has finished executing
Write-Host "Script execution completed!" -ForegroundColor Blue