views:

240

answers:

2

My mission is to press a keyboard sequence, such as Ctrl +Shift +R, to restart a Windows Service.

I have a script which works fine in the PowerShell ISE, when launched with administrative privileges.

When I try with a PowerShell script it fails due to insufficient Administrative Privileges. It’s galling that I can get it to work with an old-fashioned bat file, but not PowerShell.

The root of the problem is that shortcuts to a PowerShell script have their Administrative privileges box greyed out. So far no work-around has overcome this privilege problem.

Any ideas?

A: 

You suggest you don't like solving this problem with a batch file (e.g. net start), I think because batch files are inherently more limited than powershell scripts. What you can do is wrap your Ps script in a batch file, though, for the sake of accomplishing your stated objective -- running a powershell script with a keyboard shortcut without access permissions issues. Try this in a batch file:

 powershell set-executionpolicy remotesigned
 powershell myscript.ps1
John Sullivan
Thank you for your workaround, however if I must use a batch file then I may as well use commands such as: sc stop dnscache sleep 20000 sc start dnscache
Guy Thomas
Hmm, no problem. Wrapping your batch file in a PS would run faster than that and solve the problem you stated though.I guess I don't quite understand why you're trying to avoid batch files, aside from the fact that they are way more limited in what they can do than power shell. If I knew what the issue you have with them was I might have something else for you.
John Sullivan
+1  A: 

One approach is to start another elevated PowerShell session within your script like so:

Start-Process PowerShell.exe -arg '-nologo -noprofile script.ps1' -verb runas

That should prompt to elevate the new PowerShell session. I think you should be able to set the -WindowStyle parameter such that the new window doens't appear (if you need that behavior). Note that you will need to specify the full path to your existing script.

Keith Hill
The script works perfectly - in PowerShell, but how do I get the second PowerShell file, or its shortcut, to 'Run As Administrator'? For me that box is greyed out.Does anyone know why 'Run as Administrator' is available to PowerShell.exe on the Taskbar, but not on the desktop?
Guy Thomas
The idea is that you don't need to. The first PowerShell script is launched without admin and when it "starts" the second powershell process using -verb runas - that's when you will be prompted to elevate.
Keith Hill
At last I see - Eureka, it works. I have been guilty of over-think. Thanks for your help and for your persistence in getting me to see the light.
Guy Thomas