-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStart-Elevated.ps1
More file actions
95 lines (69 loc) · 2.81 KB
/
Start-Elevated.ps1
File metadata and controls
95 lines (69 loc) · 2.81 KB
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
if($emuHk) {
#We are inside ConEmu, with ConEmuHk enabled!
function Start-Elevated {
if($args.Length -eq 0) {
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = $env:WINDIR + '\System32\WindowsPowerShell\v1.0\powershell.exe'
$psi.Arguments = '-new_console:a -ExecutionPolicy ' + (Get-ExecutionPolicy) + ' -NoExit -Command "Import-Module ''' + $PSScriptRoot + '''"'
$psi.UseShellExecute = $false
}
else {
if( ($args[0].ToLower() -eq 'gvim') -or ($args[0].ToLower() -eq 'vim')) {
if($args[0].ToLower() -eq 'vim') {
$cmd = 'Start-Vim'
}
else {
$cmd = 'Start-gVim'
}
$newArgs = $args[1..$args.Length];
$newArgs = & { '-new_console:a'; $newArgs }
& ($cmd) $newArgs
return;
}
$program = $args[0]
$alias = Get-Alias $program -ErrorAction SilentlyContinue
while($alias) {
$program = $alias.Definition;
$alias = Get-Alias $program -ErrorAction SilentlyContinue
}
$cmdLine = '-new_console:a ';
if($args.Length -ne 1) {
$cmdLine = $cmdLine + [string]::Join(' ', ($args[1..$args.Length] | % { '"' + (([string] $_).Replace('"', '""')) + '"' }) )
}
$fullProgram = which $program | select-object -First 1
if($fullProgram) {
$program = $fullProgram
}
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = $program
$psi.Arguments = $cmdLine
$psi.UseShellExecute = $false
}
[System.Diagnostics.Process]::Start($psi) | out-null
}
}
else {
function Start-Elevated {
if($args.Length -eq 0) {
Write-Warning "You must provived to program to be executed and its command line arguments"
return
}
$program = $args[0]
$alias = Get-Alias $program -ErrorAction SilentlyContinue
while($alias) {
$program = $alias.Definition;
$alias = Get-Alias $program -ErrorAction SilentlyContinue
}
if($args.Length -eq 1) {
$cmdLine = '';
}
else {
$cmdLine = [string]::Join(' ', ($args[1..$args.Length] | % { '"' + (([string] $_).Replace('"', '""')) + '"' }) )
}
$psi = new-object System.Diagnostics.ProcessStartInfo
$psi.FileName = $program
$psi.Arguments = $cmdLine
$psi.Verb = "runas"
[System.Diagnostics.Process]::Start($psi) | out-null
}
}