Manually running a .ps1 in an administrator (elevated) shell

I have a seemingly straightforward question: How can I manually run a .ps1 script on Server 2012 R2 and have it open in an administrator elevated shell? I am right clicking and clicking "Run with Powershell" on the .ps1 file.

My environment:

Two Server 2012 R2 machines in the same domain in the same OU. Both are full GUI installs. Both have UAC set to "default".

The discrepancy:

One of the servers will run any and all .ps1 files in an administrator elevated shell. The other server will run any and all .ps1 files in a non-administrator, standard shell. I have no idea what the differences are between the two servers. Neither are running any custom Powershell profiles.

The following registry keys are all identical between the two servers:

HKEY_CLASSES_ROOT\Microsoft.PowerShellCmdletDefinitionXML.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellConsole.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellData.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellSessionConfiguration.1

HKEY_CLASSES_ROOT\Microsoft.PowerShellXMLData.1

What am I missing?

Answers


A quick foray with Google ended me up with a posting on Ben Armstrong's blog where he posted code that auto-elevated a script if it was needed. Here's the code he posted that seems perfect for your needs:

# Get the ID and security principal of the current user account
 $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
 $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)

 # Get the security principal for the Administrator role
 $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator

 # Check to see if we are currently running "as Administrator"
 if ($myWindowsPrincipal.IsInRole($adminRole))
    {
    # We are running "as Administrator" - so change the title and background color to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
    $Host.UI.RawUI.BackgroundColor = "DarkBlue"
    clear-host
    }
 else
    {
    # We are not running "as Administrator" - so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter
    $newProcess.Arguments = $myInvocation.MyCommand.Definition;

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    exit
    }

 # Run your code that needs to be elevated here
 Write-Host -NoNewLine "Press any key to continue..."
 $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Need Your Help

Code runs well on Linux but gives a different output on Windows OS

c++ windows linux console mingw

I have been wondering why the code below is giving me different output on Windows OS. Meanwhile, the same code works well on Ubuntu.

Best way to display pdf document in asp.net application

c# asp.net pdf webforms ghostscript

I'm looking for the best way to display pdf document on a website. Surely I need to convert it to jpeg or gif for the browser to handle it. I read few posts but most refer to GhostScript and its