فهرست منبع

Added code to have script self elevate to administrator

I added code that checks if the script was run as an Administrator, and if not then it self elevates with a UAC prompt asking if it can be run as an Administrator. The script works best when run as an Adminstrator.
Richard Newton 6 سال پیش
والد
کامیت
5f726f50dc
1فایلهای تغییر یافته به همراه8 افزوده شده و 0 حذف شده
  1. 8 0
      Windows10DebloaterGUI.ps1

+ 8 - 0
Windows10DebloaterGUI.ps1

@@ -1,3 +1,11 @@
+If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
+    $arguments = "&" + $MyInvocation.MyCommand.Definition + "" 
+    Write-Host "You didn't run this script as an Administrator. This script will self elevate to run as an Administrator." -ForegroundColor "White"
+    Start-Sleep 1
+    Start-Process "powershell.exe" -Verb RunAs -ArgumentList $arguments
+    Break
+}
+
 Add-Type -AssemblyName System.Windows.Forms
 [System.Windows.Forms.Application]::EnableVisualStyles()