aveyo

windows_update_reboot_toggle

Mar 17th, 2018 (edited)
3,239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @(echo off% <#%) &color 07 &title Windows Update Reboot Toggle, AveYo 2018-2020
  2. set "0=%~f0" &set 1=%*& powershell -nop -c iex ([io.file]::ReadAllText($env:0)) &exit/b ||#>)[1]
  3.  
  4. $PS ={ $Main ={
  5.  $status='Windows Update Reboot task ENABLED!'
  6.  $updatetasks='HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\Tree\Microsoft\Windows\UpdateOrchestrator'
  7.  $reboot=@('Reboot','Reboot_AC','Reboot_Battery')
  8.  $reboot |% {if (gp "$updatetasks\$_" Id_OFF -ea 0) {$status='Windows Update Reboot task DISABLED!'}}
  9.  
  10. # Choice 6 = yes, 7 = no, 2 = cancel
  11.  if ($env:1 -ne 6 -and $env:1 -ne 7) {
  12.    Add-Type -As PresentationFramework;$choice=[System.Windows.MessageBox]::Show('Disable WU Reboot task?',$status,3,32);
  13.    if ($choice -eq 2) {exit} else {$c=[int]$choice; $env:1=$c}
  14.  }
  15.  
  16. # Elevate passing $choice as $env:1
  17.  $i="`r (\   /)`r( * . * )   UAC is useless below Always-Notify`r    ```r"
  18.  $u=0;$w=whoami /groups;if($w-like'*1-5-32-544*'){$u=1};if($w-like'*1-16-12288*'){$u=2};if($w-like'*1-16-16384*'){$u=3}
  19.  if ($u -lt 2) {start powershell -args "-nop -c & {`$i='$i';`$env:1=$c;`$PS={$($PS -replace'"','\"')};& `$PS}" -Verb RunAs;exit}
  20.  
  21. # Allow read $updatetasks registry key for Users (sid S-1-5-32-545) and set SYSTEM owner (sid S-1-5-18)
  22.  reg_own "$updatetasks","all","S-1-5-32-545","S-1-5-18","Allow","ReadPermissions, ReadKey"
  23. # Unlock $updatetasks registry key for Administrators (sid S-1-5-32-544) and set SYSTEM owner (sid S-1-5-18)
  24.  reg_own "$updatetasks","all","S-1-5-32-544","S-1-5-18","Allow","FullControl"
  25.  
  26. # Toggle
  27.  if ($env:1 -eq 6) {
  28.    $reboot |% {if (gp "$updatetasks\$_" Id -ea 0) {rnp "$updatetasks\$_" Id Id_OFF -Force -ea 0}}
  29.  } else {
  30.    $reboot |% {if (gp "$updatetasks\$_" Id_OFF -ea 0) {rnp "$updatetasks\$_" Id_OFF Id -Force -ea 0}}
  31.  }
  32.  
  33. timeout -1
  34.  exit
  35. }
  36.  
  37. function reg_own([string[]]$A){ #key [opt],all,usr,own,acc,perm  : reg_own "HKCU:\My","","S-1-5-32-545","","Allow","FullControl"
  38. $D1=[IO.IODescriptionAttribute].Module.GetType('System.Diagnostics.Process').GetMethods(42) |? {$_.Name -eq 'SetPrivilege'}
  39. 'SeSecurityPrivilege','SeTakeOwnershipPrivilege','SeBackupPrivilege','SeRestorePrivilege' |% {$D1.Invoke($null, @("$_",2))}
  40. $path=$A[0]; $rk=$path-split':\\',2; $HK=gi -lit Registry::$($rk[0]) -fo; $s=$A[1]; $sps=[Security.Principal.SecurityIdentifier]
  41. $u=($A[2],'S-1-5-32-544')[!$A[2]];$o=($A[3],$u)[!$A[3]];$w=$u,$o |% {new-object $sps($_)}; $old=!$A[3];$own=!$old; $y=$s-eq'all'
  42. $rar=new-object Security.AccessControl.RegistryAccessRule( $w[0], ($A[5],'FullControl')[!$A[5]], 1, 0, ($A[4],'Allow')[!$A[4]] )
  43. $x=$s-eq'none'; function Own1($k){$t=$HK.OpenSubKey($k,2,'TakeOwnership'); 0,4 |% {try{$o=$t.GetAccessControl($_)}catch{$old=0}}
  44. if($old){$own=1;$w[1]=$o.GetOwner($sps)};$o.SetOwner($w[0]);$t.SetAccessControl($o); $c=$HK.OpenSubKey($k,2,'ChangePermissions')
  45. $p=$c.GetAccessControl(2);if($y){$p.SetAccessRuleProtection(1,1)};$p.ResetAccessRule($rar);if($x){$p.RemoveAccessRuleAll($rar)}
  46. $c.SetAccessControl($p);if($own){$o.SetOwner($w[1]);$t.SetAccessControl($o)};if($s){$subkeys=$HK.OpenSubKey($k).GetSubKeyNames()
  47. foreach($n in $subkeys){Own1 "$k\$n"}}}; Own1 $rk[1]; if($env:VO){get-acl Registry::$path|fl}}
  48. #-_-# A lean and mean .ps1 snippet to set registry ownership and permission recursively by AveYo - pastebin.com/XTPt0JSC
  49.  
  50. & $Main }; & $PS
  51. #-_-# hybrid script, can be pasted directly into powershell console
  52.  
RAW Paste Data