PowerShell Function: Elevate-Process (like SUDO for PowerShell)

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Grant-ElevatedProcess
    ##  PURPOSE........:  Runs commands with an elevated security context when 
    ##                    UAC is enabled.
    ##  REQUIREMENTS...:  PowerShell v2
    ##  NOTES..........:  
    ##--------------------------------------------------------------------------
    Function Grant-ElevatedProcess {
        <#
        .SYNOPSIS
         Runs commands with an elevated security context when UAC is enabled.
        .DESCRIPTION
         Full description of function
        .PARAMETER Arguments
         The command to run elevated. Anything typed after the function name 
         will be processed.

        .EXAMPLE
         C:\PS>Grant-ElevatedProcess ipconfig /flushdns
         
         This will prompt for elevation, and then run a command that will clear 
         the DNS resolver cache on the local machine.
         
        .NOTES
         NAME......:  Grant-ElevatedProcess
         AUTHOR....:  Joe Glessner
         LAST EDIT.:  10FEB12
         CREATED...:  11APR11
         
         Based on code posted by weestro at http://weestro.blogspot.com/
        .LINK
         https://joeit.wordpress.com/
        .LINK
         http://weestro.blogspot.com/2009/08/sudo-for-powershell.html
        #>
        $File, [String]$Arguments = $Args;
        $Process = new-object System.Diagnostics.ProcessStartInfo $File;
        $Process.Arguments = $Arguments;
        $Process.Verb = "RunAs";
        $Process.WorkingDirectory = Get-Location;
        [System.Diagnostics.Process]::Start($Process);
    }#END: Function Grant-ElevatedProcess
    Set-Alias -name sudo -value Grant-ElevatedProcess

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: