Batch file command not working - batch-file

I am trying to run this batch file remotely It will kill the IE process's but when I try to open a .lnk file it won't do it. When I go onto that machine, open up the command prompt and type in the command to run the .lnk file it works with no issues.. please help!
Code to remotely execute batch file:
psexec -u Administrator -p password -i -d \\hostname "c:\Emergency_POD\test.bat"
Code on machine to run: (Only the taskill command works.. not the for command)
cd/
taskkill /im iexplore.exe /f
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"
Command to run on cmd (This command works with no issues:
for %a in ("C:\Emergency_POD\*.lnk") do #start "" "%a"

You'd probably be better off with %%a in place of %a in the batch file.

Related

Command Prompt (cmd) is not executing Plink SSH Command

I am trying to execute a batch file (.bat) with the following commands in the batch file.
I have a batch file containing the following;
#ECHO OFF
cmd.exe /K "cd C:\Program Files (x86)\PuTTY && C:"
set PATH=%PATH%;C:\Program Files (x86)\PuTTY
pause
plink.exe -ssh username#firewall1 -pw PassWord! < commands.txt > c:\output_.csv"
pause
the plink.exe command works when entered in manually.
commands.txt is just a simple firewall command for now.
All I see when running the batch file is a cmd window open point at the Putty folder, and that's it.
So how can i get this to run please?
Your batch file at line 2, opens a new cmd window. That's not probably what you want. Also it changes the current directory and the current drive, read HELP CD, and then change that line to just cd /d "C:\Program Files (x86)\PuTTY"
Also, as a bonus recommendation, you are changing the PATH every time you run this command, adding the putty directory that you mage as current anyway, so this command is unnecessary and redundant.
So, I would leave your bat as simple as
#ECHO OFF
CD /D "C:\Program Files (x86)\PuTTY"
plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >c:\output_.csv
I would suggest making the batch file simpler still:
#Start "" /D "%ProgramFiles(x86)%\PuTTY" plink.exe -ssh username#firewall1 -pw PassWord! <commands.txt >C:\output_.csv
You may consider running other options such as /MIN, type Start /? at the command prompt for usage information

Run all SQL files in multiple directories

I combined a couple of solutions I found online to try and make this happen.
https://stackoverflow.com/a/6504317/2471473
https://sqlandme.com/2013/03/25/sql-server-executing-multiple-script-files-using-sqlcmd/
I'm trying to run a single .cmd script (Script1.cmd) with folder locations of .sql files. That single script runs another script (Script2.cmd) to use sqlcmd to execute all the .sql files in that folder location. It mostly works, but it leaves a command window open that I have to exit from for each folder location.
Script1.cmd
start Script2.cmd "C:\Location1"
start Script2.cmd "C:\Location2"
Script2.cmd
#Echo Off
FOR /f %%i IN ('DIR %1\*.Sql /B') do call :RunScript %1 %%i
GOTO :END
:RunScript
Echo Executing Script: %2
cd %1
SQLCMD -S Server123 -d Database456 -E -i %2
Echo Completed Script: %2
:END
Official command line reference for Windows XP or for Windows Server 2003, Windows Vista (and above) seems to be too brief. Read this (extended) start command documentation:
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a TITLE this can be a simple string like "My Script" or
just a pair of empty quotes "". According to the Microsoft
documentation, the title is optional, but depending on the other
options chosen you can have problems if it is omitted.
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
Next Script1.cmd should work and close started command windows:
start "" cmd /C Script2.cmd "C:\Location1"
start "" cmd /C Script2.cmd "C:\Location2"

using runas with a password and a sql command

I have a batch file with the following script (which i found here at stackoverflow)
#if (#CodeSection == #Batch) #then
#echo off
start runas "/user:domain\username" "cmd.exe"
CScript //nologo //E:JScript "%~F0"
goto :EOF
#end
WScript.CreateObject("WScript.Shell").SendKeys("mypass{ENTER}");
where mypass is the username's password.
this works perfectly and a new commandline window opens as the new user!
the problem is that i now need to run an sqlcmd with parameter and I can't find any way to write a batch that does both things together (open the new window as another user together with the command with parameters)
the sql command:
"C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE" -S "my.server.com, 1234" -d "myDataBase" -E -b -q "EXEC myDataBase.[dbo].[myTable] #parameter=1"
I tried moving the whole sql command to another batch (myBatch.bat) file and change the first batch to this:
start runas "/user:domain\username" "myBatch.bat"
but it doesn't work.
any help please?
runas /user:domain\user "cmd.exe /k c:\path\to\mybatch.bat test"
cmd /k to keep the console open after the batch execution, or cmd /c to close it

Can't kill program by taskkill

I use Tecplot to opem its script file (*mcr) to do visulization. After I write the batch codes to first open tecplot to launch the *mcr file and then kill it, I found the codes can't terminate the program. The codes in batch file is as following:
cd .\Re100\17\
cd .\0.001
tec360 extract.mcr
ping 192.0.2.2 -n 1 -w 100000
taskkill /F /IM tec360.exe
cd ..\
cd .\0.005
extract.mcr
ping 192.0.2.2 -n 1 -w 100000
taskkill /F /IM tec360.exe
But, if I manually closed the Tecplot then the batch file can work. Any help on this will be highly appreciated.
Try looking on this site http://technet.microsoft.com/en-us/library/bb491009.aspx It will show you how to use the task kill command.
You simply need to add a $!QUIT command in the end of your macro file "extract.mcr" that will close the tecplot after the macro has finished, this way your tecplot GUI will close itself. The macro file should look like this
#!MC 1410
# some other macro commands .....
$!QUIT
In my experience you don't really need the GUI of tecplot to load, you can run your macro in the batch mode which will be quick, and that won't require $!Quit command either. to run in batch mode your batch file look like this
cd .\Re100\17\0.001
tec360 -b extract.mcr
cd ..\0.005
tec360 -b extract.mcr
On an additional note, if you have same file name i.e. extract.mcr and a lot of folders where you want to run, then instead of hardcoding the bath like you did, in windows here is how it would be,
SET my_dir="Re100\17\0.001" "..\0.005"
FOR %%A IN (%my_dir%) DO (
cd %%A
tec360 -b extract.mcr
)
Now you can add as many folders in first line as you want with a space separating the paths for example
SET my_dir="path 1" "path_2" "path 3" "path 4"
and so on..

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Resources