I'm trying to create a batch file to execute all the scripts from a folder and print logs in a different folder and getting access id denied error. Below is my folder structure. Thanks in advance.
Scripts path - C:\project\Queries_Testing\Scripts
Logs - C:\project\Queries_Testing\logs
Batch file - C:\project\Queries_Testing\executeQueries.bat
Code from executeQueries.bat
#ECHO OFF
setlocal enabledelayedexpansion
set /p serverName=Enter DB Servername :
set /p dbName=Enter Database Name :
set /p userName=Enter Username :
set /p password=Enter password :
set /p scriptsPath=Enter Scripts Path :
set /p output=Enter path for output:
for %%G in (*.sql) do sqlcmd /S %serverName% /d %dbName% -U %userName% -P %password% -i"%%G" -o%output%\%%G.lng text**og
ECHO Finished!
pause
Here's an example script which uses the information I provide in my comments.
executeQueries.bat
#Echo Off
Set /P "serverName=Enter DB Server Name: "
Set /P "dbName=Enter Database Name: "
Set /P "usrName=Enter User Name: "
Set /P "password=Enter User Password: "
Set "scriptsPath=C:\project\Queries_Testing\Scripts"
Set "output=C:\project\Queries_Testing\logs"
For %%A In ("%scriptsPath%\*.sql"
) Do sqlcmd /S "%serverName%" /d "%dbName%" -U "%usrName%" -P "%password%" -i"%%A" -o"%output%\%%~nA.log"
Echo Finished!
Pause
The code above uses the provided locations for the scripts and logs directories with a standard Set command. Once you've verified that it works, you may revert back to the Set /P format as used in the 4 lines above them.Please note that your script does not provide any verification routines for the end users input information. If they input incorrect information, the sqlcmd will be run using it, and may be problematic or produce errors.
Please provide feedback accordingly; thank you.
Related
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.
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 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 Build SCCM Servers for work. We have a set of source files that need copied to multiple servers for each build. I am trying to create a script that will prompt the user for the number of role servers involved in the build, then run a loop and ask for the names of "X" number of servers, then enters the names of those servers in a text file like this:
Server1
Server2
Server3 etc..
then I plan to use that list of server names to do an xcopy to those servers. I want to get all of the input done on the front end so that the user can start it and leave it because it can take several hours to copy these files.
Here is my code so far, and it just closes after asking for the first server name. (also I left the first entry out of the do loop so that I can use it to clear the file of any old input)
#echo off
Set /p ServerCount = "How Many Role Servers? : " %=%
Set /p ServerName = "Enter Server Name : " %=%
Echo %ServerName% >Test.txt
Do i = 2 to %servercount% by 1
Set /p ServerName = "Enter Server Name : " %=%
Echo %ServerName% >> Test.txt
Echo %i
enddo
The Do loop don't exist in BAT you have to use a for loop :
#echo off
Set /p ServerCount="How Many Role Servers? :
setlocal EnableDelayedExpansion
for /l %%a in (1,1,%serverCount%) do (
Set /p ServerName=Enter Server Name [%%a]:
Echo !ServerName!>>Test.txt
)
I'm stuck in a bit of a pickle and i am wondering if someone could help. I work in a school in which at the beginning of the year, we assign new laptops to a new year level.
What i need to do is give each student local admin rights to a laptop that has been assigned to them.
I currently have a csv file which has two columns. One with the students username, the second with the laptops device name on the server.
eg. jdoe1,device001
jdoe2,device002
What i would like to do is automate adding the local admin account of each device by having the the batch script pull the names on each row and adding them as the local admin of the device that is assigned.
eg. script finds the username jdoe1, then sees device001.
It then creates a local account called jdoe1
(which is pulled from the active directory on our server)
on the laptop device001.
The script then moves to the next row and starts the process again.
This is what i have so far. This will pull the name and device name and do what is neded but if there are multiple rows of names it will only process the last row and i need it to do each row individually. The script also changes the description of the device to include the students username and the device name.
(please note this is messy as i am very new to batch scripting)
#echo.
#echo The following script will assign a student to a laptop, giving the student
#echo Administration rights to the laptop and set the laptop description.
#echo.
#echo.
#echo.
#pause
cls
#echo off
FOR /F "tokens=1,2 delims=, " %%a in (data.csv) DO (
#set user=%%a
#set LaptopID=%%b
)
#set desc= CFS - Mobile Student - %LaptopID% - %user%
#echo.
#echo Please enter Admin Username
#set /p adminusr=
cls
#echo.
#echo Please enter Admin Password (make sure noone is looking as password is displayed)
#set /p adminpw=
cls
#echo.
#echo Now creating local admin account for %user% on %LaptopID%
#echo.
#echo Description: %desc%
#echo.
#pause
#***\psexec \\%LaptopID% -u ***\%adminusr% -p %adminpw% -n 20 -e net localgroup Administrators "***\%user%" /add
#***\psexec \\%LaptopID% -u ***\%adminusr% -p %adminpw% -n 20 -e net config server /srvcomment:"%desc%"
#pause
The * is replaced as needed. For each new username i need to be able to view the username and pc it is being assigned to, so i can confirm the details (a requirement by the head of the IT department)
Can this be done?? please help!!
Thanks heaps
#echo off
setlocal enabledelayedexpansion
echo.
echo The following script will assign a student to a laptop, giving the student
echo Administration rights to the laptop and set the laptop description.
echo.
echo.
echo.
pause
cls
FOR /F "tokens=1,2 delims=, " %%a in (data.csv) DO (
set user=%%a
set LaptopID=%%b
set desc= CFS - Mobile Student - %%b - %%a
echo.
echo Please enter Admin Username
set /p adminusr=
cls
echo.
echo Please enter Admin Password (make sure noone is looking as password is displayed)
set /p adminpw=
cls
echo.
echo Now creating local admin account for %%a on %%b
echo.
echo Description: !desc!
echo.
pause
***\psexec \\%LaptopID% -u ***\!adminusr! -p !adminpw! -n 20 -e net localgroup Administrators "***\%%a" /add
***\psexec \\%LaptopID% -u ***\!adminusr! -p !adminpw! -n 20 -e net config server /srvcomment:"!desc!"
pause
)
This should get you off the ground. I've no idea what you want to substitute for ***.
Note that #echo off sets command-echoing off until an echo on is encountered. # before a command suppresses echoing of the individual command before it is executed.
setlocal enabledelayedexpansion sets a mode where a variable may be accessed in a block (parenthesised [multiline] statement sequence) by !var! to get the CURRENT value. %var% will get the value of the variable at the time the command invoking the block was PARSED - before execution.
You dont't say whether you want to apply the SAME admin a/c and password to EACH account created. The above should request both each time. The below should allow ONE entry and then apply it to EACH entry in your .csv
#echo off
setlocal enabledelayedexpansion
echo.
echo The following script will assign a student to a laptop, giving the student
echo Administration rights to the laptop and set the laptop description.
echo.
echo.
echo.
pause
cls
echo.
echo Please enter Admin Username
set /p adminusr=
cls
echo.
echo Please enter Admin Password (make sure noone is looking as password is displayed)
set /p adminpw=
cls
echo.
FOR /F "tokens=1,2 delims=, " %%a in (data.csv) DO (
set user=%%a
set LaptopID=%%b
set desc= CFS - Mobile Student - %%b - %%a
echo Now creating local admin account for %%a on %%b
echo.
echo Description: !desc!
echo.
pause
***\psexec \\%LaptopID% -u ***\!adminusr! -p !adminpw! -n 20 -e net localgroup Administrators "***\%%a" /add
***\psexec \\%LaptopID% -u ***\!adminusr! -p !adminpw! -n 20 -e net config server /srvcomment:"!desc!"
pause
)
I'd strongly suggest you prefix ECHO you your ...psexec... lines temporarily to see what the resultant script would do before committing.
No idea of what you mean by ***...