-
Notifications
You must be signed in to change notification settings - Fork 5
/
PSSudo.psm1
93 lines (70 loc) · 2.63 KB
/
PSSudo.psm1
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
function Start-Elevated {
$psi = new-object System.Diagnostics.ProcessStartInfo
$emuHk = $env:ConEmuHooks -eq 'Enabled'
if($args.Length -eq 0) {
if($emuHk) {
$psi.FileName = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
$psi.Arguments = "-new_console:a -ExecutionPolicy $(Get-ExecutionPolicy) -NoLogo"
$psi.UseShellExecute = $false
}
else {
Write-Warning "You must provide a program to be executed with its command line arguments."
return
}
}
else {
if($args.Length -ne 1) {
$cmdLine = [string]::Join(' ', ($args[1..$args.Length] | % { '"' + (([string] $_).Replace('"', '""')) + '"' }) )
}
else {
$cmdLine = ''
}
$cmd = $args[0]
if($cmd -is [ScriptBlock]) {
$tempFile = [System.IO.FileInfo] [System.IO.Path]::GetTempFileName();
$scriptFile = Join-Path $tempFile.Directory ($tempFile.BaseName + '.ps1');
Set-Content $tempFile ([string] $cmd);
mv $tempFile $scriptFile;
$cmd = $scriptFile;
$cmdLine = "$cmdLine; rm $scriptFile";
}
else {
$alias = Get-Alias $cmd -ErrorAction SilentlyContinue;
while($alias) {
$cmd = $alias.Definition;
$alias = Get-Alias $cmd -ErrorAction SilentlyContinue;
}
}
$cmd = Get-Command $cmd -ErrorAction SilentlyContinue
switch -regex ($cmd.CommandType) {
'Application' {
$program = $cmd.Source
}
'Cmdlet|Function' {
$program = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
$cmdLine = "$($cmd.Name) $cmdLine"
$cmdLine = "-NoLogo -Command `"$cmdLine; pause`""
}
'ExternalScript' {
$program = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
$cmdLine = "& '$($cmd.Source)' $cmdLine"
$cmdLine = "-NoLogo -Command `"$cmdLine; pause`""
}
default {
Write-Warning "Command '$($args[0])' not found."
return
}
}
if($emuHk) {
$psi.UseShellExecute = $false
$cmdLine = "-new_console:a $cmdLine";
}
else {
$psi.Verb = "runas"
}
$psi.FileName = $program
$psi.Arguments = $cmdLine
}
[System.Diagnostics.Process]::Start($psi) | out-null
}
Set-Alias sudo Start-Elevated