-
Notifications
You must be signed in to change notification settings - Fork 0
/
FWindowsUpdateReboot.ps1
153 lines (136 loc) · 5.59 KB
/
FWindowsUpdateReboot.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# rotate-active-hours.ps1
# PowerShell script to rotate Active Hours and set up scheduled task
param (
[switch]$Rotate,
[switch]$Uninstall
)
# Check if running as administrator
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
# Relaunch the script with administrator privileges
$arguments = "& `"${PSCommandPath}`""
if ($Rotate) {
$arguments += " -Rotate"
}
if ($Uninstall) {
$arguments += " -Uninstall"
}
Start-Process -FilePath "PowerShell" -Verb RunAs -ArgumentList $arguments
exit
}
# Define variables
$taskName = "FWindowsUpdateReboot"
$system32Path = "$env:windir\System32"
$scriptName = "FWindowsUpdateReboot.ps1"
$destinationPath = Join-Path -Path $system32Path -ChildPath $scriptName
$sourceScriptPath = $MyInvocation.MyCommand.Path
if ($Uninstall) {
# Remove the scheduled task if it exists
try {
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction Stop
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Host "Scheduled task '$taskName' has been removed."
} catch {
Write-Host "Scheduled task '$taskName' does not exist. (Skipping)"
}
# Prompt user about script removal
$removeScript = Read-Host "Would you like to remove the script from System32? (Y/N)"
if ($removeScript -eq 'Y' -or $removeScript -eq 'y') {
if (Test-Path $destinationPath) {
Remove-Item -Path $destinationPath -Force
Write-Host "Script removed from System32."
} else {
Write-Host "Script not found in System32. (Skipping)"
}
}
Write-Host "Successfully uninstalled $taskName.`nPress Enter to exit..."
$null = Read-Host
exit
}
if ($Rotate) {
# Rotate Active Hours
$activeHoursDuration = 18 # Maximum allowed active hours duration in hours
$currentHour = (Get-Date).Hour
$newActiveHoursStart = $currentHour
$newActiveHoursEnd = ($currentHour + $activeHoursDuration) % 24
# Update the registry keys
$registryPath = "HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"
Set-ItemProperty -Path $registryPath -Name "ActiveHoursStart" -Value $newActiveHoursStart -Type DWord
Set-ItemProperty -Path $registryPath -Name "ActiveHoursEnd" -Value $newActiveHoursEnd -Type DWord
Write-Host "Active hours updated: Start=$newActiveHoursStart, End=$newActiveHoursEnd"
} else {
# Install the script
# Copy the script to System32 if it's not already there
if ($sourceScriptPath -ieq $destinationPath) {
Write-Host "Script is already in System32."
} else {
Write-Host "Copying script to System32..."
try {
Copy-Item -Path $sourceScriptPath -Destination $destinationPath -Force
Write-Host "Script copied to $destinationPath"
} catch {
Write-Error "Failed to copy script to System32."
exit 1
}
}
# Remove existing scheduled task if it exists and set it up again
try {
# Check if the task exists
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction Stop
# If it exists, remove it
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Host "Scheduled task '$taskName' existed and has been removed."
} catch {
Write-Host "Scheduled task '$taskName' does not exist. Proceeding to create it."
}
Write-Host "Creating scheduled task '$taskName'..."
# Define the action to run the script from System32 with the -Rotate parameter
$scriptPath = Join-Path $system32Path $scriptName
# Build the action command without inner quotes
$action = "powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File $scriptPath -Rotate"
# Enclose the entire action in double quotes for the /TR parameter
$actionArgument = "`"$action`""
# Use SchTasks.exe to create a task that runs every hour indefinitely
$schtasksArguments = @(
'/Create'
'/RU', 'SYSTEM'
'/SC', 'HOURLY'
'/TN', $taskName
'/TR', $actionArgument
'/F'
)
# Execute the command to create the scheduled task
$result = Start-Process -FilePath "schtasks.exe" -ArgumentList $schtasksArguments -Wait -PassThru -NoNewWindow
if ($result.ExitCode -ne 0) {
Write-Error "Failed to create scheduled task. Exit code: $($result.ExitCode)"
Write-Host "Failed to create scheduled task. See the above error message for details."
}
else {
Write-Host "Scheduled task '$taskName' has been created successfully."
Write-Host "$taskName has been installed successfully."
Write-Host "Give Windows Update the finger, you have finally defeated it."
Write-Host "`n"
Write-Host " / \"
Write-Host " |\_/|"
Write-Host " |---|"
Write-Host " | |"
Write-Host " | |"
Write-Host " _ |=-=| _"
Write-Host " _ / \| |/ \"
Write-Host " / \| | | ||\"
Write-Host "| | | | | \>"
Write-Host "| | | | | \"
Write-Host "| - - - - |) )"
Write-Host "| /"
Write-Host " \ /"
Write-Host " \ /"
Write-Host " \ /"
Write-Host " \ /"
Write-Host "F Windows Update Reboot!"
Write-Host "`n"
}
Write-Host "Press Enter to exit..."
$null = Read-Host
exit
}