The Thing (jagaroth) wrote,
The Thing
jagaroth

admin access prompt for batch files

This script was written and tested for use under Windows 7 Home Basic, Windows 7 Enterprise and Windows Server 2008. It should work for Windows Vista too but I need feedback. It is written to not ask for permission under Windows XP but can be made to do so. If it does not work for you then I need to know what operating system you are using and maybe what service packs you have installed. There is a pre-made version of the test batch file here... http://sizterthing.com/misc/kaspbats/time_sync.bat

Take note that if you try to run the script directly from a web browser then the script will be blocked by the browser for security reasons. Probably for a good thing. The test file needs to be saved to a folder, You then need to open the folder and double click on it. I think I know a way around that but am yet to try it.

It actually took me ages to work out this otherwise simple concept out. I had been trying to get Windows BAT files to prompt for admin access under Windows 7 without adjusting settings or creating modified shortcuts. I could not find any info anywhere, just lots of forum posts stating that it could not be done or you needed to create a password first. I finally came up with a script to add to the top of batch files which simply prompts the user for permission to continue with admin access.

The following example evokes the cliche dialogue box asking the user permission to continue and then resyncs the time with online time servers which requires admin access on Vista and Win7. Idiot proofing simple batch scripts for users who have no idea what you are talking about when you ask them to "click the Right mouse button on the downloaded file and select 'Run as Admin'"...

@ECHO OFF

REM Changing working folder back to current directory
%~d0
CD %~dp0
REM Folder changed

REM Check first if Windows XP
for /f "tokens=3*" %%i IN ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName ^| Find "ProductName"') DO set vers=%%i %%j
echo %vers% | find "XP" > nul
if %ERRORLEVEL% == 0 goto ver_xp

REM Ask for admin access
if exist "admincheckOK.txt" goto adminOK1
del /Q admincheckOK.vbs
ECHO.
ECHO. Please wait...
echo.Set objShell = CreateObject("Shell.Application") > admincheckOK.vbs
echo.Set FSO = CreateObject("Scripting.FileSystemObject") >> admincheckOK.vbs
echo.strPath = FSO.GetParentFolderName (WScript.ScriptFullName) >> admincheckOK.vbs
echo.If FSO.FileExists(%0) Then >> admincheckOK.vbs
echo. Dim oShell >> admincheckOK.vbs
echo. Set oShell = WScript.CreateObject ("WScript.Shell") >> admincheckOK.vbs
echo. oShell.run "cmd.exe /c echo admincheckOK > admincheckOK.txt" >> admincheckOK.vbs
echo. Set oShell = Nothing >> admincheckOK.vbs
echo. objShell.ShellExecute "cmd.exe", " /c " ^& %0 ^& " ", "", "runas", 1 >> admincheckOK.vbs
echo.Else >> admincheckOK.vbs
echo. MsgBox "Script file not found" >> admincheckOK.vbs
echo.End If >> admincheckOK.vbs
cscript //B admincheckOK.vbs
goto timeend
:adminOK1
del /Q admincheckOK.txt
del /Q admincheckOK.vbs
:ver_xp
REM Admin Access allowed

net stop w32time

echo. Windows Registry Editor Version 5.00 > w32timeMAX.reg
echo. >> w32timeMAX.reg
echo. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Config] >> w32timeMAX.reg
echo. "MaxNegPhaseCorrection"=dword:ffffffff >> w32timeMAX.reg
echo. "MaxPosPhaseCorrection"=dword:ffffffff >> w32timeMAX.reg
echo. >> w32timeMAX.reg
regedit /s w32timeMAX.reg

net start w32time

w32tm /resync /rediscover
w32tm /resync /rediscover
w32tm /resync /rediscover
w32tm /resync /rediscover
w32tm /resync /rediscover

net stop w32time

echo. Windows Registry Editor Version 5.00 >> w32timeDEFAULT.reg
echo. >> w32timeDEFAULT.reg
echo. [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Config] >> w32timeDEFAULT.reg
echo. "MaxNegPhaseCorrection"=dword:0000d2f0 >> w32timeDEFAULT.reg
echo. "MaxPosPhaseCorrection"=dword:0000d2f0 >> w32timeDEFAULT.reg
echo. >> w32timeDEFAULT.reg
regedit /s w32timeDEFAULT.reg

echo.
echo.
echo. TIME HAS BEEN UPDATED.
echo.
del /Q w32timeMAX.reg
del /Q w32timeDEFAULT.reg
echo.
echo. !!!YOU MAY SHUT THIS WINDOW NOW!!!
echo.
pause

REM Following statement required if Admin access denied
:timeend
del /Q admincheckOK.vbs
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 28 comments