Rename Windows network connections with PowerTool

Version 5

    When Windows enumerates its network connections upon its first boot, it sequentially names the NICs as they are found. Windows uses a generic name in the format of Local Area Connection x, where x is simply a sequential number. It is not uncommon, particularly in cloud environments to define four to ten NICs in a UCS configuration. It would be really nice if the first NIC I defined in the service profile was associated with the first NIC found by the operating system. However, that is not the way PCI enumeration works. PCI simply sends a request out and then enumerates devices as they respond. As a result, what generally happens is the order in which you defined your NICs in your service profile is never the same order as PCI enumerates them.

     

    In Windows you can use the Windows GUI interface to go into the properties of a NIC and find its MAC address. Then you can go into the UCS Manager console, expand the NICs for the service profile and find its MAC address. Then, back in Windows, you match the MAC addresses from the two sources and rename the network connection from Local Area Connection x to whatever you have named it in the UCS service profile. But this is a laborious and time-consuming process. When deploying many machines, in fact, it becomes very burdensome. Therefore, I wrote the following PowerTool script that quickly renames the Windows names to be the same as the UCS service profile names.

     

    <#

     

    Rename-UcsHyperVNICs.ps1

     

    Script to rename the NICs on the host to match the names on the Service Profile

     

    NOTE: There are some variables that need to be changed to reflect your environment.

         - change the IP address for accessing UCSM

     

           This script will work running between two systems that are in the same domain or two

           systems that are in workgroups.  It will not work across domain-workgroup.

     

           The following Windows firewall rules must be enabled on the target machine.

             COM+ Network Access (DCOM-IN)

             Windows Management Instrumentation (WMI-IN)

    #>

     

    # Import required modules

     

    if ((Get-Module |where {$_.Name -ilike "CiscoUcsPS"}).Name -ine "CiscoUcsPS")

         {

         Write-Host "Loading Module: Cisco UCS PowerTool Module"

         Import-Module CiscoUcsPs

         }

     

    $trash = set-ucspowertoolconfiguration -supportmultipledefaultucs $false

     

     

    ###  Variables to be tailored to customer environment ###

     

    $UcsmAddress = "10.29.130.100"

     

    # Connect to UCSM

     

    $ucsCreds = Get-Credential

    $UCSMHandle = Connect-Ucs $UcsmAddress $ucsCreds

     

     

    # Get Name of server to work on

     

    Write-Host "Enter server on which to rename default NIC names"

    Write-Host "The name of the server and the name of the UCS Service Profile must be the same"

    $Srvr = Read-Host "NOTE: Case must be EXACTLY the same as the UCS Service Profile"

    $Org = Read-Host "Enter Sub-Organization name of Service Profile, or 'root'"

    If ($org.Length -eq 0) {$org = "root"}

    $OrgLevel = Get-UcsOrg -Name $Org

    $SrvrProfile = $OrgLevel.DN + "/" + $Srvr

     

    # Retrieve table of NICs from the UCS Profile

     

    $UCSAdapters = Get-UcsVnic -ServiceProfile $SrvrProfile

     

    ForEach ($UcsA in $UcsAdapters) {

        $NICindex = (Get-WMIobject Win32_NetworkAdapterConfiguration -namespace "root\CIMV2" -computername $Srvr | Where-Object {$_.MACaddress -eq $UcsA.Addr}).Index

        $NIC = (Get-WMIobject Win32_NetworkAdapter -computername $Srvr | Where-Object {$_.Index -eq $NICindex})

        If ($NIC.NetconnectionID -ne $UcsA.name)

        {

            $tmp = $NIC.NetconnectionID ; $tmp_1 = $UcsA.Name

            Write-Host "Changing NIC $tmp to be named $tmp_1"

            $NIC.NetconnectionID=$UcsA.name

            $trash = $NIC.Put()

        }

    }

     

    Disconnect-Ucs

     

    I use this script all the time in my lab.  On new server builds, this works great.  However, it is possible to run into another problem.  If you have a Windows image that is used to boot a server, and you move that server's assigned service profile to another server with a different hardware configuration, you could end up with Windows reverting the network connection names back to Local Area Connection x.  (This will not happen if the service profile is moved from one server to a like server, e.g. one B200 M3 to another B200 M3 with the same processor, but if you move it to different server, say B200 to C220, it will definitely happen, or B200 M2 to B200 M3.  There are lots of variations that cause this to occur.)

     

    No problem, you say. Just re-run the above script and everything will be fine.  Well, that's not quite how it will work.  When an existing Windows image is used to boot a different piece of hardware, PCI will re-enumerate the devices, but it does not replace the existing device information that is contained in the Windows registry.  What this means is that character strings you used to rename your NICs the first time are still (tattooed) in the registry, even though the names you now see on the network connections are changed to Local Area Connection x.  So if you re-run the above script, it will error out because you are trying to rename a connection to a name that already exists in the registry.

     

    I have not incorporated the following hack into the above script to fix the problem, but here is a PowerShell script snippet that could be adapted to delete the existing names from the registry.  Then the above script could be run to rename to be the same as the service profile again.  CAUTION: Modifying the registry can cause problems that result in a system that is inoperable.  Proceed with caution.

     

    $Path = Get-Location

    $pathString = 'HKLM:\system\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}'

    Set-Location $pathString

     

    $vals = Get-ChildItem

    ForEach ($v in $vals)

    {

        $v1 = $v.Name

        $v1 = $v1.replace('HKEY_LOCAL_MACHINE', 'HKLM:')

        $sv = split-path $v1 -leaf -resolve

        if ($sv -ne 'Descriptions')

        {

            $v1 = $v1 + '\Connection'

            $v2 = Get-ItemProperty $v1

            If ($v2.Name -eq 'DeleteMe')

            {

                Remove-Item -Path $v2.PSParentPath -Recurse -Force

            }

        }

    }

     

    Set-Location $Path

     

    Note that the above snippet is not written to loop through the names of the service profile.  In fact, it is written just to test the basic code.  You would need to adapt it to the initial rename script if you wanted it to first check for the existence of a name before you try to rename it, or you could create a separate script that just does the registry deletions.

     

    This document was generated from the following discussion: Rename Windows network connections with PowerTool