I need to create a .bat to put together with my setup system to install a network driver, but I have some difficulties in creating the bat.
This .bat needs:
execute a cmd with administrator privileges
run this command: netcfg.exe -v -l networkbll_lwf.inf -c s -i nt_networkbll
exit
The folder for all files location is: c:\Windows\System\Drivers.
You might have to use another batch file first to launch the second with admin rights.
In the first use
runas /noprofile /user:mymachine\administrator batchfilename.bat
PAUSE
and write the needed command in another bat file
Related
I need to create a batch file that will open cmd.exe (as a administrator) with these lines:
cd C:\Program Files\Putty
cd C:\Program Files\Putty>psftp -i XXXXXXXXX.ppk xxxx_test#111.111.111.11 -b C:\UPLOAD\upload.ftp
Can anyone help me?
DoStuff.bat
#echo off
runas /user:%USERDOMAIN%\%USERNAME% "cmd.exe /K \"cd C:\Program Files\Putty\" & psftp -i XXXXXXXXX.ppk xxxx_test#111.111.111.11 -b C:\UPLOAD\upload.ftp"
runas will let you run a command as Administrator (you may need to change the domain/user to match your system, and enter a password).
Running cmd.exe /K will run the following commands.
The quotes around the commands need to be escaped with \"
I'm having a hard time seeing the difficulty.
Can you explain what you tried, and why it didn't work?
I Want to invoke docker compose using bat file.I have tried to invoke using the following but its not executing the commands.
this is my .bat file
echo on
cd C:\Program Files\Docker Toolbox\
start start.sh cd desktop
cd test
docker-compose up
Is there any other way to execute docker commands using bat file.or any other file.
A batch file is a script file. A script needs an interpreter. For a batch file the interpreter is cmd.exe – the Windows command interpreter.
A *.sh file is also a script which needs an interpreter. The interpreter is on Unix/Linux systems sh, bash, ksh, ... which are also executables but without file extension .exe because on Unix/Linux executables usually do not have a file extension.
On Windows there is no interpreter installed by default for Unix/Linux shell scripts. If start start.sh works at all than because of having installed a shell interpreter on Windows which has been registered in Windows registry as application for opening *.sh files which means interpreting the commands in the shell script file by started application.
But commands in a batch script interpreted by cmd.exe and executed within a Windows command environment can't be executed in shell environment of the shell interpreter.
start start.sh starts a new process running parallel to the Windows command process created for execution of the batch file. The batch file processing immediately continues after this line with executing the next command in command process while the shell interpreter process interprets parallel the commands in start.sh.
So what you need here is a batch file which creates a shell script to call start.sh and executes other shell commands in shell environment by shell interpreter.
The batch code below might work. It is not tested by me as I don't have Docker Toolbox nor any shell interpreter installed on my Windows machine.
#echo off
set "ShellScriptFile=%TEMP%\%~n0.sh"
( echo start.sh
echo cd desktop/test
echo docker-compose up
) >"%ShellScriptFile%"
start "Docker Compose" /D"%ProgramFiles%\Docker Toolbox" /wait "%ShellScriptFile%"
del "%ShellScriptFile%"
set "ShellScriptFile="
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~n0 (name of batch file without file extension and path)
del /?
echo /?
set /?
start /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of redirection operator > used here to create the shell script to execute with the 3 lines:
start.sh
cd desktop/test
docker-compose up
Those 3 lines are executed in the shell environment by shell script interpreter.
To run docker-compose via a bat file,
1. create a bat file eg run-docker-compose-up.bat
cd c:\docker
docker-compose up -d
pause
c:\docker is where I placed my docker-compose.yml
-d run containers in the background docker compose up docs
docker-compose must run as Administrator.
While a bat file itself cannot be marked as run as Administrator, you can either
right click on the bat file and choose 'Run as administrator'
or create a shortcut to the bat file and mark it as run as Administrator
(right click on shortcut, choose Properties, choose Advance, choose 'Run as administrator')
As I solved this problem, in a docker-compose case:
Go to your docker-compose.yml folder
On that, create a batchfile, such as: mydockerbatchfile.bat, using notepad (or other)
On this file, type your docker commands sequence', such as:
echo on
docker-compose down
docker container prune --force
docker system prune --volumes --force
docker image rm xxx/web-api
docker system df
docker image ls
pause
OBS: the commando pause will allow you check the results execution, in the screen)
Save -> make sure you saved in batch format/extension (mydockerbatchfile .bat)
In your windows desktop, create a ShortCut for the 'item 3 batch-file' (here - do not set the shortcut to "run as administrator")
I am trying to redirect my plink command file output (That executes a script on UNIX server) to another file. But I am getting Access Denied issue while redirecting output.
Batch file:
cd "C:\Program Files\PuTTY"
plink -t w44gq8asd#USA7061UX02.wv.abcd.net -pw T#12Ts "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8asd/a.sh" > output.txt
cmd
Your working directory is in Program Files, where you do not have a write permissions, so it's only natural, that you are getting "Access denied".
You have to write the output file to a different folder:
plink ... > C:\path\where\you\can\write\to\output.txt
If you wanted to write to the path, where your batch file is, what about not changing the working directory in the first place:
"C:\Program Files\PuTTY\plink" ... > output.txt
(and remove the cd command)
I have to transfer a file from one server to another. I log in to the first one with PuTTY and then type this:
sftp -v -oIdentityFile=path username#host
cd path
put file
Everything works perfectly! Now I'm trying to do it with a batch file. In the .bat I have:
putty.exe -ssh host1 -l username1 -pw password1 -m script.txt
In the script.txt file:
sftp -v -oIdentityFile=path username2#host2
cd path
put file
exit
It connects to the server number two but then it stops. The prefix sftp> does not appear and it does not read the following lines. Do you have any suggestion?
The remote shell takes the commands and executes them one by one. So it executes the sftp, waits for it to exit (what it never does) and only then it would execute the cd command (but in shell, not in the sftp), the put (failing as that's not a shell command), etc.
If your intention was to simulate typing the commands as on a terminal, use Plink and input redirection.
The Plink (PuTTY command-line connection tool) is a tool from PuTTY package that works like PuTTY, but it is a console, not GUI, application. As such it can use input/output redirection. And anyway, the Plink is the tool to automate tasks, not PuTTY.
plink.exe -ssh host1 -l username1 -pw password1 < script.txt
For more details, see How to type commands in PuTTY by creating batch file? on Super User.
As part of as our Team Build MSBuild script, we have a deployment batch file which I need to execute on a remote server:
<Exec Command="psexec -accepteula \\servername D:\Build_Drop\DeploySites.bat "/>
I can confirm it is not permissions or firewall: if the bat command is changed to run iisreset or calc.exe for example, the command will work.
I have tried -s and >nul flags, and also wrapping the batch file in a call to cmd. I have tried with and without quotes around the bat cmd.
All the options I have tried will work fine from the cmd line on the build server, but none will work from within the build script itself.
Any ideas?
Adding a -i parameter to psexec seems to have worked.
Oddly, we now get the error message:
The command "psexec -accepteula \\server -i cmd /c "D:\Build_Drop\DeploySites.bat"" exited with code 5. Please verify that you have sufficient rights to run this command.
But the command does actually work