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
)
Related
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.
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 work in IT in a GIS group (mapping). We have a server with folders for all of our GIS users. Whenever a new GIS-using co-worker is identified, because GIS data can be HUGE, they are given READ access to ALL folders on that server and a new folder just for them is created and they are given WRITE access to it.
I would like to create a batch process that would ask for a username, then give that username READ access to all folders, ask for a foldername and create that folder and give that username WRITE access to that folder.
How would I do this?
That what you are trying to do is an login script.
I have wrote an example with 3 users:
#echo off
:start
set /p username=Insert Username:
if /i "%username%"=="User1" goto User1
if /i "%username%"=="User2" goto User2
if /i "%username%"=="User3" goto User3
goto start
:User1
set /p folder1=Insert Foldername:
mkdir %folder1%
goto stop
:User2
set /p folder2=Insert Foldername:
mkdir %folder2%
goto stop
:User3
set /p folder3=Insert Foldername:
mkdir %folder3%
goto stop
:stop
cls
exit
The permissions are allowed by default.
I hope I was able to help you.
I have created a batch script that imports a virtual box appliance and configures it for that machine. Part of the script deletes the share (between the guest and host) and creates it again with a new host path like this:
"%vbPath%VBoxManage" sharedfolder remove "%devBoxName%" --name wwwshare
"%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
When I put these lines of code into a separate file and set shareName directly above, it works fine (eg. shareName=c:/test). However, when I run it as part of the script, the share that is created would have c:/test" as the share path, which of course means it fails.
This is all the code that is used for this variable:
setlocal enabledelayedexpansion
:: Check they want the default share folder name
SET shareName=C:\ld-sd%devNum%-rh7-www\
:getShareName
SET /P shareNameOK="Your shared folder will be %shareName%. Do you want to keep this value (enter y to keep and n to change): "
cls
if /I "%shareNameOK%"=="n" (
SET /P shareName="Please enter the path and name you would like for your shared folder: "
GOTO :getShareName
) else (
if /I not "%shareNameOK%"=="y" (
echo WARNING: You did not enter y or n
GOTO :getShareName
)
)
:getShareCheck
:: Check if this folder exists and if they plan to keep or overwrite it if it does
if /I exist "%shareName%" (
echo WARNING: Selecting yes will result in all website files being erased!
SET /P shareNameOverwrite="This folder already exists, would you like to overwrite the files in it with a clean copy (yes/no)? "
if /I not "!shareNameOverwrite!"=="no" (
if /I "!shareNameOverwrite!"=="yes" (
#RD /S /Q "!shareName!"
timeout /t 1 /nobreak > NUL
mkdir "!shareName!"
) else (
echo WARNING: You did not enter yes or no - NOTE full name
GOTO :getShareCheck
)
)
) else (
SET shareNameOverwrite=yes
mkdir "!shareName!"
)
Whenever I echo the variable (anywhere in the file) it shows no quotations in it. This is the only point it causes an issue. I have even echoed the entire line in question:
echo "%vbPath%VBoxManage" sharedfolder add "%devBoxName%" --name wwwshare --hostpath "%shareName%"
and it shows fine. Where are the quotation marks coming from?
The problem ended up being the line:
SET shareName=C:\ld-sd%devNum%-rh7-www\
should have been:
SET shareName=C:\ld-sd%devNum%-rh7-www
I had missed that there was a \ on the end.
The result of this is that with a \" it will take the " as part of the string and not the end of the string.
So if ever you have a rouge " at the end of a variable, make sure your variable does not end in a backslash!
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 ***...