Windows Server 2008 r2 dsadd usercreation does not work - batch-file

i made a batchfile that is supposed to create users from a txt file but somehow it seems to skip the (for /f .........) dsadd part :( am i doing something wrong?
It's Fixed thx :D
this is the working code
#echo off
cls
echo Creating Accounts
echo -----------------
for /F "tokens=1-2" %%A in (Users.txt) do dsadd user cn=%%A,ou=OU-%%B,dc=school,dc=local -disabled no -pwd Welkom01 -memberof cn=%%B,ou=OU-%%B,dc=school,dc=local -hmdir \\DC01\%%A$\Data -hmdrv S: -profile \\DC01\%%A$\Profile -canchpwd yes -pwdneverexpires yes
echo -----------------
pause

{ANSWERED}
The right code is
#echo off
cls
echo Creating Accounts
echo -----------------
for /F "tokens=1-2" %%A in (Users.txt) do dsadd user cn=%%A,ou=OU-%%B,dc=school,dc=local -disabled no -pwd Welkom01 -memberof cn=%%B,ou=OU-%%B,dc=school,dc=local -hmdir \\DC01\%%A$\Data -hmdrv S: -profile \\DC01\%%A$\Profile -canchpwd yes -pwdneverexpires yes
echo -----------------
pause
the file got messed up becouse I made it with parts of other codes. :)

Related

Batch File Using Net User "Username Not Found" Batch Hangs No Prompt

My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.

bat file Error "invalid verb switch" when using wmic

Hey guys i need some help i am a total noob to bat files i am getting this error "invalid verb switch" I dont understand why?
All i am wanting to do is rename the current user with %newusername%
I have looked on line and everything seams to be the same any thoughts?
#echo off
for /F "tokens=4 delims=.:" %%G in (
'ping -4 %COMPUTERNAME%^|find "Reply from"'
) do set "IP=%%G"
set "ipadd=%IP:~-3%"
set mydate=%date:~4,2%%date:~7,2%%date:~10,4%
set /p pathName=Employees Name:%=%
set /p store=Store prefix (ie: rfl, rac, rca, rdcjr):%=%
set /p location=location (ie: salesflrN1, partsNE2):%=%
echo.
echo.
echo ip address: %last3digits%
set newusername=%store%.%ipadd%-%pathName%
echo New User Name Will Be: %newusername%
echo.
echo ___________________________________________________________________________
echo ***************************************************************************
echo ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
echo.
echo By Pressing enter you will be performing the following actions....
echo.
echo 1. Updating The computers name to: *******
echo 2. Updating The computers User Name to: *******
echo 3. And You Will Be Forceing A System Reboot: *******
echo.
echo 3. Press "Enter" To Continew Or Alt+F4 To Exit
echo.
echo ___________________________________________________________________________
echo ***************************************************************************
echo ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
pause
wmic useraccount where name='%username%' rename %newusername%
pause
This ended up fixing my error:
wmic useraccount where name='%USERNAME%' call rename name='%newusername%'
net.exe user "%newusername%" /fullname:"%newusername%"
Looks like I was missing the call and name= option.

Adding a local admin account to a device using a batch script and csv

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 ***...

Batch Script to replace line in an .ini

Please excuse me. I'm a complete noob.
I have 1 ini file with only 1 IP address and a version number.in this format:
xxx.xxx.xxx.xxx
abcde
I have 3 total IP's that I need to accommodate.
I'm looking to create a batch script that will ask a user for a location they'd like to connect to (each location would correspond to an IP address).
So for example: NY = 10.0.0.0 DC = 20.0.0.0 LA = 30.0.0.0
The batch would say "Where do you want to connect? 1= NY, 2= DC, 3= LA"
When a user selects 1 for "NY", the script will search the .ini file (which is always in the same location c:\sample) and change the IP to the correct one (10.0.0.0).
It would have an output of something like "You're now connected to NY!"
When a user selects "DC", the script will search the .ini file and change it to 20.0.0.0 etc.I'm able to do a simple find/replace script, but only with 2 IPs, and I had to make one for each location which was rather inconvenient.
Any help or guidance is much appreciated!
This example will:
Ask user to enter NY, DC or LA;
Create x.tmp which is a copy of x.ini, replacing all xxx.xxx... with argument;
Replace exisitng x.ini with x.tmp;
Code:
#Echo Off
:Begin
If Exist c:\sample\x.tmp Del c:\sample\x.tmp
Set /P "var=Choose location (NY, DC, LA):"
If /I "%var%"=="NY" Call :ReplaceIP 10.0.0.0
If /I "%var%"=="DC" Call :ReplaceIP 20.0.0.0
If /I "%var%"=="LA" Call :ReplaceIP 30.0.0.0
If Exist c:\sample\x.tmp (
Move /Y c:\sample\x.tmp c:\sample\x.ini 1>Nul
Echo Success!
) Else (
Echo Invalid option!
)
Pause
GoTo :Begin
:ReplaceIP
For /F "Tokens=1,2,3,4 Delims=." %%i In (c:\sample\x.ini) do If %%j.==. (
Echo %%i >> c:\sample\x.tmp
) Else (
Echo %1 >> c:\sample\x.tmp
)
GoTo :EOF
This should help you out.
#echo off
set "var="
set /p "var=Enter 1 for NY, 2 for DC or 3 for LA"
if "%var%" EQU 1 (
>c:\sample\file.ini echo 10.0.0.0
>>c:\sample\file.ini abcde
)
if "%var%" EQU 2 (
>c:\sample\file.ini echo 20.0.0.0
>>c:\sample\file.ini abcde
)
if "%var%" EQU 3 (
>c:\sample\file.ini echo 30.0.0.0
>>c:\sample\file.ini abcde
)
echo Good luck - I hope you entered the right number! :)

Batch file: save & load

I have one big problem. This app SHOULD ASK user for his name ONCE. It saves username and then when he run this app again 1st stage should be skipped and variable "name" should has his name from file with extension .user, but it does not work :/ Please Help!
#echo off
If NOT EXIST .settings md .settings
cd .settings
If NOT EXIST *.user goto abc
pause
If EXIST *.user set name=*.user
cd ..
pause
goto welcome
pause
:abc
cd ..
echo Hi! Welcome in my program!
set /p name=Tell me your name!
If NOT EXIST .settings md .settings
cd .settings
echo >>%name%.user
pause
:welcome
cls
echo Welcome %name%
pause
Other commands
EDIT: Pauses are only for me :)
You're just setting name to the string *.user. You need to parse the filename and extract just the first part.
Try:
If EXIST *.user (
for /f %%a in ('dir /b *.user') do (set name=%%~fa)
)
cd ..
Also, to just create a blank file, you can use
Type nul > %name%.user
It's not particularly clear what you are trying to do.
If fred has entered his name, this line:
echo >>%name%.user
will send the ECHO status (Echo is on/off) to the file fred.user
If you want the file .user to contain "fred" then you'd need one of
echo %name%>.user
>.user echo %name%
(personally, I'd use the second)
If you then want to read the user's name from the file .user then
set /p name=<.user
So all together, I'd try
#ECHO OFF
SETLOCAL
IF EXIST .user (SET /p name=<.user) ELSE (SET /p name=Your name ?)
>.user ECHO %name%
ECHO name is %name%
(I'll leave you to do the directory-manipulation)
Note that I've used > which means 'start a brand new filewhereas>>means 'append to an existing file or create a new file

Resources