-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunAsAdmin.ps1
35 lines (30 loc) · 1.35 KB
/
RunAsAdmin.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
function RunAsAdmin {
param (
[string]$ScriptToRun = $null,
[string[]]$ScriptArgs = @()
)
# Check if the script is running with administrative privileges
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
if ($ScriptToRun) {
Write-Host "Restarting $ScriptToRun with administrative privileges using 'runas'..." -ForegroundColor Yellow
$Command = "powershell.exe -ExecutionPolicy Bypass -File `"$ScriptToRun`" $($ScriptArgs -join ' ')"
Start-Process -FilePath "powershell.exe" -ArgumentList "-Command $Command" -Verb RunAs
} else {
Write-Host "No script specified. Exiting..." -ForegroundColor Red
}
exit
}
Write-Host "Script is running with administrative privileges." -ForegroundColor Green
# If a script is provided, execute it
if ($ScriptToRun) {
Write-Host "Executing $ScriptToRun..." -ForegroundColor Cyan
& $ScriptToRun @ScriptArgs
} else {
Write-Host "No script provided to execute." -ForegroundColor Yellow
}
}
# Check if alias 'raa' exists and remove it if it does
if (Test-Path Alias:\raa) {
Write-Host "Removing existing alias 'raa'..." -ForegroundColor Yellow
Remove-Item Alias:\raa
}