batch file to set IP address and join domain - prompting for username & pw

Can anyone help with this batch file.  I'd like it to

- Prompt for user to input a pre-determined IP address
- Promt for 1 of 2 gateways

(the bit i'm struggling with:)
- Join domain, asking for username and password in order to do so
@ECHO OFF
set varsm=###.###.###.###
set vargw1=###.###.###.###
set vargw2=###.###.###.###
set vardns1=###.###.###.###
set vardns2=###.###.###.###
set vardns3=###.###.###.###
set vardns4=###.###.###.###
       
EcHO Setting IP Address and Subnet Mask
ECHO.
ECHO.
Echo "Enter static IP of machine as per hardware spreadsheet"
Set /p StaticIP=

netsh int ip set address name = "Local Area Connection" source = static addr = %StaticIP% mask = %varsm%

ECHO Setting Gateway

echo Choice
echo.
echo 1 ###.###.###.###
echo 2 ###.###.###.###

:choice
set /P C=[1,2]?
if "%C%"=="1" goto two
if "%C%"=="2" goto twenty

:two
netsh int ip set address name = "Local Area Connection" gateway =###.###.###.### gwmetric =0

:twenty
netsh int ip set address name = "Local Area Connection" gateway =###.###.###.### gwmetric =1
:end

ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%

ECHO Setting Secondary DNS

netsh int ip add dns name = "Local Area Connection" addr = %vardns2%
netsh int ip add dns name = "Local Area Connection" addr = %vardns3%
netsh int ip add dns name = "Local Area Connection" addr = %vardns4%
    
ECHO Here are the new settings for %computername%:
Echo.
netsh int ip show config
Echo next change IP address and join domain
Echo "Enter PC name of machine as per hardware spreadsheet"
Set /p computername=
netdom.exe join %computername% /Domain:###.###.##.## /UserD:%username% /PasswordD:%password%

pause

Open in new window

gnevilleAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Dan MuzrallConnect With a Mentor EHS SpecialistCommented:
One issue that you may be facing is that %computername% is already a system variable.  Consider changing the name of the variable to something like %pcname%

Also, you may want to try using SET /P to acquire the username and password.  Again, %username% is already a system variable, so you will probably want to change that variable name as well...something like %uname% should work.

See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true for a list of Windows Command Line Variables.
0
 
gnevilleAuthor Commented:
Thanks for the reply - I tried changing the variables but I'm still getting an error as if the syntax of the netdom command is wrong. . any suggestions?
0
 
Dan MuzrallEHS SpecialistCommented:
From the bottom of http://www.petri.co.il/join-a-domain-in-windows-7.htm

Syntax should be:
netdom join %computername% /domain:DOMAIN.COM /userd:DOMAIN\administrator /passwordd:P@ssw0rd

Notes: Replace DOMAIN.COM and DOMAIN with your correct domain name, and of course, enter the proper user credentials. Also note there's an additional "d" in "user" and "password", that is NOT a typo.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ReneGeCommented:
May I ask why don't you use DHCP for your first part?

Thanks
0
 
subhashchyConnect With a Mentor Commented:
If your script is asking for user credentilas then i guess you should add local user name and pass word as well in your script like below...
NETDOM JOIN <computername> /Domain:<FQDN of domain> /UserD:<username of domain admin account> /PasswordD:* /UserO:<Local admin account> /PasswordO:* /REBoot:10 /verbose

0
 
canaliCommented:
I changed an old script to fit your request (I haven't the two gateways an only 2 dns)
Bye Gastone

 
::-----------------------------------------------------------------------------
::-----------------------------------------------------------------------------
:: set-ip.cmd
:: change ip / mask / gw / interface 
:: for the interface $NIC_NAME 
:: with 4 parameters "ip" "mask"  "gw"  "interface name"
:: start in  non interactive mode
::   Ex.: set-ip "17.24.16.99" "255.255.255.0" "17.24.16.254"  "Connessione alla rete locale (LAN)"
::   Ex.: set-ip "17.24.16.99" "255.255.255.0" "17.24.16.254"  "Local Area Connection"
::TODO 
:: check entered values
:: interface with different name  ex.: "Local Area Connection 2"    
:: NEED: findstr.exe
:: Gastone Canali - novembre 2009 v. 1.3
:: Gastone Canali - maggio   2011 v. 2.0
@echo off
setlocal
title=%~n0
:: *********************** set default predeterminated ip  ********************
set $DEF_NIC_NAME="Connessione alla rete locale (LAN)"
set $DEF_ADDR=17.24.16.123
set $DEF_MASK=255.255.255.0
set $DEF_GW=17.24.16.254
set $DEF_GW1=17.24.16.1
set $DEF_DNS1=17.24.8.1
set $DEF_DNS2=17.24.8.2
set $DEF_DNS3=17.24.8.3
set $DEF_DNS4=17.24.8.4
:: *********************** end setting  predeterminated values  ***************

:: --- if four parameters are present
:: --- start in non interactive mode
if +%4+==++ goto :_FILL_IP
 set $ADDR=%~1
 set $MASK=%~2
 set $GW=%~3
 set $NIC_NAME="%~4"
 set $DNS1=%$DEF_DNS1%
 set $DNS2=%$DEF_DNS2%
 set $DNS3=%$DEF_DNS3%
 set $DNS4=%$DEF_DNS4%
Goto :_GO

:_FILL_IP
 cls
 set $NIC_NAME=%$DEF_NIC_NAME%
 set $DNS1=%$DEF_DNS1%
 set $DNS2=%$DEF_DNS2%
 set $DNS3=%$DEF_DNS3%
 set $DNS4=%$DEF_DNS4%
 call :_ASKFOR "$ADDR"  "%$DEF_ADDR%"   "Enter ip Address? [%$DEF_ADDR%]" 
 call :_ASKFOR "$MASK"  "%$DEF_MASK%"   "Enter the subnetmask? [%$DEF_MASK%]"
 call :_ASKFOR "$G"     "1" "Gateway: 1 for %$DEF_GW% or 2 for %$DEF_GW1%? [1]"
 if +%$G%+==+1+ ( set $GW=%$DEF_GW% & set $GWM=0
  ) else (
    set $GW=%$DEF_GW1% & set $GWM=1
 )
 call :_ASKFOR "$USER"     "user"          "Enter the USERNAME? [user]"
 call :_ASKFOR "$PASSOWRD" "password"      "Enter the password? [password]"

 rem call :_ASKFOR "$GW"       "%$DEF_GW%"     "Enter the gateway? [%$DEF_GW%]"
 rem call :_ASKFOR "$DNS1"     "%$DEF_DNS1%"   "Enter the DNS1? [%$DEF_DNS1%]" 
:_GO
 call :_DISPLAY_IP
 call :_ASKFOR_APPLY_IP
rem "APPLY"
 call :_JOIN_AND_EXIT "%$USER%" "%$PASSOWRD%"

Goto :_END
::-----------------------------------------------------------------------------
::-----------------------------------------------------------------------------

:_DISPLAY_IP
 cls
 Echo.
 Echo ----------------------------------
 Echo ^  IP address   :  %$ADDR%
 Echo ^  Netmask      :  %$MASK%
 Echo ^  dns          :  %$DNS1%
 Echo ^  Gateway      :  %$GW%
 Echo ----------------------------------
 Echo ^  User for join:  %$USER%
 Echo ----------------------------------
 Echo.
Goto :EOF

:_ASKFOR_APPLY_IP
 if /I +%~1+==+APPLY+ (
  call  :_SET_IP 
  goto :EOF
  )
 Echo Apply configuration [Yes,no,restart]
 Set /p $GO=Go^? ^[Y^|n^|r]
 if /I +%$GO%+==++  goto :_SET_IP
 echo %$GO%|findstr /i "y" >NUL && goto  :_SET_IP
 echo %$GO%|findstr /i "r" >NUL && goto  :_FILL_IP
 echo %$GO%|findstr /i "q" >NUL && exit /b
Goto :EOF

:_SET_IP
 Echo.
 netsh int ip set address name=%$NIC_NAME% source=static addr=%$ADDR% mask=%$MASK%
 netsh int ip set address name=%$NIC_NAME% gateway =%$GW% gwmetric =%$GWM%
 netsh int ip set dns name=%$NIC_NAME% source=static addr=%$DNS1% register=PRIMARY
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS2%
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS3%
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS4%
 rem ipconfig
Goto :EOF

:_ASKFOR
 set %~1=%~2
 Set /p %~1=%~3 
Goto :EOF

:_JOIN_AND_EXIT
 rem %1 user %2 password
 netdom join   %computername%  /d:mydomain.dom.uni.it /UD:%~1 /PD:%~2
 if %errorlevel% EQU 0 (echo Join OK & echo  Join OK ) else ( echo  Join KO )
 exit 
Goto :EOF

:_END

Open in new window

0
 
canaliCommented:
If u don't want the question  "Apply configuration [Yes,no,restart]"
change the line 65 adding the "APPLY"
call :_ASKFOR_APPLY_IP "APPLY"


Bye
Gastone
0
 
canaliCommented:
Remember to change
set $DEF_NIC_NAME="Connessione alla rete locale (LAN)"
 to
set $DEF_NIC_NAME="Local Area Connection"


I left the italian  ....
0
 
gnevilleAuthor Commented:
@ canali - Thats great it's about 90% perfect!

I get a few errors when I run the batch, but crucially, can you edit it to ask for the PC name before joining to domain please?

The errors:

Enter ip Address? [129.11.20.117] 129.11.20.162
Enter the subnetmask? [255.255.255.0] 255.255.255.0
'_APPLY_IP' is not recognized as an internal or external command,
operable program or batch file.
Apply configuration [Yes,no,restart]
Go? [Y|n|r]

Open in new window

0
 
ReneGeConnect With a Mentor Commented:
You can ask a question then define a variable like this:

ECHO Please enter ip Address
SET /p ipAddress=

Also, Don't forget that if you try to joyn a PC to a domain that was built with an image, you may need to change it's SID.

newsid.exe /a /n %NewComputerName%

NewSID.exe is a freeware:
http://technet.microsoft.com/en-us/sysinternals/bb897418

http://www.google.ca/#hl=en&source=hp&biw=1599&bih=675&q=newsid&oq=newsid&aq=f&aqi=g5g-s1g4&aql=&gs_sm=e&gs_upl=1117l2147l0l6l5l0l0l0l0l150l443l3.2&fp=d5ad23689e277677
0
 
ReneGeCommented:
By the way, I never tried NewSID.exe on W7
0
 
ReneGeCommented:
NewSID is not supported on Windows Server 2008 R2 and Windows7. To change SID, use sysprep.
http://www.brajkovic.info/virtualization/using-newsid-to-change-sid/

I may be out of course here but I felt like sharing.

PS: I don't expect points for my so far possible contribution.
0
 
gnevilleAuthor Commented:
From what I can gather, the PC name change requires a reboot.  Is that even possible with batch files - to execute some commands,  reboot, then execute futher commands?
0
 
canaliConnect With a Mentor Commented:
try it now


::-----------------------------------------------------------------------------
::-----------------------------------------------------------------------------
:: set-ip.cmd
:: change ip / mask / gw / interface 
:: for the interface $NIC_NAME 
:: with 4 parameters "ip" "mask"  "gw"  "interface name"
:: start in  non interactive mode
::   Ex.: set-ip "17.24.16.99" "255.255.255.0" "17.24.16.254"  "Connessione alla rete locale (LAN)"
::   Ex.: set-ip "17.24.16.99" "255.255.255.0" "17.24.16.254"  "Local Area Connection"
::TODO 
:: check entered values
:: interface with different name  ex.: "Local Area Connection 2"    
:: NEED: findstr.exe
:: Gastone Canali - novembre 2009 v. 1.3
:: Gastone Canali - maggio   2011 v. 2.0
::                                v. 2.01
@echo off
setlocal
title=%~n0
:: *********************** set default predeterminated ip  ********************
set $DEF_NIC_NAME="Local Area Connection"
set $DEF_ADDR=17.24.16.123
set $DEF_MASK=255.255.255.0
set $DEF_GW=17.24.16.254
set $DEF_GW1=17.24.16.1
set $DEF_DNS1=17.24.8.1
set $DEF_DNS2=17.24.8.2
set $DEF_DNS3=17.24.8.3
set $DEF_DNS4=17.24.8.4
:: *********************** end setting  predeterminated values  ***************

:: --- if four parameters are present
:: --- start in non interactive mode
if +%4+==++ goto :_FILL_IP
 set $ADDR=%~1
 set $MASK=%~2
 set $GW=%~3
 set $NIC_NAME="%~4"
 set $DNS1=%$DEF_DNS1%
 set $DNS2=%$DEF_DNS2%
 set $DNS3=%$DEF_DNS3%
 set $DNS4=%$DEF_DNS4%
Goto :_GO

:_FILL_IP
 cls
 set $NIC_NAME=%$DEF_NIC_NAME%
 set $DNS1=%$DEF_DNS1%
 set $DNS2=%$DEF_DNS2%
 set $DNS3=%$DEF_DNS3%
 set $DNS4=%$DEF_DNS4%
 call :_ASKFOR "$ADDR"  "%$DEF_ADDR%"   "Enter ip Address? [%$DEF_ADDR%]" 
 call :_ASKFOR "$MASK"  "%$DEF_MASK%"   "Enter the subnetmask? [%$DEF_MASK%]"
 
 call :_ASKFOR "$G"     "1" "Gateway: 1 for %$DEF_GW% or 2 for %$DEF_GW1%? [1]"
 if +%$G%+==+1+ ( set $GW=%$DEF_GW% & set $GWM=0
  ) else (
    set $GW=%$DEF_GW1% & set $GWM=1
 )
 call :_ASKFOR "$NEWPCNAME"  "pcname"        "Enter the new pc NAME? [pcname]"
 call :_ASKFOR "$USER"       "user"          "Enter the USERNAME? [user]"
 call :_ASKFOR "$PASSOWRD"   "password"      "Enter the password? [password]"

 rem call :_ASKFOR "$GW"       "%$DEF_GW%"     "Enter the gateway? [%$DEF_GW%]"
 rem call :_ASKFOR "$DNS1"     "%$DEF_DNS1%"   "Enter the DNS1? [%$DEF_DNS1%]" 
:_GO
 call :_DISPLAY_IP
 call :_ASK_APPLY_IP
rem "APPLY"
 call :_JOIN_AND_EXIT "%$USER%" "%$PASSOWRD%"

Goto :_END
::-----------------------------------------------------------------------------
::-----------------------------------------------------------------------------

:_DISPLAY_IP
 cls
 Echo.
 Echo ----------------------------------
 Echo ^  IP address   :  %$ADDR%
 Echo ^  Netmask      :  %$MASK%
 Echo ^  dns          :  %$DNS1%
 Echo ^  Gateway      :  %$GW%
 Echo ----------------------------------
 Echo ^  User for join:  %$USER%
 Echo ----------------------------------
 Echo ^  New pcname   :  %$NEWPCNAME%
Goto :EOF

:_ASK_APPLY_IP
 if /I +%~1+==+APPLY+ (
  call  :_SET_IP 
  goto :EOF
  )
 Echo Apply configuration [Yes,quit,restart]
 Set /p $GO=Go^? ^[Y^|q^|r]
 if /I +%$GO%+==++  goto :_SET_IP
 echo %$GO%|findstr /i "y" >NUL && goto  :_SET_IP
 echo %$GO%|findstr /i "r" >NUL && goto  :_FILL_IP
 echo %$GO%|findstr /i "q" >NUL && exit 
Goto :EOF

:_SET_IP
 Echo.
 netsh int ip set address name=%$NIC_NAME% source=static addr=%$ADDR% mask=%$MASK%
 netsh int ip set address name=%$NIC_NAME% gateway =%$GW% gwmetric =%$GWM%
 netsh int ip set dns name=%$NIC_NAME% source=static addr=%$DNS1% register=PRIMARY
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS2%
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS3%
 netsh int ip add dns name=%$NIC_NAME% addr=%$DNS4%
 rem ipconfig
Goto :EOF

:_ASKFOR
 set %~1=%~2
 Set /p %~1=%~3 
Goto :EOF

:_JOIN_AND_EXIT
 rem %1 user %2 password
 netdom join    %$NEWPCNAME%  /d:mydomain.dom.uni.it /UD:%~1 /PD:%~2
 if %errorlevel% EQU 0 (echo Join OK & echo  Join OK ) else ( echo  Join KO )
 exit 
Goto :EOF

:_END

Open in new window

0
 
gnevilleAuthor Commented:
Thanks for that canali, but I don't think that will work.  You have to change the PC name before joining to domain - right?

netdom join    %$NEWPCNAME%  /d:mydomain.dom.uni.it /UD:%~1 /PD:%~2
- won't work because at that time the pc name will still be the default?
0
 
canaliCommented:
Right! I've changed without thinking.
netdom join    %computername%  /d:mydomain.dom.uni.it /UD:%~1 /PD:%~2

For the reboot and continue is possible:
 -join the pc
 -Enable Autologon (http://support.microsoft.com/kb/315231)
 -create a rename pc batch ex: renamepc.cmd (netdom)
    - renamepc
         netdom rename
         disable autologon
         remove shortcut in AllUsersStartup
         reboot
 -create a  shortcut of renamepc.cmd in AllUsersStartup
 -reboot
 
Bye Gastone
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.