Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the UAC prompt in PowerShell if the action requires elevation

Tags:

powershell

uac

I have a simple PowerShell script to stop a process:

$p = get-process $args
if ( $p -ne $null )
{
$p | stop-process
$p | select ProcessName, ID, HasExited, CPU, Handles
}
else { "No such process" }

If I try to stop a process not started by the current user; it works on Windows Server 2003. However, on Windows Server 2008 (and other Windows flavours with User Account Control), I get the following error:

Stop-Process : Cannot stop process "w3wp (5312)" because of the following error: Access is denied

Is there any way to get around this without running PowerShell with elevated privileges ? It would be OK if the user was just presented with the UAC prompt, whenever he tries to execute an action, that requires elevation.

like image 297
driis Avatar asked Oct 14 '09 15:10

driis


People also ask

How do I open an elevated command prompt in PowerShell?

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.

How do I open PowerShell with elevated privileges?

To run the PowerShell window with elevated permissions just click Start then type PowerShell then Right-Click on PowerShell icon and select Run as Administrator. Shortcut: Start. Type PowerShell.

How do I elevate PowerShell to administrator without prompt?

It is possible to right click Powershell.exe (or it's Start menu shortcut) and run it 'As Admin'. Shortcuts can be edited to always run as Admin - Properties | Shortcut | Advanced then tick "Run as administrator".

How do I elevate a PowerShell script?

To run PowerShell as an administrator in the command line: Type : PowerShell to enter into PowerShell console. Now, Type: Start-Process PowerShell -Verb RunAs.


2 Answers

AFAIK, there is no way to do it in the sense that you seem to want. That is running a specified .exe and expecting a prompt to appear immediately.

What I do is for commands that I know have to be run with administrative privs, I run them with a functions I have laying around called Invoke-Admin. It ensures that I'm running as admin and will prompt the user with the UAC dialog if i'm not before running the command.

Here it is

function Invoke-Admin() {
    param ( [string]$program = $(throw "Please specify a program" ),
            [string]$argumentString = "",
            [switch]$waitForExit )

    $psi = new-object "Diagnostics.ProcessStartInfo"
    $psi.FileName = $program 
    $psi.Arguments = $argumentString
    $psi.Verb = "runas"
    $proc = [Diagnostics.Process]::Start($psi)
    if ( $waitForExit ) {
        $proc.WaitForExit();
    }
}
like image 82
JaredPar Avatar answered Oct 15 '22 18:10

JaredPar


First install PowerShell Community Extensions choco install pscx via Chocolatey (you may have to restart your shell environment)

then enable pscx

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser #allows scripts to run from the interwebs, such as pcsx

Then use Invoke-Elevated, for example

Invoke-Elevated {Add-PathVariable $args[0] -Target Machine} -ArgumentList $MY_NEW_DIR
like image 28
Jonathan Avatar answered Oct 15 '22 18:10

Jonathan