Solved

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

Posted on 2011-02-16
18
2,043 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:gneville
  • 5
  • 4
  • 4
  • +3
18 Comments
 
LVL 8

Accepted Solution

by:
moonie42 earned 125 total points
ID: 34906227
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
 

Author Comment

by:gneville
ID: 34907311
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
 
LVL 8

Expert Comment

by:moonie42
ID: 34909495
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 10

Expert Comment

by:ReneGe
ID: 35338333
May I ask why don't you use DHCP for your first part?

Thanks
0
 
LVL 8

Assisted Solution

by:subhashchy
subhashchy earned 125 total points
ID: 35380119
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
 
LVL 14

Expert Comment

by:canali
ID: 35509148
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
 
LVL 14

Expert Comment

by:canali
ID: 35509166
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
 
LVL 14

Expert Comment

by:canali
ID: 35706605
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
 

Author Comment

by:gneville
ID: 35718777
@ 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
 
LVL 10

Assisted Solution

by:ReneGe
ReneGe earned 125 total points
ID: 35718842
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35718882
By the way, I never tried NewSID.exe on W7
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35718907
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
 

Author Comment

by:gneville
ID: 35718960
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
 
LVL 14

Assisted Solution

by:canali
canali earned 125 total points
ID: 35729166
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
 

Author Comment

by:gneville
ID: 35729887
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
 
LVL 14

Expert Comment

by:canali
ID: 35733358
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
 
LVL 69

Expert Comment

by:Qlemo
ID: 35944356
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question