0

VB scripting is completely alien to me but today landed in a situation to write a small one. I need Admin rights to run my .bat file. So I am trying to elevate to Admin rights if not have them. With the help of SO and Google I reached upto:

Function Length()

Set WshShell = WScript.CreateObject("WScript.Shell") 
If WScript.Arguments.length = 0 Then 
    Set ObjShell = CreateObject("Shell.Application") 
    ObjShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " RunAsAdministrator", , "runas", 1 
Else
    Dim shell
    set shell=createobject("wscript.shell")
    shell.run "ExtractFiles.bat"
End If

End Function

Length

Here, this .vbs and ExtractFiles.bat are saved in same folder. I opened 2 command prompts. One in Admin mode and other normal. When running this script thorugh command prompt in Admin mode, I am getting success. But in normal mode, first I get a window to switch to Admin mode and I press Yes on it. Then I get below error:

enter image description here

Can anyone point me to correct code. I am getting error in line shell.run "ExtractFiles.bat". Please help!

As I have also mentioned the requirement, a different approach is also welcome. In this problem, I am not sure how I am able to run the bat file in admin mode and failing in normal mode.

Sandy
  • 11,332
  • 27
  • 76
  • 122

3 Answers3

0

Add the Admin VBS code into your bat file. Here is my routine for the job.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if Needed 'true'.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set "xVBUAC=%Temp%\AdminUAC.vbs"
set "xSuccess=true"
set "xAdmin=false"
if not "%~3"=="" set "xSuccess=%~3"

:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set "xAdmin=%xSuccess%"

:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
    echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
    if exist "%xVBUAC%" (
        "%xVBUAC%"
        rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
        del "%xVBUAC%"
    )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof

How to Use it

:: Example Admin check

@echo off
setlocal EnableExtensions

call :Admin xReturn true 1
if not "%xReturn%"=="1" goto End

:: Do my .bat stuff here.

goto End

:: TODO Place the admin function here.

:End
David Ruhmann
  • 11,064
  • 4
  • 37
  • 47
0

Depending on how you launch the VBScript the directory in which the scripts reside isn't necessarily the working directory. Try this:

Set fso = CreateObject("Scripting.FileSystemObject")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
shell.run "%COMSPEC% /c """ & fso.BuildPath(scriptDir, "ExtractFiles.bat") & """"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

What you enter in the command prompt? Is it...

InstallACS.vbs ExtractFiles.bat

Your script works just fine on XP x64 (if that important) never mind how I'll run it - from the sell or from the console, and also work with and without argument.

Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28