Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script to restart a service

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?

like image 875
Guy Thomas Avatar asked Nov 05 '22 13:11

Guy Thomas


1 Answers

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.

like image 63
Keith Hill Avatar answered Nov 15 '22 06:11

Keith Hill