sudo for Powershell

25 May 2010

Today I discovered I had written a script that required admin rights (restarting a Windows service). I began looking into options for the Linux equivalent of sudo and came upon these two pages: Link 1, Link 2. Both describe simplified ways of trying to launch an app from an elevated account (notepad, powershell, etc). However, there were a few things missing. First, if I want to run an elevated powershell script I had to run

sudo powershell

, then once in the elevated prompt, cd to the directory and run the script

.\services.ps1

This obviously isn’t ideal – and the one source had a few mistakes with how the actual passed file was called (it didn’t deal well with the “.\” portion of a passed script.

I made some changes, added comments, and updated to allow for powershell scripts to be run with a simple flag “-ps”

Here’s the code:

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
## sudo.ps1
#
# Authors: pezhore, mrigns, This guy: http://tsukasa.jidder.de/blog/2008/03/15/scripting-sudo-with-powershell,
#             other powershell peoples
#
# Sources:
#       http://tsukasa.jidder.de/blog/2008/03/15/scripting-sudo-with-powershell
#       http://www.ainotenshi.org/%E2%80%98sudo%E2%80%99-for-powershell-sorta
#
# Version:
#       1.0     Initial version
#       1.1     added -ps flag, cleaned up passed $file/$script full path
#       1.2     Comments
#       1.3     Fixed passing working directory to powershell/auto closing
 
param(
        [switch]$ps,               # Switch for running args as powershell script
        [string]$file,             # Script/Program to run
        [string]$arguments = $args # Arguments to program/script
     )
 
# Find our powershell full path
$powershell = (get-command powershell).definition
 
# Get current directory
$dir = get-location
 
#If we're running this as a elevated powershell script
if ($ps){
 
        # Script verification
        if([System.IO.File]::Exists("$(get-location)\$file")) {
 
                # Set the $script to full path of the ps script
                $script = (get-childitem $file).fullname
        }
 
        # Create a powershell process
        $psi = new-object System.Diagnostics.ProcessStartInfo $powershell
 
        $psi.WorkingDirectory = Get-Location
 
        # Combine the script and its arguments
        $sArgs = $script + " " + $arguments
 
        # Set the arguments to be the ps script and it's arguments
        $psi.Arguments = "-noexit -command set-location $dir; $sArgs"
 
        # Magic to run as elevated
        $psi.Verb = "runas";
}
 
# We're running something other than a powershells script
else {
 
        # File verification
        if([System.IO.File]::Exists("$(get-location)\$file")) {
 
                # Get full path
                $file = (get-childitem $file).fullname
        }
 
        # Same as above, create proccess/working directory/arguments/runas
        $psi = new-object System.Diagnostics.ProcessStartInfo $file
        $psi.Arguments = $arguments
        $psi.Verb = "runas"
}
 
# Start the process
[System.Diagnostics.Process]::Start($psi)

I added an alias in my profile and sudo was born:

1
New-Alias -name sudo 'd:\git-code\Powershell\ps_misc\sudo.ps1'
Comments (0) Trackback Leave a comment
  1. No comments yet.
Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
  1. No trackbacks yet.