Cannot run a batch code as admin privilege - windows

I am using a batch code in removal Disk to install some software (from removal Disk).
My Problem is I need to run this batch code as administrator , but when I ran it as administrator it ran from C:\Windows\system32 but my installer path is different.
This is a sample script :
Setup1.exe /S
Setup2.exe /S
Now how can I change the directory to installer path after run as admin

As per my comment:
You can either add the following to the top of your batch script:
#CD /D "%~dp0"
…or prefix your setup file like this:
"%~dp0setup.exe" /S

Related

How can I create a windows 10 script which runs a command under specified path?

I created a npmruns.bat file with a content:
C:\myfolder>npm run s
I wanted to create a script file which run a command: npm run s under specified location: C:\myfolder , but it doesn't work. I run it by double click (I have an admin rights).
I tried to create a script which can be executed from any location (other than C:\myfolder).
pushd and cd /d will be the options for this:
#echo off
pushd "C:\myfolder"
call npm.cmd run s
popd
This will push to the path of the batch-file as the working directory, popd is not required if you do not need to go back to the starting working directory, which as admin, will be "%systemroot%\system32"
Alternatively you can run:
cd /d "c:\myfolder"

How to run a .bat (that runs an .exe that is in the same directory) as administrator from a pendrive?

I'm able to run a .bat (that runs an .exe that is in the same directory) as administrator: I right-click in the bat file and select "Run as administrator".
To be able to do that, I used the following answer: Run exe from current directory in batch
Here's the code:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
start %1myprogram.exe
exit
However, this will only work if the .bat file and the program are in the system drive.
Because if they are, for instance, in a pendrive, and I right-click and select "Run as Administrator", I get the error:
"Windows cannot find 'myprogram.exe'. Make sure you've typed the name correctly, then try again."
Why this happens and how can I fix it?
I thought that by using cd %~dp0 it would always point to the folder in which the bat .file resides.
Thanks in advance.
Solution
Change cd %~dp0 to cd /d %~dp0
Explanation
When you run something with administrator privileges, the working directory changes to:
'C:\Windows\System32'
Although %~dp0 still points to the drive and the directory containing the batch file, cd %~dp0 does not work, because it only changes the directory, but stays on the same drive.
Using the /d parameter, you can tell the cd-command to change the drive, too.
You may need to tell cd to also change drives:
cd /d %~dp0
If the current drive is C: (e.g., the prompt says C:\>), and you do CD D:\FOO, the current directory on drive D: is set to \FOO, but you will still be "on" drive C:. Try the following:
#echo off
:A
cls
echo This will start the program.
pause
cd %~dp0
%~d0
start %1myprogram.exe
exit
(also, why %1myprogram.exe instead of just myprogram.exe, or even just myprogram? If you're right-clicking on the batch file to run it, there isn't going to be a %1.)

the installation package could not be open batch file

I've been working on a batch file all day, that I can't get to work open through GPO (another day, another question). So I decided to do it manually with every computer. I have two exe's and one MSI. The exe's work perfectly fine. They get installed, and it all works out. The MSI, however, doesn't. It gives me the error: the installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.
Now when I go to the network share and use it from there, it works perfectly fine. So there must be an issue with my code.
Here's the code:
#echo off
IF NOT EXIST "C:\Program Files (x86)\Citrix\ICA Client\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\1\"
.\CitrixReceiver-4.4.1000.exe /silent
)
IF NOT EXIST "C:\Program Files (x86)\triCerat\Simplify Printing\ScrewDrivers Client v4\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\"
msiexec.exe /i ".\Screwdriver.msi"
)
IF NOT EXIST "C:\Program Files\Cloudwerx\CloudwerxPlugin\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\3\"
.\cloudwerx-setup.exe /silent
)
pause
Any help would be greatly appreciated, thanks.
I am guessing that your problem is the distinction in powershell between the current location (set by the pushd command) and the working directory (unaffected by the pushd command). You can see the working directory of the powershell process using the [Environment]::CurrentDirectory property:
# C:\> [Environment]::CurrentDirectory = "c:\"
# C:\> [Environment]::CurrentDirectory
c:\
# C:\> pushd C:\Temp
# C:\Temp> [Environment]::CurrentDirectory
c:\
# C:\Temp> Get-Location
Path
----
C:\Temp
WHat is probably happening is that msiexec.exe is using the working directory (i.e. [Environment]::CurrentDirectory) and not the current powershell location at invocation. I would just specify the full path to msiexec:
msiexec.exe /i "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\\Screwdriver.msi"
MSI installation packages build with an older WIX utility would throw the error whenever installation was attempted from a batch script that was accessed on a shared drive using UNC path instead of a mapped drive letter. On the other hand whenever the batch file was executed with a mapped drive letter the installation would work normally.
I'm not blaming WIX here because I'm not certain whether they are responsible. I'm just describing symptoms here. It might just be the result of invoking plain vanilla Windows batch script that in turn executes msiexec with a bunch of command line parameters.

BATCH move files to C:\Windows

I'm trying to move sleep.exe and nircmd.exe to C:\Windows using move command in my batch file. The code goes like this
move "%cd%\sleep.exe" "C:\Windows"
move "%cd%\nircmd.exe" "C:\Windows"
When I do not use administrator privileges I get error:
Access denied
But when I use administrator privileges I get error:
System cannot find the path specified
EDIT:
I also tried:
move "%cd%\sleep.exe" "C:\users\%username%\desktop"
And that worked, but as I said I want it to move to C:\Windows
When using administrator it resets the working directory, and which is why it couldn't find the files.
The solution here sets the location of the files to the folder which contains the batch file.
for %%a in ("sleep.exe" "nircmd.exe") do move "%~dp0\%%~a" "C:\Windows"
I didn't know that administrator mode changes your current director (%cd%)
but thanks to #foxidrive i know that now gooled it and found the answer
#setlocal enableextensions
#cd /d "%~dp0"

Why cannot i use batch XCOPY while running in admin mode?

i have run very simple script:
xcopy some.exe c:\folder\ /h/y and it works normally. but when i try to run .bat file with this code as administrator - cmd line opens for a moment but nothing happens (file not copied). Can anyone explain this issue?
i also tried to use echo xcopy instead of xcopy, but nothing had changed.
i need only admin running of .bat file, cause i want to copy file in \windows\system32 folder
when you start a batchfile as administrator, it's working directory is C:\windows\system32\. So your script doesn't find your file. Either work with absolute paths, or change the working directory.
You can change it to the directory, where your batchfile resides with:
cd /d "%~dp0"
Note: to keep the window open to read any errormessages, append a pause command.

Resources