Skip to content

Instantly share code, notes, and snippets.

@jkeiser
Last active August 29, 2015 14:27
Show Gist options
  • Save jkeiser/008631ca1ddf4d10488f to your computer and use it in GitHub Desktop.
Save jkeiser/008631ca1ddf4d10488f to your computer and use it in GitHub Desktop.
# 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
}
$erroractionpreference = 'stop'
$pantry_uri = "https://github.com/chef/pantry-chef-repo/archive/master.zip"
$dir = md -f $env:programdata\chef-pantry
$dst = "$dir\pantry"
$zip = "$dst.zip"
($zip,$dst) | rm -erroraction ignore -r -force
echo "Downloading $pantry_uri to $zip ..."
iwr $pantry_uri -outfile $zip
echo "Extracting to $dir ..."
$shl = New-Object -comobject Shell.Application
(mkdir $dst).fullname
$files = $shl.NameSpace("$zip\pantry-chef-repo-master").Items()
$shl.NameSpace($dst).CopyHere($files, 20)
Set-Location $dst
cat bin\pantry.ps1 | Out-String | Invoke-Expression
# Run your code that needs to be elevated here
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment