I am trying to setup a batch script which can connect to a set of servers and execute start script. Since there is a password getting saved in the commands.txt file. I need to delete it after the execution of start on remote servers. But that del command gets executed before everything and which is causing issues for the loop and errors out that commands.txt file is missing. Not sure how thats getting executed before the loop when its put after the loop. How can I fix this?
Below is the code I am trying.
#echo off
Echo Please enter your password in the popup window and then press enter
set tempbat="%temp%\p.cmd"
REM Create temporary batch file to make popup window for entering password 'masked'
echo mode 20,1 >%tempbat%
echo color EF >>%tempbat%
echo Title Enter Password >>%tempbat%
echo setlocal enabledelayedexpansion >>%tempbat%
echo set /p Pass= >>%tempbat%
echo echo !pass!^>"%temp%\pass.txt" >>%tempbat%
echo exit >>%tempbat%
echo exit >>%tempbat%
start /wait "" %tempbat%
set /p Password=<"%temp%\pass.txt"
#echo echo %password% ^| sudo -S -u x0ats echo startup.sh start>>
%cd%\commands.txt
#echo read>> %cd%\commands.txt
for /f "delims=" %%a in (%cd%\serverlist.txt) DO (
Start PuTTY username#%%a -pw %password% -m "%cd%\commands.txt"
)
del %cd%\commands.txt}
There are a few issues in your CMD script, but most specifically, as I mentioned above you are not putting double quotes around paths.
Additionally you were adding spaces to the ends of all of your line sin the bat file but that will cause you to collect the wrong PW, you also didn't delete your temp bat file or password file after the fact.
It seems like a bit of trouble to go through just to pop up a separate window, you could forgo creating a second batch file and call a sub function instead by making your script support cmd line arguments.
In Any case this should work more as you expect:
#ECHO OFF
ECHO.Please enter your password in the popup window and then press enter
SET "_TempBat=%temp%\p.cmd"
SET "_PWFile=%temp%\pass.txt"
SET "_CMDs=%cd%commands.txt"
REM Create temporary batch file to make popup window for entering password 'masked'
ECHO.SETLOCAL>"%_TempBat%"
REM ECHO.ECHO OFF >"%_TempBat%"
ECHO.mode CON COLS=42 LINES=1 >"%_TempBat%"
ECHO.color CF>>"%_TempBat%"
ECHO.Title Enter Password >>"%_TempBat%"
ECHO.SET /p "_Pass=Enter Password: " >>"%_TempBat%"
ECHO.ECHO.%%_pass%%^>"%_PWFile%">>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
ECHO.exit>>"%_TempBat%"
START /wait "" "%_TempBat%"
DEL /F /Q "%_TempBat%"
IF NOT EXIST "%_PWFile%" (
"%_PWFile%" Not Created!
) ELSE (
FOR /F "Tokens=*" %%A IN ('TYPE "%_PWFile%"') DO (
SET "_PW=%%A"
DEL /F /Q "%_PWFile%"
)
)
#ECHO.ECHO.%_PW% ^| sudo -S -u x0ats ECHO.startup.sh start>> "%_CMDs%"
#ECHO.read>> "%_CMDs%"
for /f "delims=" %%a in ('Type "%_CMDs%"') DO (
Start PuTTY username#%%a -pw %_PW% -m "%_CMDs%"
)
Pause
DEL /F /Q "%_CMDs%"
ALSO I changed the colors you chose Bright Yellow on Bright right is hardly readable, I used Bright white on Bright Red. oh and I also added an Echo Off into your second script because again that was not helpful to have all the extra stuff in there
Also I looked at this and noticed that you were not going to get the PW saved in the PW file so I fixed that by using %% so that the first % will get stripped off.
Related
I am trying to make a batch script that moves a file into a startup folder and I want to make it universal
Currently I have this but I want to make it work on any user's computer (C:\users\USERNAME)
Here is the code
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %name%
move C:%user%\Desktop\Directory 1\dile.txt C:%user%\Desktop\Directory 1\file folder 1
:end
cmd /k
The file is called dile.txt located in a folder called Directory 1 on the desktop and I want to make it move to a folder called file folder 1 inside the Directory 1 folder. Is there a way to do this while making it work on anyone's computer?
%USERNAME% can be used to grab the active user account. Try something like this. Make sure to enclose paths in quotes when folders have spaces in their names.
#echo off
color A0
echo Startup...
echo Startup..
echo Startup.
echo Startup
echo Startup.
echo Startup..
echo DONE
echo your name is %USERNAME%
move "C:\Users\%USERNAME%\Desktop\Directory 1\file.txt" "C:\Users\%USERNAME%\Desktop\Directory 1\file folder 1\"
:end
cmd /k
Just for the sake of providing something a little bit different:
#Echo Off & SetLocal EnableExtensions
Set /P "=Startup . " 0< NUL
For /L %%G In (1,1,5) Do (Set /P "=. " 0< NUL
%SystemRoot%\System32\PATHPING.EXE 127.0.0.1 -n -q 1 -p 500 1> NUL)
Echo(&Echo Your name is %UserName%
%SystemRoot%\System32\Robocopy.exe "%UserProfile%\Desktop\Directory 1" "%UserProfile%\Desktop\Directory 1\file folder 1" "dile.txt" /Mov 1> NUL 2>&1
Pause
This uses some animated text, %UserProfile%, instead of C:\Users\%UserName%, and robocopy instead of Move. Using RoboCopy, allows for the creation of your destination directory, if it does not already exist.
Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name
thanks
here is my code
REM This script runs in MS-DOS Batch File Language
#echo off
set /p id= Enter ID or Hostname:
echo %id%
WMIC computersystem where caption='%ComputerName%' rename %id%
REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B
Here's how I'd probably do it:
#Echo Off
Echo Your current name is %ComputerName%
:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
GoTo AskID)
Rem Put here some more checks for disallowed words or characters
WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"
Notes
This will need to be run 'As administrator'.
The change will not take effect until the next reboot.
It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.
I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !
set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause
I am doing an ftp batch script. It's an mget command but I have an error with the file name because it has ":"(colon)s in it. So my codes does a for loop which does the get command multiple times. My problem is, is that the .dat file has an empty line which is an error for the ftp command.
Below is my code:
#echo off& setlocal enableextensions
Echo open sample.net>second.dat
Echo user»second.dat
Echo pass»second.dat
Echo ascii»second.dat
Echo cd path/path/path»second.dat
setlocal enabledelayedexpansion
for /f "delims=" %%a in (file_list.txt) do (
set "remote=%%a"
echo get !remote:~0,-1! !remote::=.!»second.dat)
endlocal
Echo bye»second.dat
ftp -v -i -s:second.dat
del second.dat
pause
The file_list.txt is the list of file names that I need to rename using this code
setlocal enabledelayedexpansion
for /f "delims=" %%a in (file_list.txt) do (
set "remote=%%a"
echo get !remote:~0,-1! !remote::=.!»second.dat)
endlocal
The output file is this one
sample.net
user
pass
ascii
cd path/path/path
get logsample-txt-871749063-10-17-2017_22:00:05:692474.txt logsample-txt-871749063-10-17-2017_22.00.05.692474.txt
get logsample-txt-871749063-10-18-2017_08:00:57:671751.txt logsample-txt-871749063-10-18-2017_08.00.57.671751.txt
get logsample-txt-871749063-10-18-2017_08:00:07:724779.txt logsample-txt-871749063-10-18-2017_08.00.07.724779.txt
bye
What I what is, I need to remove the blank spaces for the ftp command to read it properly
sample.net
user
pass
ascii
cd path/path/path
get logsample-txt-871749063-10-17-2017_22:00:05:692474.txt logsample-txt-871749063-10-17-2017_22.00.05.692474.txt
get logsample-txt-871749063-10-18-2017_08:00:57:671751.txt logsample-txt-871749063-10-18-2017_08.00.57.671751.txt
get logsample-txt-871749063-10-18-2017_08:00:07:724779.txt logsample-txt-871749063-10-18-2017_08.00.07.724779.txt
bye
I want the batch file to ask for Serial number and username and delete two specific folders from users profile. I made this but it seems to want to delete *.* from folder I am running it from.
#echo off
set /p serial="Enter Serial: "
set /p username="Enter Username: "
del *.* \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations
del *.*
\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations
pause
Try next approach with basic checking all user's input:
#ECHO OFF
SETLOCAL EnableExtensions
:inputServer
set "serial="
set /p "serial=Enter Serial: "
if not defined serial goto :endlocal
pushd "\\%serial%\C$\"
if errorlevel 1 (
echo wrong server name "\\%serial%"
goto :inputServer
)
:inputUser
set "_username="
set /p "_username=Enter Username: "
if not defined _username goto :endstack
cd "\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
if errorlevel 1 (
echo wrong user name "%_username%" or path
echo "\\%serial%\C$\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
goto :inputUser
)
dir AutomaticDestinations\*.*
dir CustomDestinations\*.*
:endstack
popd
:endlocal
pause
Replace dir with del /Q no sooner than debugged.
Resource for PUSHD - POPD pair:
When a UNC path is specified, PUSHD will create a temporary drive
map and will then use that new drive. The temporary drive letters are
allocated in reverse alphabetical order, so if Z: is free it will be
used first.
c$ is administrator hiding ressources, you can't access to it without login
If you want to connect to network share on the remote computer, use this:
net use * \servername\sharename
The syntax of the del command does not allow to separate the file mask from the path where the files to delete are stored, so you need to use
del /q "\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*"
del /q "\\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*"
The added quotes will prevent problems with paths that include spaces and the /q switch will avoid the security confirmation to delete the all the files in the indicated folder.
First, remove the #echo off untill your code is working so you can see what's happening, then add some echo to see what is stored in the variable that you're using. After that try with DIR instead of DEL so it list the file that match your criteria.
REM #echo off
set /p serial="Enter Serial: "
Echo %serial%
pause
set /p username="Enter Username: "
echo %username%
pause
dir \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*
dir \\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*
pause
i am trying to redirect output from an exe(commanline exe) which is being called in from batch file to log file. In this script IP addresses of hostnames provided in input.txt are being redirected to result.txt. i am trying to run .exe within same batch script to place those IPs in maintenance mode in my monitoring tool. Script runs fine and performs the action as expected but it fails to capture the output from .exe. please help.
#echo off
setlocal enabledelayedexpansion
set OUTPUT_FILE=result.txt
>nul copy nul %OUTPUT_FILE%
for /f %%i in (input.txt) do (
set SERVER_ADDRESS=ADDRESS N/A
for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
if %%x==Pinging set SERVER_ADDRESS=%%y
if %%x==Reply set SERVER_ADDRESS=%%z
if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
)
echo !SERVER_ADDRESS::=!>>%OUTPUT_FILE%
)
start c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"
Output from .exe if i run it directly from command prompt:
PS C:\> .\MaintenanceMode.exe ON C:\result.txt username
Not an IP!!
Reading IPs from File: C:\result.txt
Valid Arguments
System put into MM Host# 10.*.*.* Status# Success
System put into MM Host# 10.*.*.* Status# Success
You are redirecting the output of the START command, but not the exe.
If you want to use START and redirect the output, then you most execute a new CMD.EXE session and escape the redirection so it occurs within the new session:
start cmd /c c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% ^>^> "c:\result2.txt"
But why are you using START? It would be so much simpler if you simply execute your exe directly:
c:\MaintenanceMode.exe ON %OUTPUT_FILE% %USERNAME% >> "c:\result2.txt"