├── PSSudo.psd1 ├── PSSudo.psm1 └── README.md /PSSudo.psd1: -------------------------------------------------------------------------------- 1 | @{ 2 | RootModule = 'PSSudo.psm1' 3 | ModuleVersion = '1.4.0' 4 | GUID = '75640361-a312-4e57-8564-9c8b904d333b' 5 | Author = 'Eduardo Sousa' 6 | Description = 'Function for executing programs with adminstrative privileges' 7 | PowerShellVersion = '3.0' 8 | DotNetFrameworkVersion = '4.0' 9 | CLRVersion = '4.0' 10 | FunctionsToExport = @('Start-Elevated') 11 | AliasesToExport = @('sudo') 12 | HelpInfoURI = 'https://github.com/ecsousa/PSSudo' 13 | PrivateData = @{ 14 | Tags='sudo elevation' 15 | ProjectUri='https://github.com/ecsousa/PSSudo' 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /PSSudo.psm1: -------------------------------------------------------------------------------- 1 | function Start-Elevated { 2 | $psi = new-object System.Diagnostics.ProcessStartInfo 3 | 4 | $emuHk = $env:ConEmuHooks -eq 'Enabled' 5 | 6 | if($args.Length -eq 0) { 7 | if($emuHk) { 8 | $psi.FileName = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName 9 | $psi.Arguments = "-new_console:a -ExecutionPolicy $(Get-ExecutionPolicy) -NoLogo" 10 | $psi.UseShellExecute = $false 11 | } 12 | else { 13 | Write-Warning "You must provide a program to be executed with its command line arguments." 14 | return 15 | } 16 | } 17 | else { 18 | if($args.Length -ne 1) { 19 | $cmdLine = [string]::Join(' ', ($args[1..$args.Length] | % { '"' + (([string] $_).Replace('"', '""')) + '"' }) ) 20 | } 21 | else { 22 | $cmdLine = '' 23 | } 24 | 25 | $cmd = $args[0] 26 | 27 | if($cmd -is [ScriptBlock]) { 28 | $tempFile = [System.IO.FileInfo] [System.IO.Path]::GetTempFileName(); 29 | $scriptFile = Join-Path $tempFile.Directory ($tempFile.BaseName + '.ps1'); 30 | 31 | Set-Content $tempFile ([string] $cmd); 32 | 33 | mv $tempFile $scriptFile; 34 | 35 | $cmd = $scriptFile; 36 | 37 | $cmdLine = "$cmdLine; rm $scriptFile"; 38 | 39 | } 40 | 41 | else { 42 | 43 | $alias = Get-Alias $cmd -ErrorAction SilentlyContinue; 44 | while($alias) { 45 | $cmd = $alias.Definition; 46 | $alias = Get-Alias $cmd -ErrorAction SilentlyContinue; 47 | } 48 | } 49 | 50 | $cmd = Get-Command $cmd -ErrorAction SilentlyContinue 51 | 52 | switch -regex ($cmd.CommandType) { 53 | 'Application' { 54 | $program = $cmd.Source 55 | } 56 | 'Cmdlet|Function' { 57 | $program = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName 58 | 59 | $cmdLine = "$($cmd.Name) $cmdLine" 60 | $cmdLine = "-NoLogo -Command `"$cmdLine; pause`"" 61 | 62 | } 63 | 'ExternalScript' { 64 | $program = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName 65 | 66 | $cmdLine = "& '$($cmd.Source)' $cmdLine" 67 | $cmdLine = "-NoLogo -Command `"$cmdLine; pause`"" 68 | } 69 | default { 70 | Write-Warning "Command '$($args[0])' not found." 71 | return 72 | } 73 | } 74 | 75 | if($emuHk) { 76 | $psi.UseShellExecute = $false 77 | $cmdLine = "-new_console:a $cmdLine"; 78 | } 79 | else { 80 | $psi.Verb = "runas" 81 | } 82 | 83 | 84 | $psi.FileName = $program 85 | $psi.Arguments = $cmdLine 86 | } 87 | 88 | [System.Diagnostics.Process]::Start($psi) | out-null 89 | 90 | } 91 | 92 | Set-Alias sudo Start-Elevated 93 | 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSSudo 2 | 3 | **Note: I will no longer enhance this module. I recommed using 4 | [gsudo](https://github.com/gerardog/gsudo) instead, which provide a much better 5 | solution.** 6 | 7 | PowerShell module that provides `Start-Elevated` function: it will execute the 8 | provided command line with elevated privileges. This module is able to handle 9 | the following argument types: 10 | 11 | * Application 12 | * Cmdlet 13 | * Function 14 | * PowerShell Script 15 | * Script Block 16 | * Alias _(For Alias, PSSudo will try to resolve as one of the other types 17 | above)_ 18 | 19 | This function is aliased as `sudo` 20 | 21 | **Note**: When using Cmdlets, Functions or Scripts, PowerShell will resolve 22 | variable arguments before passing them to the Start-Elevated function. For most 23 | situations that would be OK. But there is a few exceptions. For instance, in 24 | the following command: 25 | 26 | `sudo Set-MpPreference -DisableRealtimeMonitoring $False` 27 | 28 | PowerShell would resolve `$False` to `'False'` before executing `Start-Elevated`. 29 | Then, the following block would be executed in the elevated PowerShell: 30 | 31 | `Set-MpPreference -DisableRealtimeMonitoring False` 32 | 33 | This will throw an error, because PowerShell is not able to implicitly convert 34 | the `'False'` string into a Boolean type. 35 | 36 | For such cases, you could execute: 37 | 38 | `sudo Set-MpPreference -DisableRealtimeMonitoring 0` _(As PowerShell converts 39 | zero into $False)_ 40 | 41 | Or use a Script Block: 42 | 43 | `sudo { Set-MpPreference -DisableRealtimeMonitoring $False }` 44 | 45 | 46 | ## Installing 47 | 48 | Windows 10 users: 49 | 50 | Install-Module PSSudo -Scope CurrentUser 51 | 52 | Otherwise, if you have [PsGet](http://psget.net/) installed: 53 | 54 | Install-Module PSSudo 55 | 56 | Or you can install it manually coping `PSSudo.psm1` to your modules folder (e.g. 57 | `$Env:USERPROFILE\Documents\WindowsPowerShell\Modules\PSSudo\`) 58 | --------------------------------------------------------------------------------