I wrote a small script which intends to copy a file from our Servers, copy it to a local machine and run it. It works for me, however I would like to add parameters in order to make it easy for others to use it as well.
#echo off
pushd \\NetworkPath & copy batfile.bat \\ComputerName\c$\Users\UserName\Desktop & popd & psexec -i -s -d \\ComputerName -u UserName -p UserNamePassword "C:\Users\UserName\Desktop\batfile.bat"
As you can see it copies the file locally to the Desktop of the user and runs the file itself. Please tell Me how I can use variables for ComputerName,UserName and UserNamePassword in order to have a query each time asking me what are the values.
You need the SET /p command:
#echo off
SET /p pwd=password:
SET /p usr=user name:
SET /p compname=computer name:
pushd \\NetworkPath & copy batfile.bat \\%compname%\c$\Users\%usr%\Desktop & popd & psexec -i -s -d \\%compname% -u %usr% -p %pwd% "C:\Users\%usr%\Desktop\batfile.bat"
Related
I am currently trying to write a script to change the location (snmp) of more than 200 Cisco switches.
My problem is that I can't run more than one command at once. I've made a batch file which connects to the switch automatically and reads a .txt file where the commands are listed. But no matter what I do the best result I got was that only the first command was executed.
batch File:
cmd.exe /c echo n | "Filepath(plink)" -ssh Switch Hostname -l Username -pw "Password" -m "txt File"
txt File:
conf t
snmp-server location test
end
wr
exit
I've already tried other delimiters in the txt-File like ; | etc.
But nothing seems to work.
It's actually a known limitation of Cisco, that it does not support multiple commands in an SSH "exec" channel command.
Quoting section 3.8.3.6 -m: read a remote command or script from a file of PuTTY/Plink manual:
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
Though actually, your commands are probably not standalone top-level shell commands anyway. I guess that the snmp-server (and others) are subcommands of conf t, aren't they? So your code would not work, even if Cisco did support multiple commands on the "exec" channel.
For details, see How to type commands in PuTTY by creating batch file?
You need to execute the conf t and then provide its subcommands to its standard input.
One way to do that is like this:
(
echo snmp-server location test
echo end
echo wr
echo exit
) | plink -ssh hostname -l username -pw password conf t
If the above mentioned Cisco limitation doesn't affect this syntax:
SET /P USERNAME=Enter remote Username:
SET "psCommand=powershell -Command "$pword = read-host 'Enter remote Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" for /f "usebackq delims=" %%p in (`%psCommand%`) do set PASSWORD=%%p
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND1; COMMAND2; COMMAND3; ETC"
If the above mentioned Cisco limitation DOES affect the above syntax:
SET /P USERNAME=Enter remote Username:
SET "psCommand=powershell -Command "$pword = read-host 'Enter remote Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" for /f "usebackq delims=" %%p in (`%psCommand%`) do set PASSWORD=%%p
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND1"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND2"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "COMMAND3"
plink -t -pw %PASSWORD% %USERNAME%#Hostname "Etc"
Barbaric, yes, but I think Cisco can be thanked for that ;) (This is un-tested as I don't have a cisco device to poke at, but theory should be sound)
Good afternoon all,
I am having an bit of trouble with the following Batch file.
#echo off
set /p Pass=Enter your password:
ncftp <<EOF
open -u thomas -p %Pass% MyHost.com
cd "Program"
lcd "../Program"
put -R *
bye
EOF
It returns after the password the following error
<< not expected at this time
I have this script from somewhere on the internet. And if he does it it works like an charm.
What am i doing wrong or what must i do i have searched the internet with << but i didnt get any search results on that matter.
With kind regards,
Thomas de Vries
This is the problem in your script.
ncftp <<EOF
<<EOF is a syntax error and <EOF would be redirecting a file called EOF in the same folder, into the ncftp executable.
It would seem that your source material is not quite debugged.
#echo off
set /p Pass=Enter your password:
(
echo open -u thomas -p %Pass% MyHost.com
echo cd "Program"
echo lcd "../Program"
echo put -R *
echo bye
) | ncftp
I'm in a process of creating a batch file to list names of all SQL scripts in a folder to a text file. My plan to convert this text file to a batch file so that I can use it to execute the scripts in the server. So I would like to have the following string appended before each file name while creating the initial text file
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
This is a batch file command and I would like to be appended before the each file names.
eg:
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i 001_ALTER_PERSON.sql
The code I'm using is
set MSSQLSERVER_NAME = "%MSSQLSERVER_NAME%"
set %MSSQLSERVER_DBNAME = "%MSSQLSERVER_DBNAME%"
set myvar=sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
for /r . %%g in (*.sql) do echo %myvar% %%~nxg >> D:\test.txt
pause
Out put I'm getting
sqlcmd -S -d -i 015_ALTER_vBOARD_PAPERS.sql
Let me know how to tackle this
set myvar=sqlcmd -S %%MSSQLSERVER_NAME%% -d %%MSSQLSERVER_DBNAME%% -i
should cure your problem - % escapes %
note that spaces ARE significant in variable names, so set var = somethingwill set var[space] to [space]something
set %varname%=... is rare -it sets the variablename (contents of varname).
Even more rare to have unbalanced %... not sure that will work at all...
I am using sql server 2008 , I am developing script that get all .sql file of given path ( also serch in subfolder recursively). Thanks in advance.
You could use a batch file like this. Call it ashwin.bat (or whatever you like) and it will look for all the files in C:\tmp\so\ashwin that have a .sql extension and then invokes sqlcmd against all of those files against a named instance database of localhost\localsqla and runs them in the master database.
#echo off
For /R "C:\tmp\so\ashwin\" %%i in (*.sql) DO CALL sqlcmd.exe -E -S localhost\localSQLA -d master -i %%i
A litle enhancement for logging purposes:
#echo off
For /R "C:\Deploy\SQL" %%i in (*.sql) DO CALL echo %%i && sqlcmd.exe -E -S DB_IP -d DATABASE -i %%i -j
I get a bat file as below:
#ECHO Executing scripts...
PAUSE
for %%X in (*.SQL) do SQLCMD -S localhost -d CTL -I -i "%%X" >> ResultScript.txt
pause
In this I want to has user inputs for localhost (Sql server instance) and CTL (database).
How can this be achieved in DOS (os: WinXP)
Thanks
SET /P variable=PromptString
So your batch file would be something like this
#ECHO Executing scripts...
PAUSE
SET /P sqlServer=Please enter SQLServer:
SET /P CTL=Please enter CTL:
for %%X in (*.SQL) do SQLCMD -S %sqlServer% -d %CTL% -I -i "%%X" >> ResultScript.txt
pause
Use parameter markers, where %1 refers to the first parameter, %2 the second, and so forth.:
for %%X in (*.SQL) do SQLCMD -S %1 -d %2 -I -i "%%X" >> ResultScript.txt
If your batch file was called ExecScript.bat, your user would run it as
ExecScript instancename databasename
You'll probably want to add a test above the for loop to make sure both parameters are passed in. Running SQLCMD with a blank instance and database wouldn't work too well.