Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Fantasillion/08b3c4caad1b5fb24870c388f150519d to your computer and use it in GitHub Desktop.
Save Fantasillion/08b3c4caad1b5fb24870c388f150519d to your computer and use it in GitHub Desktop.
Check for specific Windows KB, then Uninstall KB, Stop and Disable wuauserv, create scheduled task in 30 days to start and enable automatic wuauserv, reboot prompt
# Check if running as administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$response = Read-Host "This script requires elevated permissions.
Do you want to run this script as an Administrator?
(y/n)"
if ($response -eq 'y') {
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`"" -Verb RunAs
exit
} else {
"Exiting script." | Out-Host
exit
}
}
$t = @"
_____ _____ _ _ _
| __ \ / ____| | | | |
| |__) |____ _____ _ __ (___ | |__ ___| | |
| ___/ _ \ \ /\ / / _ \ '__\___ \| '_ \ / _ \ | |
| | | (_) \ V V / __/ | ____) | | | | __/ | |
|_| \___/ \_/\_/ \___|_| |_____/|_| |_|\___|_|_|
"@
for ($i=0;$i -lt $t.length;$i++) {
if ($i%2) {
$c = "red"
}
elseif ($i%5) {
$c = "yellow"
}
elseif ($i%7) {
$c = "green"
}
else {
$c = "white"
}
write-host $t[$i] -NoNewline -ForegroundColor $c
}
" "| Out-Host
# Script started output to screen
"1. Script started." | Out-Host
" - "| Out-Host
# Check if KB5029263 exists
"2. Checking for KB5029263......." | Out-Host
try {
$kb = Get-HotFix | Where-Object { $_.Description -eq 'Update' -and $_.HotFixID -eq 'KB5029263' }
"2.a. Check complete." | Out-Host
# If KB exists, uninstall it
if ($kb) {
"2.b. Uninstalling KB5029263..." | Out-Host
wusa.exe /uninstall /kb:5029263 /quiet /norestart | Out-Host
"2.c. KB5029263 has been uninstalled." | Out-Host
} else {
"2.1. KB5029263 not found." | Out-Host
}
} catch {
"2.2. Error while checking or uninstalling KB5029263: $_. Continuing script..." | Out-Host
}
" -- "| Out-Host
# Check the status and startup type of the Windows Update Service (wuauserv)
try {
$wuauserv = Get-Service -Name "wuauserv"
"3. WUAUSERV" | Out-Host
"3.a. ------Windows Update Service (wuauserv) status:------"
Get-Service -Name "wuauserv" | select -property name,starttype,Status |Out-Host
"3.b. ------Windows Update Service (wuauserv) status check complete.------" | Out-Host
# If the service is running or its startup type is not 'Disabled', stop and disable it
if ($wuauserv.Status -ne 'Stopped' -or $wuauserv.StartType -ne 'Disabled') {
"3.1.a. Stopping and disabling Windows Update Service (wuauserv)..." | Out-Host
# Stop dependent services first
$wuauserv.DependentServices | ForEach-Object {
"3.1.b. Stopping dependent service $($_.Name)..." | Out-Host
Stop-Service -Name $_.Name -Force | Out-Host
}
# Now stop wuauserv
"3.2.a. Stopping wuauserv service $($_.Name)..." | Out-Host
Stop-Service -Name "wuauserv" -Force | Out-Host
# Disable wuauserv
"3.2.b. Disabling wuauserv service $($_.Name)..." | Out-Host
Set-Service -Name "wuauserv" -StartupType Disabled | Out-Host
"3.2.c. Windows Update Service (wuauserv) has been stopped and disabled succesfully." | Out-Host
} else {
"3.3. Windows Update Service (wuauserv) is already stopped and disabled." | Out-Host
}
} catch {
"3.4. Error while checking or modifying Windows Update Service (wuauserv): $_" | Out-Host
}
for ($i = 3; $i -gt 0; $i--) {
"3.4.a. Continuing in $i seconds..." | Out-Host
Start-Sleep -Seconds 1
}
"3.4.b. ------Windows Update Service (wuauserv) status:------"
Get-Service -Name "wuauserv" | select -property name,starttype,Status |Out-Host
"3.4.c. ------Windows Update Service (wuauserv) status check complete.------" | Out-Host
" --- "| Out-Host
# Check if the task already exists and if so, remove it
"4.1.a. Check if scheduled task 'EnableWUService' exists and if yes then remove it." | Out-Host
try {
$existingTask = Get-ScheduledTask -TaskName "EnableWUService" -ErrorAction SilentlyContinue
if ($existingTask) {
Unregister-ScheduledTask -TaskName "EnableWUService" -Confirm:$false
"4.1.b. Existing scheduled task 'EnableWUService' has been removed." | Out-Host
}
} catch {
"4.2. Error while checking or removing existing scheduled task: $_. Continuing script..." | Out-Host
}
" ---- "| Out-Host
# Schedule a task to restart and enable the service in 30 days
"5.1.a. Create Scheduled Task to start, enable and auto-start wuauserv in 30 days" | Out-Host
try {
# Define the action to be executed
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-NoProfile -WindowStyle Hidden -Command "Set-Service -Name ''wuauserv'' -StartupType Automatic; Start-Service -Name ''wuauserv''"'
# Define the trigger (30 days from now)
$triggerTime = (Get-Date).AddDays(30)
$trigger = New-ScheduledTaskTrigger -At $triggerTime -Once
# Register the task
$null = Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "EnableWUService" -User "NT AUTHORITY\SYSTEM" -RunLevel Highest
"5.1.b. Scheduled task has been created to enable and start Windows Update Service (wuauserv) service in 30 days." | Out-Host
} catch {
"5.2. Error while creating scheduled task: $_" | Out-Host
}
" ----- "| Out-Host
"6. Script completed." | Out-Host
"-----------------------------------------------------------------------------------------" | Out-Host
"-----------------------------------------------------------------------------------------" | Out-Host
" ------ "| Out-Host
# Prompt user to press any key to exit
"7. You will be asked to confirm if you want to reboot now. Press any key to continue..." | Out-Host
[System.Console]::ReadKey($true) > $null
# First reboot prompt
Add-Type -AssemblyName System.Windows.Forms
$result1 = [System.Windows.Forms.MessageBox]::Show("These changes require a reboot in order to take effect.
Do you want to reboot now?", "Reboot Prompt", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($result1 -eq "Yes") {
# Second confirmation prompt
$result2 = [System.Windows.Forms.MessageBox]::Show("Are you sure you wish to reboot now?
All open programs will be closed!", "Reboot Confirmation", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($result2 -eq "Yes") {
"Rebooting computer..." | Out-Host
Restart-Computer
} else {
"Reboot cancelled."
exit
}
} else {
"Reboot cancelled." | Out-Host
"Make sure to reboot to make changes take effect!" | Out-Host
"Press any key to exit..." | Out-Host
[System.Console]::ReadKey($true) > $null
exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment