0

I've created the following script, which has the purpose to install & configure a Windows Service. When called, Windows brings up the UAC window which must be acknowledged by the user to continue the process.

@echo off
CALL "%~dp0.\installAnchorService.exe"
sc config "FLEXnet Licensing Service" start= auto

The problem is, that normally the batch file isn't started in administrator mode. I already found a solution for that here: How to request Administrator access inside a batch file.

This works fine when i start the bat-file directly with double-click. When i start it via Java, the script falls into an endless loop. Somehow the VBS creation fails. Do I have to call my batch script in a special way that it's executed correctly in java? My approach was:

ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "start", "createService.bat");
builder.start();

Any thoughts/hints are appreciated...

Full script:

@echo off

:: BatchGotAdmin
:-------------------------------------
REM Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
:--------------------------------------

CALL "%~dp0.\installAnchorService.exe"
sc config "FLEXnet Licensing Service" start= auto
Community
  • 1
  • 1
Teazl
  • 233
  • 3
  • 12
  • Falls into an endless loop: How do you determine that? Surely there's nothing in that bat file that would do that, so how? Is the loop in your Java program? – laune Jul 23 '14 at 10:00
  • the cmd window opens itself and closes very fast. the only readable thing is **Requesting administrative privileges...**. i have to reboot my computer to make it stop... – Teazl Jul 23 '14 at 10:21
  • Since the exact form of the command has been established, I see no reason to retain my answer which was based of incorrect premises. - Redirecting standard output and standard error and obtaining the return code (from Process.waitFor()) might shed some more light on what fails. – laune Jul 23 '14 at 11:18
  • Due to that infinite loop, i don't get any response. It seems I'm not able to get any result back so i have to figure out why the script doesn't work as it should. i added the full script for clarification reasons. – Teazl Jul 23 '14 at 11:28

1 Answers1

0

I've experimented a lot and found a solution, finally!

First I created a standalone VBscript:

Set args = WScript.Arguments
file = args.Item(0)
Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute "cmd.exe", "/c " & file, "", "runas", 1

As you can see, i pass the batch file to start as parameter. The call looks like the following:

String[] scriptCmd = {"cscript", pathToVbscript, pathToBatchfile}
ProcessBuilder builder = new ProcessBuilder(scriptCmd);
builder.start();

I ignored the part where the script determines if I already have the correct permission or not. Normally the system where my script should run onto doesn't have admin rights per default. So this is my way to go. May it help others too... :)

Teazl
  • 233
  • 3
  • 12