I'm creating an application that with continuously attempt to run a list of programs until they are open. I'm having an issue when it comes to programs that require administrative priveleges however.
I've done some searching and found the below script (in createElevationScript()) that should run a program with administrative priveleges, however it doesn't start the program successfully.
Here's the relevant code:
public void tryStart()
{
if (!checkRunning())
{
try
{
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(path);
processBuilder.start();
}
catch (IOException e)
{
System.out.println("ForceStartUp: Insufficient privileges to run ProgramEntry with Name '" + name + "' at Path '" + path + "', attempting to start elevated");
tryElevated();
}
}
}
public void tryElevated()
{
if (!checkRunning())
{
try
{
elevationScript = createElevationScript();
String[] commandArray = new String[] {"cmd.exe", elevationScript.getAbsolutePath(), path};
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(commandArray);
processBuilder.start();
elevationScript.delete();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public File createElevationScript()
{
File elevatorScriptFile = new File(elevatorPath);
if (elevatorScriptFile.exists())
elevatorScriptFile.delete();
try
{
elevatorScriptFile.createNewFile();
elevatorScriptFile.deleteOnExit();
FileWriter fw = new FileWriter(elevatorScriptFile, false);
fw.write(
"@echo Set objShell = CreateObject(\"Shell.Application\") > %temp%\\sudo.tmp.vbs\r\n"
+ "@echo args = Right(\"%*\", (Len(\"%*\") - Len(\"%1\"))) >> %temp%\\sudo.tmp.vbs\r\n"
+ "@echo objShell.ShellExecute \"%1\", args, \"\", \"runas\" >> %temp%\\sudo.tmp.vbs\r\n"
+ "@cscript %temp%\\sudo.tmp.vbs\r\n"
+ "del /f %temp%\\sudo.tmp.vbs\r\n"
);
fw.flush();
} catch (IOException ignored) {}
return elevatorScriptFile;
}
Any idea's on how to get this working?