forked from SCUR0/PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisable-UpdateReboot.ps1
169 lines (149 loc) · 6.59 KB
/
Disable-UpdateReboot.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<#
.SYNOPSIS
Disable Windows update auto reboot
.DESCRIPTION
This script disables and sets permissions on registry key and task files to prevent the system
from re-enabling reboot task
.NOTES
Script will have to be run again after feature updates. This is because windows wipes the windows directory during it's update process.
This script was created by SCUR0
.LINK
https://github.com/SCUR0/PowerShell-Scripts
#>
[cmdletbinding()]
param ()
If (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).`
IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")){
Write-Error "Admin permissions are required to run this script. Please open powershell as administrator."
pause
break
}
#Variables
$Errors=$null
$RebootTaskError=$null
$RebootRegKey="SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree\Microsoft\Windows\UpdateOrchestrator\Reboot"
$RebootReg="Registry::HKEY_LOCAL_MACHINE\$RebootRegKey"
$RebootTask="$env:WinDir\System32\Tasks\Microsoft\Windows\UpdateOrchestrator\Reboot"
$RegAdminRule = New-Object System.Security.AccessControl.RegistryAccessRule ("BUILTIN\Administrators","FullControl","Allow")
$RegSystemRule = New-Object System.Security.AccessControl.RegistryAccessRule ("System","ReadKey","Allow")
$RegUserRule = New-Object System.Security.AccessControl.RegistryAccessRule ($env:USERNAME,"FullControl","Allow")
$FileAdminRule = New-Object System.Security.AccessControl.filesystemaccessrule ("BUILTIN\Administrators","FullControl","Allow")
$FileSystemRule = New-Object System.Security.AccessControl.filesystemaccessrule ("System","ReadAndExecute","Allow")
$FileUserRule = New-Object System.Security.AccessControl.filesystemaccessrule ($env:USERNAME,"FullControl","Allow")
$owner = [Security.Principal.NTAccount]$env:USERNAME
Function Enable-Privilege {
param($Privilege)
$Definition = @'
using System;
using System.Runtime.InteropServices;
public class AdjPriv {
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr rele);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name,
ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid {
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
public static bool EnablePrivilege(long processHandle, string privilege) {
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = new IntPtr(processHandle);
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero,
IntPtr.Zero);
return retVal;
}
}
'@
$ProcessHandle = (Get-Process -id $pid).Handle
$type = Add-Type $definition -PassThru
$type[0]::EnablePrivilege($processHandle, $Privilege)
}
#verify task is created
If (!(test-path $RebootTask)){
Write-Output "Reboot task has to first be created by windows update in order to disable. Please run script after first cumulative update."
}else{
do {} until (Enable-Privilege SeTakeOwnershipPrivilege)
#SET ACL for registry key
#Set ownership
Write-Verbose "Modifying registry keys." -Verbose
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($RebootRegKey,`
[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::TakeOwnership)
$RegACL = $key.GetAccessControl()
if ($null -eq $RegACL){
Write-Warning "Error encountered while trying to modify registry for reboot"
$Errors=$true
}else{
$RegACL.SetOwner($owner)
$key.SetAccessControl($RegACL)
#Modify permissions
$RegACL = $key.GetAccessControl()
$RegACL.ResetAccessRule($RegAdminRule)
$RegACL.SetAccessRule($RegSystemRule)
$RegACL.SetAccessRule($RegUserRule)
$key.SetAccessControl($RegACL)
#remove inheritance
$RegACL = Get-Acl -Path $RebootReg
$RegACL.SetAccessRuleProtection($true,$false)
$RegACL | Set-Acl
}
#SET ACL for task file
#Change owner
Write-Verbose "Modifying scheduled task files." -Verbose
$FileACL = Get-ACL -Path $RebootTask -ErrorAction SilentlyContinue
if ($null -eq $FileACL){
Write-Warning "Error encountered while trying to modify task file for reboot"
$Errors=$true
}else{
$FileACL.SetOwner($owner)
Set-Acl -Path $RebootTask -AclObject $FileACL
#remove inheritance
$FileACL = Get-Acl -Path $RebootTask
$FileACL.SetAccessRuleProtection($true,$false)
$FileACL | Set-Acl -ErrorAction Stop
#remove and set permissions
$FileACL = Get-ACL -Path $RebootTask
$FileACL.Access | %{$FileACL.RemoveAccessRule($_)} |Out-Null
$FileACL.SetAccessRule($FileAdminRule)
$FileACL.SetAccessRule($FileSystemRule)
$FileACL.SetAccessRule($FileUserRule)
Set-Acl -Path $RebootTask -AclObject $FileACL
}
if (!$Errors){
#attempt to set task to disabled
Write-Verbose "Attempting to set task to disabled via task scheduler." -Verbose
try{
Get-ScheduledTask Reboot -ErrorAction Stop | Disable-ScheduledTask -ErrorAction Stop | Out-Null
}catch{
$RebootTaskError=$true
}
if (!$RebootTaskError){
#remove admin permissions
Write-Verbose "Restricting security permisions on registry and task file." -Verbose
$RegACL.RemoveAccessRule($RegAdminRule) | Out-Null
$key.SetAccessControl($RegACL)
$FileACL.RemoveAccessRule($FileAdminRule) | Out-Null
Set-Acl -Path $RebootTask -AclObject $FileACL
Write-Verbose "Script complete." -Verbose
Write-Warning "Script will need to be run again after a feature (new windows build) update."
}
}else{
Write-Output "Errors were encountered while attempting to make changes. The script was not successful."
}
}