Vmware commands executing batch file - batch-file

net use x: "\\vmware-host\Shared Folders\ShareME">>logfile.txt 2>&1
ping -n 11 127.0.0.1 > nul
x:
cd "Firefox18"
call autorun.bat>>logfile.txt
cd ../
exit
When I execute the above batch file by double clicking, logfile.txt is generated. But when I use vmrun -T ws -gu <Username> -gp <Password> runProgramInGuest <Vm-Path> <path of Batch File to execute> The scripts runs fine, only the logfile.txt is not generated.
This happens only in windows 7. It works fine for Windows XP.

The problem solved I just needed >>"C:\Users\Ebryx 2\Desktop\logfile.txt" 2>&1
The quotation mark for the path seemed to work for windows 7.

Related

PuTTY -m command option failing with "unable to open ... file" on Windows 7

I'm trying to make an automatic PuTTY login using a batch file. I have this:
start "title" "C:\Program Files\PuTTY\putty.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
Everything works on my Windows 10 machine but in Windows 7, the command option -m does not work. The error message is:
unable to open command file:"commands_to_execute.txt"
I have tried changing all paths to "C:\Program Files\PuTTY", setting the working directory /D, working in the actual directory and I also add the path to the enviroment variables in:
Advance system settings >Enviroment Variables
I have also used plink instead of putty.
What is happening?
It is very unlikely that your problem has anything to do with Windows 7 vs Windows 10.
Most likely the working directory for your batch file execution on Windows 7 is not set to the folder, where the commands_to_execute.txt file is stored.
Possible solutions are:
Set the working directory the same way you have set it on Windows 10
Use a full path to the script file:
-m "C:\path\to\commands_to_execute.txt"
Set working directory for PuTTY explicitly using:
start "title" /D "C:\path\to" "C:\Program Files\PuTTY\putty.exe" ...
Or, if the script file is in the same folder as your batch file, you can use:
start "title" /D "%~dp0" "C:\Program Files\PuTTY\putty.exe" ...
you need to use plink.exe for this not putty.exe, just replace:
start "title" "C:\Program Files\PuTTY\plink.exe" "server_name#server_IP" -pw "password" -m "commands_to_execute.txt"
or make it even easier:
cd C:\Program Files\PuTTY\
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD ~/script.sh
plink.exe -ssh pi#192.168.1.166 -P 22 -pw P#SSWRD -m commands.txt
pause
either of the two lines work.

Piping output in cmd batch file on Azure Batch service error codes not detected

I have a .cmd batch file (let's call it RunSQlCmd.cmd) which pipes out output of sqlcmd to 7 zip compressor
sqlcmd -i.\table.sql -S . -E -s "," -I -h -1 -W| "c:\Program Files\7-Zip\7z.exe" a -tbzip2 -si "out.csv.bz2"
I run it from Azure Batch in a task from a C# driver program with the following command line
(1) cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
But what seem to happen is that command line returns almost immediately and commands in RunSqlCmd.cmd are not fully executed and empty compressed archive is created. Azure Batch task exits with success code 0.
If I change task's command line to
(2) cmd /c start /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
commands in a batch file run successfully but stdout and stderr is lost for Azure Batch as batch file runs in a separate cmd window and task hangs up without getting any error code.
changing tasks's command line to
(3) cmd /c start /B /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
is similar to (1)
What is the correct way to do it so that Azure Batch detects correctly when task is finished and RunSqlCmd.cmd command finishes completely?
P.S. Real content of RunSqlCmd.cmd file is as
#echo Run SqlCmd
sqlcmd -i "%~dp0%1.sql" -d dbName -S serverName -U userName -P "password" -s "," -I -h -1 -W -b | "%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 -si "%~dp0%1.csv.bz2"
#echo Done SqlCmd
It takes 1 parameter - name of sql file to pull data
I do not quite understand why you want to run cmd /c and start /wait to kick off a program. you do not need to call cmd.exe from start and do not need to call cmd to start a batch file.
if it is a batch file, you can just call the batch file. it will automatically wait internally for the command to complete.
%AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd
That should work as is without calling start or cmd again.
The main problem is that you are sending the command with parameters in quotes to external program, so you you call cmd again and it sends the commands to this cmd window and stripping out quotes when it sends to the external. Just calling the script as is will do.

Catch result of process from exe (right click context)

I wrote following reg file inorder to delete files/folders securely.
Cmd working as expected. But sometimes I would like to see result of it (I do not want to disappare of cmd window)
How can I pause / wait / stop before windows disappared
"D:\Desktop\_NotUse\SDelete\sdelete.exe -s -p 5 \"%1\"" /pause did not work.
Here is my registry file.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\SDelete]
#="Secure Delete File"
"Icon"="imageres.dll,-89"
[HKEY_CLASSES_ROOT\*\shell\SDelete\Command]
#="D:\\Desktop\\_NotUse\\SDelete\\sdelete.exe -p 5 \"%1\""
[HKEY_CLASSES_ROOT\Directory\shell\SDelete Directory]
#="Secure Delete Folder"
"Icon"="imageres.dll,-89"
[HKEY_CLASSES_ROOT\Directory\shell\SDelete Directory\command]
#="D:\\Desktop\\_NotUse\\SDelete\\sdelete.exe -s -p 5 \"%1\""
Sdelete doesn't have a pause command. You'll need to run it with a program that doesn't exit so console stays open.
See cmd /?
eg
cmd /k forfiles
or
cmd /c forfiles & pause

