Link to home
Start Free TrialLog in
Avatar of gneville
gneville

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Dan Muzrall
Dan Muzrall
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gneville
gneville

ASKER

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?
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.
May I ask why don't you use DHCP for your first part?

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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
Remember to change
set $DEF_NIC_NAME="Connessione alla rete locale (LAN)"
 to
set $DEF_NIC_NAME="Local Area Connection"


I left the italian  ....
@ 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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, I never tried NewSID.exe on W7
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.
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.