Rename Hyper-V NICs

Document created by tcerling on Apr 7, 2014
Version 1Show Document
  • View in full screen mode

UCS Power Scripting Submission Form


UCS Communities Login ID: tcerling

Twitter handle (optional): <Name>

Have you read the Official Rules of the Contest and do you accept the terms and conditions     Yes  [ X ]    No  [   ]

Are you a Cisco employee                                                                                                                          Yes  [ X ]    No  [   ]

Does the script run on an emulator -                                                                                                          Yes [ X ]    No  [   ]

          If yes which version? _______________



Include your script here:




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

# Connect to UCSM
Write-Host -ForegroundColor DarkYellow "`nEnter credentials for UCS Manager`n"
$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 "`nNOTE: Case must be EXACTLY the same as the UCS Service Profile"
$Org = Read-Host "`nEnter 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 $
                $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