Batch Processing of automatically changing IP \ MACHINE name \ Workgroup for Network Management

Source: Internet
Author: User
Tags dns2 echo display

I have taken charge of hundreds of machines in my department, and I have used many methods to restore the original configuration after I reinstall it:
1: manually change the name.
2: Use the netsh dump + file and netsh-F + file to change the format, but it is useless to format the entire disk.
3: Use a dedicated small change IP Address Program But you still need to manually specify the IP

Finally, I thought of the following methods:
1: First implement: netsh interface IP to change IP
2: Use WMIC computersystem to modify the workgroup
3: Use Reg add to change the machine name
4: Because it takes effect only after the machine name is changed, restart with: reboot. INF.
5: the configuration of the new machine name and IP address is added later. The machine name is obtained using the serial number of the machine (Lenovo: wmic bios get serialnumber). The last one of the IP addresses is random.
Because the serial number can be read through WMIC, it is much easier to register the network configuration of the local machine.
6: Finally, put the corresponding table such as the sorted IP address into bat to prevent bat from calling other files.
7: After the above functions are implemented, I can encapsulate the BAT file into the gho file. After the restart, I can automatically set the file. I don't want to perform other operations, so users can use it. if necessary, you can add and automatically delete itself after execution.

the entire Code is as follows: (You are welcome to continue optimizing and organizing) copy Code the code is as follows: @ echo off & code by m13632750052
for/F "tokens = 12 delims =" % I in ('ipconfig/All ^ | find "physical address "') do set localmac = % I
echo uses the MAC address % localmac % to find the corresponding machine name
for/F "Skip = 90 tokens = 1, 2, 3, 4, delims = "% I in (% ~ F0) do (
If [% K] = [% localmac %] Set Name = % I
If [% K] = [% localmac %] set IP = % j
If [% K] = [% localmac %] Set MAC = % K
If [% K] = [% localmac %] Set workgroup = % L
set mask = 255.255.0.0
set gway = 168.30.1.233
set DNS = 168.1.4.147
set dns2 = 168.1.6.243
)
echo display query result code by CN-dos breakme
if defined name (echo % name % IP % Mac % workgroup %) else (goto newset)
: ente Rset
for/L % I in (3,-1, 0) Do ping-N 2 127.0.0.% I> NUL & @ echo. change the machine name after % I s ......

Reg Add "HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ shellnoroam"/V @/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Control \ computername"/V "computername"/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Control \ computername \ activecomputername"/V "computername"/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Services \ EventLog"/V "computername"/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Control \ computername"/V "computername"/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Services \ Tcpip \ Parameters"/V "NV hostname"/T REG_SZ/D "% name %"/F
Reg Add "HKLM \ System \ CurrentControlSet \ Services \ Tcpip \ Parameters"/V "hostname"/T REG_SZ/D "% name %"/F

Echo refresh the Registry
If exist c: \ WINNT (SeCEdit/refreshpolicy machine_policy/enforce) else (gpupdate/force)

Echo modify workgroup
WMIC computersystem where name = "% name %" Call joindomainorworkgroup name = "% workgroup %"
WMIC computersystem where name = "% computername %" Call joindomainorworkgroup name = "% workgroup %"

Echo IP address Modification
: Find the connection name corresponding to the Mac
For/F "tokens = 1, 2, 4, 5, 6 delims =-" % A in ("% Mac %") do set macwmic = % A: % B: % C: % d: % E: % F
For/F "Skip = 2 tokens = 2 * delims =" % A in ('wmic Nic where "macaddress = '% macwmic %' and netconnectionid! = NULL "Get netconnectionid/value') do set" connname = %"
If not defined connname set connname = "Local Connection"
Netsh interface IP Set address name = "% connname %" Source = static ADDR = % IP % mask = % mask %
Netsh interface IP Set address name = "% connname %" Gateway = % gway % gwmetric = 0
Netsh interface IP Set DNS name = "% connname %" Source = static ADDR = % DNS % register = primary
Netsh interface IP add DNS name = "% connname %" ADDR = % dns2 % Index = 2
Netsh interface IP Set wins name = "% connname %" Source = static ADDR = none

Echo ARP binding % IP % Mac %
ARP-d
ARP-S % IP % Mac %
Echo ARP is bound to the gateway % gway %. If the network is disconnected, the default gateway Mac: 00-00-5e-00-01-01 will be used.
Ping-N 2% gway %> NUL 2> NUL
If [% errorlevel %] = [1] (Set destination MAC = 00-00-5e-00-01-01) else (
For/F "Skip = 3 tokens = 2" % H in ('Arp-A % gway % ') do set hosts MAC = % H
)
CMD/c arp-S % gway % running Mac %

For/L % I in (3,-) Do ping-N 2 127.0.0.% I> NUL & @ echo. % I s and restart your computer ......
Setlocal
CD/d % Temp %
Echo [version]> Reboot. inf
Set INF = installhinfsection defaultinstall
Echo Signature = $ Chicago $> Reboot. inf
Echo [defainstall install]> Reboot. inf
Rundll32 setupapi, % INF % 1% TEMP % \ reboot. inf
Del reboot. inf
Exit

: Newset
Echo your machine may be a new machine and you need to manually enter an IP address
For/F "tokens = 1, 2 delims =" % I in ('wmic BIOS get serialnumber/value ^ | find "=" ') do set name = % J
If not defined name set/P name = enter the name of the new machine
Echo machine name: % name %
Call: Select "physical address" "Mac"
Echo Mac: % Mac %
Set/a aa = % random % 9
Set IP = 168.30.7.1% Aa %
Set mask = 255.255.0.0
Set gway = 168.30.1.233
Set DNS = 168.1.4.147
Set dns2 = 168.1.6.243
Set workgroup = vga00l
Echo IP configuration: % IP % mask % gway % DNS % dns2 % workgroup %
For/L % I in (5,-) Do ping-N 2 127.0.0.% I> NUL & @ echo. % I s and then modify the machine name ......
Goto enterset

: Select
For/F "tokens = 2 delims =:" % I in ('ipconfig/All ^ | findstr/I/c: % 1') do if not "! % ~ 2! "=" "Set" % ~ 2 = % I"
Goto: EOF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.