共用方式為


How to set a static IP address and rename a NIC based on a known MAC address

I had a question that I thought I would share the answer for.

A customer was deploying multiple identical servers with multiple NIC into a testing lab as virtual machines. They needed a way to beat the plug and play detection of NIC cards so that they could set the correct static IP for the NIC which is “patched” to a virtual NIC port. The only static information they could use was giving all the identical, and isolated VMs the same MAC addresses from within Hyper-V

In each identical VM (VM Guest 1, 2, 3 in the picture below), there are 4 NICs. 1 NIC is enabled for DHCP with a Hyper-V dynamic MAC address. The other 3 NICs have 1 of 3 known MAC addresses. The 3 NICs with known static MAC addresses all need static IP addresses. All the servers which share static MAC addresses must also share static IP addresses. And the name of NIC must be changed to make it clear in the VMs installation of RRAS (and to the administrators) which NIC is patched to which Hyper-V network. In this way the servers have identical, non-overlapping networks for administrators to test on – and one additional network where all the VMs can contact each other for sharing files.

Lab Hyper-V NIC Setup

The routine was this:

  1. Set the Static IP address based on the known MAC address
  2. Rename the NIC based on the known MAC address
  3. Rename the DHCP NIC based on the fact it is NOT one of the known MAC addresses

Step 1

wmic nicconfig where MACAddress=”00:12:34:56:78:9A” call EnableStatic ("1.2.3.4"), ("255.255.255.0")

Step 2

wmic /output:NICNameUNICODE.txt nic where MACAddress="00:12:34:56:78:9A" get NetConnectionID /FORMAT:LIST

type NICNameUNICODE.txt > NICName.txt

for /F "skip=2 tokens=1,2 delims==" %%i IN (NICName.txt) do netsh interface set interface name="%%j" newname="Some Name"

We have to output WMIC to the text file instead of piping as it pipes with a <CR> at the end of each line, instead of a <CRLF>, which breaks the coming FOR /F command.

But WMIC saves the resulting file as Unicode format, which the FOR /F cannot read, so we run this through TYPE to get the output formatted as UNICODE.

The resulting NICName.txt looks like this:

image

There was also 1 additional NIC installed which did not have a static MAC address assigned by Hyper-V, and was enabled for DHCP. This NIC also needed renaming:

Step 3

wmic /output:DHCPNameUNICODE.txt nic where “MACAddress!=’00:12:34:56:78:9A AND MAC!=’00:12:34:56:78:9B’ AND MACAddress!=’00:12:34:56:78:9C’ AND AdapterType=’Ethernet 802.3’" get NetConnectionID /FORMAT:LIST

type DHPNameUNICODE.txt > DHCPName.txt

for /F "skip=2 tokens=1,2 delims==" %%i IN (DHCPName.txt) do netsh interface set interface name="%%j" newname="DHCP LAN"

 

I hope this helps someone one day with their deployments.

Comments

  • Anonymous
    July 06, 2011
    Really usefull info! , have not quite got this working as i want yet, but it has sent me in the right direction! thanks for posting this!

  • Anonymous
    April 24, 2013
    Excellent info!

  • Anonymous
    July 02, 2015
    Utilitary. Thanks!