Skip to content

Instantly share code, notes, and snippets.

@oogles
Last active December 12, 2015 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oogles/a6de0462cfa755013a90 to your computer and use it in GitHub Desktop.
Save oogles/a6de0462cfa755013a90 to your computer and use it in GitHub Desktop.
Script that opens a Windows command prompt, requesting Administrator privileges from UAC. Designed for working with Vagrant/VirtualBox on Windows, where the "vagrant up" command needs Administrator privileges if symlinks need to be created in the synced folder.
@echo off
:: =============================================================================
:: When using Vagrant with VirtualBox under Windows, with VirtualBox shared
:: folders, creating symlinks in the synced folder requires "vagrant up" be run
:: with Administrator privileges.
:: dev.cmd is a script designed to open a command prompt with Administrator
:: privileges, ready for running Vagrant commands.
:: It accepts the full path to a Vagrant project directory and does the following:
:: 1. Uses UAC to request Administrator privileges.
:: 2. Changes to the project directory.
:: 3. Checks for the presence of a Vagrantfile. If found, it runs "vagrant status".
:: 4. Leaves the command prompt open for interaction.
::
:: It is designed to work with project-specific script files that simply call it
:: with the relevant path to that project.
:: E.g.
::
:: @C:\path\to\dev.cmd "C:\path\to\my-project"
:: =============================================================================
:: Use UAC to elevate privileges.
:: Adapted from http://stackoverflow.com/a/12264592
:: -----------------------------------------------------------------------------
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift /1 & goto gotPrivileges)
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
echo args = "ELEV " >> "%temp%\OEgetPrivileges.vbs"
echo For Each strArg in WScript.Arguments >> "%temp%\OEgetPrivileges.vbs"
echo args = args ^& strArg ^& " " >> "%temp%\OEgetPrivileges.vbs"
echo Next >> "%temp%\OEgetPrivileges.vbs"
echo UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%SystemRoot%\System32\WScript.exe" "%temp%\OEgetPrivileges.vbs" %*
exit /B
:gotPrivileges
if '%1'=='ELEV' shift /1
setlocal & pushd .
cd /d %~dp0
:: -----------------------------------------------------------------------------
:: Open given project directory
cd /d %1
:: If a vagrant project, run "vagrant status"
if exist "Vagrantfile" (
echo This project appears to use Vagrant.
echo Running vagrant status...
echo.
vagrant status
)
echo on
:: Allow interaction with the shell
@cmd /k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment