cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4334
Views
0
Helpful
6
Replies

Rename Windows network connections with PowerTool

tcerling
Level 4
Level 4

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 that order in which you defined your NICs in your service profile is never in 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 can 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 lts of 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, different, server, 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 that you used to rename your NICs the first time are still 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.

6 Replies 6

hetalsoni
Level 1
Level 1

Great script, it works well.

1.     Would you please provide scripts to change NIC Card Description as well?

2.     I have a only three vNIC, is it possible to use different name in OS then using vNIC name from UCS service profile?

3.     Is it possible to add Staic IP to each vNIC in script?

Thanks for posting this useful script.

Thanks

Hetal Soni

1.  What are you calling the 'NIC Card Description'?  Until I know that, I don't know whether or not it is changeable.

2. Yes, you can set the netconnectionid to anything you want.  I use the name from the Service Profile because it is consistent, and it provides a nice tie back to the Service Profile.  You would need to come up with some other way to provide the relationship between the MAC address and the character string you want to use as as netconnectionid.

3. Yes, I have done that in other scripts.  Again, a method has to be defined as to how to come up with a fixed IP address to be mapped to the NIC.  In another script I used local IP addresses in the 192.168.x.y range, where x was defined by the VLAN associated with the NIC.  I pulled that information from the Service Profile, too.  I asked for a 'system ID' in the range of 2-250, or some such, and used that as the y value across all NICs.

PowerShell and PowerTool will allow you to do a lot of manipulation of the values associated with a NIC.  But the first thing you need to do is come up with a methodology that you can implement with the script.  After that, it's SMOP - small matter of programming. <grin>

tim

Thanks again for quick response.

1. Below is output from Windows IPconfig/all

Ethernet adapter NIC2:

   Connection-specific DNS Suffix  . :
   Description . . . . . . . . . . . : Cisco VIC Ethernet Interface

I would like to change above description(Cisco VIC Ethernet Interface) property as well.

2. I am trying to map service profile vNIC  and OS NIC name as below

Service Profile vNICs name: NIC1, NIC2, NIC3

OS NIC name:  HV, HA, Guest

3. I am trying to assign IP address for each vNIC as below

Service Profile vNICs name: NIC1, NIC2, NIC3

IP Assignment: 192.168.XXX.AAA, 192.168.YYY.AAA,DHCP

I would appeciate , if you please provide script for above situation, I am still learning powershell command.

Thanks for your help..

I'm sorry, but I can't write scripts for everybody that asks - I have a regular job, too. <grin>

Besides, digging these things out on your own helps you learn that much faster.  In the above script, when I set the netconnectionid, you see this command.

        $NIC.NetconnectionID=$UcsA.name

Now, there is logic in there to get the right information, but to change the name of the card in this logic, you would include an instruction like this:

        $NIC.Name=$UcsA.name

This is starting to get into the Get-WMIObject and calling various Win32 objects defined in the WMI model.  If you want to start doing things to devices in Windows, you will need to learn how to find the right class and what you can modify.  Bing is my favorite search engine.  Microsoft also has a forum for PowerShell - http://social.technet.microsoft.com/Forums/en-US/home?forum=winserverpowershell  I've gotten a number of questions answered there.  Again, they don't write scripts but they help you find your problems.

For the second item, in order to determine which is NIC1, NIC2, and NIC3, you still have to access the Service Profile.  Personally, I think it is a best practice to define the NICs in the Service Profile with a meaningful name instead of generic names.  That way someone coming along after you has an idea about what the NICs are for simply by looking at the Service Profile.  But, the logic I provided above is already finding the NICs in the Service Profile.  You simply have to substitute your names.  You could do this with a list of values.  But that is separating the naming from the Service Profile, so it is obfuscating the actual usage of the NIC.  Again, my recommendation is to rename the NICs in the SP so they reflect their purpose, and then the script works as is.

For your third item, here is the script I wrote to combine renaming the NICs and assigning fixed IP addresses.  You will need to change the logic to fit your needs.

$ucsIP     = "123.123.123.123"

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

# Connect to UCSM

$ucsCreds = Get-Credential
$UCSMHandle = Connect-Ucs $UcsIP $ucsCreds

Write-Host ""
Write-Host -ForegroundColor Yellow "Entered name of host must match case of service profile name"
$srvr = Read-Host "Enter the name of the Hyper-V host to target"
Write-Host ""
[int]$hostNum = Read-Host "Enter a numeric value between 1-254 to use as the host number"

Write-Host ""
Write-Host "Not all NICs should have their IP address altered, e.g. Mgmt and iSCSI boot NICs"
$in = Read-Host "Enter a comma separated list of NICs to ignore"
$in2 = $in -replace " ",""
$ignoreNic = $in2 -split ","

Write-Host ""
$org = Read-Host "Enter Sub-Organization name of Service Profile, or 'root'"
If ($org.Length -eq 0) {$org = "root"}
$orgLevel = Get-UcsOrg -Name $org
$svcProfile = $orgLevel.DN + "/" + $srvr
Write-Host ""

# Retrieve table of NICs from the UCS Profile

$ducsVnics = Get-UcsVnic -ServiceProfile $svcProfile
If ($ducsVnics.length -eq 0)
{
    Write-Host -ForegroundColor Red "Invalid Service Profile name - $svcProfile"
    Disconnect-Ucs
    Exit
}

# This is a special check to remove the dyanmic virtual function vNICs created by having VM-FEX defined
$ucsVnics = @()
Foreach ($d in $ducsVnics)
{
    If ($d.addr -ne "derived")
    {
        $ucsVnics +=, $d
    }
}

Write-Host "$srvr has the following vNICs"
$ucsVnics.Name

$vlans = Get-UcsLanCloud | Get-UcsVlan | Select Name, Id
$assignedIP = @()

# Rename the NICs on the server to match the NIC name of the service profile
# If NIC is not one entered to be ignored, change the IP address

ForEach ($u in $UcsVnics)
{
    $adapterConfig = (Get-WMIobject Win32_NetworkAdapterConfiguration -namespace "root\CIMV2" -computername $srvr | `
        Where-Object {$_.MACaddress -eq $u.Addr})
    $hostNic = (Get-WMIobject Win32_NetworkAdapter -computername $srvr | Where-Object {$_.Index -eq $adapterConfig.Index})
    If ($hostNic.NetconnectionID -ne $u.name)
    {
        $tmp = $hostNic.NetconnectionID ; $tmp_1 = $u.Name
        Write-Host "Changing NIC $tmp to be named $tmp_1"
        $hostNic.NetconnectionID=$u.Name
        $trash = $hostNic.Put()
    }
    $check = $FALSE

    Foreach ($ig in $ignoreNic)
    {
        If ($ig -eq $u.Name) {$check = $TRUE}
    }

    If (!$check)
    {
        Foreach ($v in $vlans)
        {
            If ($v.Name -eq $u.name)
            {
                $adapterConfig.DHCPenabled = $False
          $adapterConfig.SetDynamicDNSRegistration($false) | out-null
                $newIP = "192.168." + $v.Id + "." + $hostNum
                Foreach ($aIP in $assignedIP)
                {
                    If ($newIP -eq $aIP)
                    {
                        $octets = ($newIP.split("."))
                        [int]$lastOctet = $octets[3]
                        $lastOctet++
                        $newip = $octets[0] + "." + $octets[1] + "." + $octets[2] + "." + $lastOctet
                    }
                }
                $adapterConfig.enablestatic($newIP,"255.255.255.0") | out-null
                $assignedIP +=, $newIP
                Write-Host $u.Name "new IP > $newIP"
            }
        }
    }
}

Tim,

Thanks again for such a quick response.. It is great help...  Great scripts and excellent work...

Thnaks

tcerling
Level 4
Level 4

I've updated my initial script to make greater use of the PowerShell 4.0 capabilities that Microsoft has released with Windows Server 2012 R2 and Windows 8.1.  It works very similarly to the original script, except that it removes some of the arcane Win32 calls and replaces them with more understandable PowerShell cmdlets that were not previously available.

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 = "99.99.99.99"

# 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
$remAdapters = Invoke-Command -ComputerName $srvr {Get-NetAdapter}

ForEach ($ucsA in $ucsAdapters) {
    $ucsMac = $ucsA.Addr -replace ":", "-"
    ForEach ($remA in $remAdapters) {
        If ($ucsMac -eq $($remA.MacAddress))
        {
            If ($ucsA.Name -ne $remA.name)
            {
                $tmp = $($remA.ifDesc).Contains("Hyper-V Virtual Ethernet")
                If ($tmp -eq $false)
                {
                    $old = $remA.Name; $new = $ucsA.Name
                    Write-Host "Changing NIC $old to be named $new - MAC $($remA.MacAddress)"
                    Invoke-Command -ComputerName $srvr {param($old, $new)Rename-NetAdapter -Name $old -NewName $new} -args $old,$new
                    break
                }
            }
        }
    }
}


Disconnect-Ucs

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Cisco UCS X-Series Energy Efficiency Offer