Search code examples
powershellelevated-privileges

Running A PowerShell Script At A UNC Path With Admin Privilege From Shortcut


I have created a PowerShell script to install some Features On Demand for our system administrators throughout our company. I have created a shortcut for the script and selected "Run As Administrator" in the advanced options. When I run the script directly it runs but errors state:

The requested operation requires elevation

If I run the script from the shortcut, UAC pops up to allow to run as administrator of which I hit Yes and then a Black CMD box pops up and then immediately closes. It doesn't display any part of my script and doesn't pause at the end like I have in the script. In the Target of the shortcut I have put:

powershell.exe -ExecutionPolicy Bypass

Still has the same problem.

If I put the powershell.exe -ExecutionPolicy Bypass into the script it opens an Admin Powershell window on top of the original window but the script runs in the lower window. I can only assume that it launches the admin window back at C:\Windows\System32

Is there a way to run a command to start PowerShell as admin and go directly to my script on a UNC path?

Something link this:

Start-Process Powershell.exe -Verb runAs -File "\SERVER\Scripts\Script.ps1"

Or is there a better way to get my shortcut to launch my scripts as an administrator to the script on the UNC path?


Solution

  • After a bunch of research I found the issue. Turns out I was correct in thinking the Administrative box was starting at C:\Windows\System32 and there was no parameter passing to the box to tell it what to write. After some trial and error this is what I got to work and has done exactly what I wanted:

    In the shortcut I removed the "Run As Admnistrator" option under Advanced. I added some values to the Target of the shortcut:

    powershell -ExecutionPolicy Bypass "\\UNC\To\Script.ps1"
    

    This started a launch script which ran in user mode. To get the administrative functions done by the script I moved those command to a different script "AdminScript.ps1" and called to it from Script.pst1 like so:

    Start-Process powershell -Argument "-ExecutionPolicy Bypass -noexit \\UNC\Path\to\AdminScript.ps1" -Verb runAs
    

    This then launched the Administrative Powershell window and set the ExecutionPolicy to Bypass since it starts default as Restricted.

    From there I was able to complete the the installation of the FoD with administrative privileges.