-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tips - Sample Self-Auto elevated script.pS1
48 lines (37 loc) · 1.86 KB
/
Tips - Sample Self-Auto elevated script.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
# Set ExecutionPolicy for current user (TEST)
# Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser
# Auto Elevate
Write-Output 'Checking for Elevated privileges for this process'
# Self-elevate the script if required
# Get the ID and security principal of the current user account
$MyWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$MyWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($MyWindowsID)
# Get the security principal for the Administrator role
$AdminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($MyWindowsPrincipal.IsInRole($AdminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
# $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
# $Host.UI.RawUI.BackgroundColor = "DarkBlue"
# Clear-Host
# Set execution policy to unrestricted
# Echo Write-Host "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser" | PowerShell.exe -noprofile
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$NewProcess = New-Object System.Diagnostics.ProcessStartInfo 'PowerShell'
# Specify the current script path and name as a parameter
$NewProcess.Arguments = $MyInvocation.MyCommand.Definition
# Indicate that the process should be elevated
$NewProcess.Verb = 'runas'
# Start the new process
[System.Diagnostics.Process]::Start($NewProcess)
# Exit from the current, unelevated, process
exit
}
# Sample script code (replace by your own)
Write-Output 'Hello PS is running in RunAsAdmin mode'
Pause # in this sample code, pause is just to avoir the close the shell after executing the code