Batch scripting with silent admin access to install applications - batch-file

Im having trouble with a script to silently install 2 files without getting an admin prompt. The first file changes an ini to a program can upgrade. The second installs the new application.
The script will then remove the old icon on the users desktop and replace it with a new link to the new application.
Every time I try to run it I get the dreaded UAC Prompt.
I tried to incorporate the batchgotadmin snippet at the top of my file but I cant seem to get it to work right as I still get a admin prompt and I need it to install the application silently with admin rights to the users machine.
Here is my code I'm working with now:
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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' (
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: Installs refresh
"\\server\share\program1.exe /VERYSILENT /LOG="c:\REFRESH.TXT"
:: Installs v8
"\\server\share\program2.exe" /VERYSILENT /LOG="C:\INSTALL.TXT" /NORESTART
:: Removes old applicationshortcut
del /Q "%allusersprofile%\desktop\program.lnk"
:: Adds new application shortcut
xcopy "c:\programfiles(x86)\program.exe" "%userprofile%\desktop"
What am I missing to make this work properly?

Related

Running a Batch with Admin Rights, but with pre saved User&Pass in the Batch to avoid windows asking for the Cridentials

I am already using the following Batch to open my Jar file with Admin Right:
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
-cp Jarfile.jar com.testApp
however after clicking on the Batch file, the windows will ask me to insert the Username and password.
The Question is , is there any way to give the user and pass inside the Batch file to open the Jar file but without windows asking for user and pass?

Store the current directory in a variable to be used later

I have a program that asks for admin privileges, stops some services, deletes a few files that couldn't be deleted if the services were running, restarts the services, and goes to a hard coded address where I have a vbs script I want to run next.
My problem is I'm about to put it on more computers, and I don't want to edit the hard coded address each time I set it up. I know that when you open a batch file, it starts at the directory where its located. I would like to save that in a variable so i can come back to it later to run the vbs script. Is there any way to do this?
Here is my code:
problem is at the bottom of the script
#echo off
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>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 )
#echo off
: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"
#echo off
net stop wuauserv
net stop bits
---save directory here--
cd /
cd Windows
echo To clean Windows Update, we need to wipe a folder. Wiping
rd /s SoftwareDistribution
mkdir SoftwareDistribution
echo The folder has been wiped.
pause
echo.
cd /
net start wuauserv
net start bits
---go to saved directory---
popup <-- VBS Script
pause
exit
A big thank you to #Compo for suggesting to me the use of ‘’’%~dp0’’’
It fixed pretty much everything.

How to create a service to run an application in admin mode or how can we configure an application to run in admin mode always using batch script?

I faced this problem, my task to delete some folder and some service and to change system environment variable automatically after some days of installation. So I created a .exe file which will do all above requirements. But I faced this problem for deleting service, changing environment variable we need administrator privilege.
My .exe file contains below codes
sc stop LaptopAudit
sc delete LaptopAudit
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V MYPROJECT
start cmd /k "#RD /S /Q %projectFolder% & exit"
my Service creation coding is here
nssm install LaptopAuditDestruct "%FolderPath%\selfdestr.exe"
I need to execute this selfdestr.exe file after some days automatically also in administrator mode. Is it Possible? Please Help me. Thank You. Also tried to create task scheduler there also same problem administrator mode required.
To run programs in admin using a batch-file you must have admin permissions on your script first, if it's the same as C++. Here's something I've been using that I found somewhere on this website. Put it at the begining of your script and tell me if it worked!
rem Checking permissions
if "%PROCESSOR_ARCHITECTURE%" == "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) else (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
rem If error then no admin permissions
if %errorlevel% NEQ 0 (
echo Catching admin permissions
timeout /t 2 /nobreak >nul
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
) else (
pushd "%CD%"
CD /D "%~dp0"
)

How to run a Command Prompt run as a Administrator using batch file

I want to run a jar file, jar file contents some external command so required it as run administrator, so I want it to run without using batch file with run as administrator
You can run using either right click on it and select run as administrator or following code content batch file code for run as administrator.
#echo off
:: BatchGotAdmin (Run as Admin code starts)
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"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
C:
test.jar

Running batch file using Shell with VB6 causes loop

I have a bat file that checks if UAC admin is required, if so, prompts UAC and then copies some dll files:
#echo off
:-------------------------------------
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"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
cd /d %~dp0
if /i "%PROCESSOR_IDENTIFIER:~0,3%"=="X86" (
echo system is x86
copy .\*.dll %windir%\system32\
regsvr32 /s /c %windir%\system32\filetoregister.dll
) else (
echo system is x64
copy .\*.dll %windir%\SysWOW64\
regsvr32 /s /c %windir%\SysWOW64\filetoregister.dll
)
This works fine when run from windows explorer, if UAC is required, it prompts etc, does exactly what I want, however, if I try to run the batch file from a VB6 programme thus:
Dim FilePath As String
FilePath = App.Path & "\setup\install.bat"
ShellVal = Shell(FilePath, vbNormalFocus)
It works fine if UAC not required but if it is, it asks for permission, after selecting an admin account it then goes into a loop of opening and closing the cmd window. Shutting down the computer is often the only way to kill it
I have tried just using the UAC part of the bat file, still same problem
Really stuck on this one, any help appreciated!
RESOLVED
I sorted this problem by removing the admin request from the batch file and using ShellExecute in the
VB6 programme to get admin rights (if required) and then run the batch file
Dim FilePath As String
FilePath = App.Path & "\setup\install.bat"
ShellExecute 0, "runas", FilePath, Command & "/admin", vbNullString, SW_SHOWNORMAL

Resources