Batch file command not working

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.

dos batch script using psexec shows 'help' text every time it loops

Currently working on a script to ping every host on a /24 subnet, and then executes another script which runs psexec on those machines which are online. The ping sweep script is called ping.bat and the other script which actually runs psexec on the machines is called deploy_mir.bat. I can simply run deploy_mir.bat on a remote host and it will run no problem.
The problem im having is that every time mir.bat runs, which itself contains a loop, it will display the help info for psexec in the cmd window. As far as i can tell everything is working fine, aside from the annoying fact that everytime the loop inside of mir.bat runs my cmd window gets filled with the help info for psexec. I dont have #echo enabled, not that it would cause this anyway.
hoping for a quick fix, but if my code is needed to get an answer ill post it.
Posting the code anyway...
#echo on
setlocal EnableDelayedExpansion
set /p ipAddress="enter ip address: "
for /l %%i in (1,1,255) do (
ping -n 1 %ipAddress%.%%i | find "TTL" > nul
if !errorlevel! == 0 (
call deploy_mir.bat %ipAddress%.%%i
)
)
endlocal
deploy_mir.bat code
#ECHO OFF
echo "Mir Agent deployment to: %1"
rem net use T: \\%1\C$ /user:administrator "password"
net use T: \\%1\C$ /user:administrator "username"
copy /y conf.xml T:\WINDOWS\
copy /y setup_mir.bat T:\WINDOWS\
net use t: /delete
rem psexec \\%1 -i -u administrator -p "password" c:\windows\setup_mir.bat
psexec \\%1 -i -u administrator -p "username" c:\windows\setup_mir.bat
Desired cmd line result of running deploy_mir.bat
C:\DOCUME~1\socuser2\MIR>deploy_mir.bat 10.180.145.66
"Mir Agent deployment to: 10.180.145.66"
The command completed successfully.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
t: was deleted successfully.
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\setup_mir.bat exited on 10.180.145.66 with error code 0.
C:\DOCUME~1\socuser2\MIR>
Just a suggestion. Not sure if it will solve your problem, but may provide some guidance:
My first step would be a small test by explicitly calling psexec on some test batch file in place of the line call deploy_mir.bat %ipAddress%.%%i. If no help message appears, since deploy_mir.bat works find on its own, try explicitly placing it's content in place of the same line call deploy_mir.bat %ipAddress%.%%i. If that works, then there is some issue in the said line we've been replacing. I believe dos / batch will open a sub shell from this line of code and run it's code in that scope. That may be causing the problem. Just guessing with the information provided.
Code Specific Notes:
#echo is enabled, but you say it is not in your question.
!errorlevel! == 0 should be !errorlevel! EQU 0
Some General Notes:
In general, I used to pass parameters to batch scripts in quotes, then strip the quotes with %~1 once inside the batch script. Similarly for if conditions, as !someVar! == a will throw and error if someVar is not set / empty, while "!someVar!" == "a" will gracefully not meet the criteria of the if condition.
I don't know why it works when called from outside the loop. But the psexec line in deploy_mir.bat should have cmd /c.
psexec \\%1 -i -u administrator -p "username" cmd /c c:\windows\setup_mir.bat

Resources