Align Windows Network Adapter Names to vSphere NIC Network-Labels

Version 3

    Tested with:

    • Windows 2012 R2
    • VMware ESXi 5.5 U2
    • VMware vCenter 5.5 U2
    • VMware PowerCLI 6.0 Release 2 build 3056836

     

    Example Output:

    ExampleOutput.png

     

    if (!(get-pssnapin -name VMware.VimAutomation.Core -erroraction silentlycontinue)) {

      add-pssnapin VMware.VimAutomation.Core

    }

     

    $i = 1

    $GCred = Get-Credential

     

    #Change VM Name here

    $VMtoProcess = Get-VM -Name MyTestVM

     

    Write-Host -foregroundcolor DarkGreen -backgroundcolor white "vSphere Network Config of the VM:"

    $VMtoProcess | Get-NetworkAdapter | Select Parent, NetworkName, MacAddress | ft -AutoSize

    Write-Host -foregroundcolor DarkGreen -backgroundcolor white "NIC count of the VM:"

    $NICcount = ($VMtoProcess | Get-NetworkAdapter).count

    $NICcount

     

     

    while($i -le $NICcount){

        $MacAddress = ($VMtoProcess | Get-NetworkAdapter -Name *$i).MacAddress

        $NetworkName = ($VMtoProcess | Get-NetworkAdapter -Name *$i).NetworkName

        $MacAddressWIN = $MacAddress -replace ":", "-"

        $script = 'Get-NetAdapter | Where-Object {$_.MacAddress -eq "'  + $MacAddressWIN + '"} | Rename-NetAdapter -NewName ' + $NetworkName + ' -PassThru'

        Write-Host -foregroundcolor DarkGreen -backgroundcolor white "InGuest Script to process:"

        $script

        $VMtoProcess | Invoke-VMScript -ScriptText $Script -GuestCredential $GCred -ScriptType Powershell | Out-Null

        $i++

    }

    $script = 'Get-NetAdapter | Select Name, MacAddress | ft -AutoSize'

    Write-Host -foregroundcolor DarkGreen -backgroundcolor white "New InGuest NIC Config:"

    $VMtoProcess | Invoke-VMScript -ScriptText $Script -GuestCredential $GCred -ScriptType Powershell