VMware Cloud Community
joe_cruz
Contributor
Contributor

Script to re-create the VMkernel port group on ESXi servers

Hey, folks.

We recently discovered that one of our ESXi 4.1 U2 servers had a duplicate MAC address with one of our physical servers, causing vMotion timeouts.  Nevermind that this should be next to impossible to happen!

What we discovered is that the Port Group created at setup for the VMkernel and VMotion was assigned a physical MAC address from Dell/Intel (depending on the on-board NIC).

It turns out that if you re-create the Port group, it is assigned a VMware MAC address, as is expected.  Re-creating the VMkernel group isn't that straightforward, so (as suggested by @shogan85) I'm posting it here for folks to learn from.

I don't claim to be a POSH expert, so you shoudl definitely test this script extensively before deploying into your environment.  Feel free to respond if you have any questions about the hows or the whys!

Smiley Happy

Joe

jcruz@wharton.upenn.edu

http://beacon.wharton.upenn.edu/joecruz

$SnapinTest = Get-PSSnapin | Select-String "vmware"
if ($SnapinTest -eq $null) { Add-PSSnapin *vm* }

#which VCenter are we connectin to?
$vCenterServer = Read-Host "Which vCenter are we Connecting To?"

#Get Hosts to work on
Connect-VIserver $vCenterServer
$vmhosts = Get-VMHost
$vmhosts = $vmhosts | % {$_.Name -replace "-m.wharton.private", ""} | sort-object
Disconnect-VIServer $vCenterServer -confirm:$false

#DNS Lookup FUnction
Function forward_dns
{
    $cmd = "nslookup " + $args[0] + " " + $ns
    $result = Invoke-Expression ($cmd)
    trap
    {
        $global:controlladns = $true
        $global:solved_ip = "No record found"
    continue
    }
        $global:controlladns = $false
        $global:solved_ip = $result.SyncRoot[4]
    if (-not $global:controlladns)
    {
        $leng = $global:solved_ip.Length -10
        $global:solved_ip =
        $global:solved_ip.substring(10,$leng)
    }
}

$root_pwd = ""
$placeholderIP = '10.0.1.6'

$vmhosts | % {

    $lowerstring = $_.ToLower()
    
    $first=$lowerstring[0]
    $length=$lowerstring.length
    $last=$lowerstring[($length-1)]
    $lastletter=$length-1

    $hostname = $lowerstring + "-m"

    forward_dns $hostname

    $OriginalHostIP = $solved_IP

    $password = $root_pwd + $lowerstring[0] + $lowerstring[$lastletter] + $length
   
    Connect-VIServer $hostname -user root -password $password
   
    #Create Placeholder vmKernel network
    $vmhost = Get-VMHost $hostname
    $vswitch = Get-VirtualSwitch -VMHost $vmhost -Name vSwitch0
   
    New-VMHostNetworkAdapter -vmhost $vmhost -PortGroup "VMkernel Placeholder" -VirtualSwitch $vswitch -VMotionEnabled $true -IP $placeholderIP -SubnetMask 255.255.248.0 -ManagementTrafficEnabled $true
    Get-VirtualPortGroup -VMHost $vmhost -Name "VMkernel Placeholder" | Set-VirtualPortGroup -VLanId 4
   
    #remove original bogus vmKernel network
    $vkernelAdapter = Get-VMHostNetworkAdapter | ? {$_.IP -like $OriginalHostIP}
    Remove-VMHostNetworkAdapter $vkernelAdapter -confirm:$false
   
    #connect to Placeholder IP address
    Disconnect-VIServer $hostname -confirm:$false
    Connect-VIServer $placeholderIP -user root -password $password
   
    #Remove old Port Group
    Get-VirtualPortGroup| ? {$_.Name -eq 'VMkernel Management'} | Remove-VirtualPortGroup  -confirm:$false
   
    #Re-create original vmKernel network
    $vmhost = Get-VMHost $placeholderIP
    $vswitch = Get-VirtualSwitch -VMHost $vmhost -Name vSwitch0
   
    New-VMHostNetworkAdapter -vmhost $vmhost -PortGroup "VMkernel Management" -VirtualSwitch $vswitch -VMotionEnabled $true -IP $OriginalHostIP -SubnetMask 255.255.248.0 -ManagementTrafficEnabled $true
    Get-VirtualPortGroup -VMHost $vmhost -Name "VMkernel Management" | Set-VirtualPortGroup -VLanId 4
   
    #remove placeholder vmKernel network
    $vkernelAdapter = Get-VMHostNetworkAdapter | ? {$_.IP -like $placeholderIP}
    Remove-VMHostNetworkAdapter $vkernelAdapter -confirm:$false
       
    #Close connections
    Disconnect-VIServer $placeholderIP -confirm:$false
    Connect-VIServer $hostname -user root -password $password
   
    #remove old Port Group
    Get-VirtualPortGroup| ? {$_.Name -eq 'VMkernel Placeholder'} | Remove-VirtualPortGroup -confirm:$false
   
    Disconnect-VIServer $hostname -confirm:$false
}

Reply
0 Kudos
4 Replies
Shoganator
Enthusiast
Enthusiast

Hi Joe,

Thanks for posting this up. I for one will find it useful as I will soon be automating the deployment of a particular staging environment build I have been deploying lately. The portgroup creation here will come in handy as a reference Smiley Happy

Cheers!

Sean

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
Reply
0 Kudos
joe_cruz
Contributor
Contributor

Put the script up on GitHub if anyone wants to expand on it.

https://github.com/guamaniac/posh/blob/master/Re-create%20VMkernel%20Port%20Group.ps1

My first Git, so hopefully I did it right!

Smiley Happy

Joe

Reply
0 Kudos
Shoganator
Enthusiast
Enthusiast

Nice. I am still trying to come to terms / get the hang of Github myself. I have the client installed and key pairing done, but that is about it so far :smileygrin:

My personal blog: http://www.shogan.co.uk .::. Twitter: shogan85 .::. if an answer has helped or solved your question, please don't forget to mark as "Answered" or "Helpful"!
Reply
0 Kudos
joe_cruz
Contributor
Contributor

GitHub for Windows looks pretty awesome; just ran across it and it’s fresh off the Branch! </gitHumour>

http://windows.github.com/

Joe

Reply
0 Kudos