VMware Cloud Community
flynmooney
Enthusiast
Enthusiast
Jump to solution

PowerCLI to reset network settings like done in DCUI

I am going through our entire environment making a change where the mac address of the management nic changes.  Long story short, on each host I am having to manually login to the console and do a network restore.  I'd like to try to program this as you can login through powershell to a host directly.  I happened upon a page http://searchvmware.techtarget.com/tip/PowersHell-Factory-Reset-of-ESXi  which shows doing a complete factory reset through powershell.  I just want to restore the network settings to default.

The code in the webpage is:

$vmhost = "esx4.vi4book.com"

$vcname = "virtualcenter4.vi4book.com"

#Connect to vCenter & Enter Maintenance Mode

Connect-VIServer $vcname -username administrator -password vmware

$esxhost = Get-VMHost $vmhost

$hostview = $esxhost | Get-View

Set-VMHost $esxhost -State maintenance

# Remove ESX host from vCenter...

Remove-VMHost $vmhost -Confirm:$false

# Carry out factory reset...

Connect-VIServer $vmhost -username root -password password

$esxhost = Get-VMHost $vmhost

$hostview = $esxhost | Get-View

$ns = Get-View -Id $hostview.ConfigManager.firmwareSystem

$ns.ResetFirmwareToFactoryDefaults()

I've looked around a bit in the API but haven't found anything yet.

0 Kudos
1 Solution

Accepted Solutions
flynmooney
Enthusiast
Enthusiast
Jump to solution

The "RefreshNetworkSystem" call did not fix the mac but it led me to going through the network members again and I found "UpdateVirtualNic" which worked like a charm.  Code snip below.  Thanks for the help.

Variables needed:

$ipaddress

$esxcred

$mac

Connect-VIServer $ipaddress -credential $esxcred

$esxhost = Get-VMHost

$hostview = $esxhost | Get-View

$net = Get-View -Id $hostview.configmanager.networksystem

$vnic = $net.networkinfo.vnic | ? { $_.device -eq "vmk0" }
$spec = $vnic.spec

$spec.mac = $mac

$net.UpdateVirtualNic("vmk0",$spec)

disconnect-viserver * -confirm:$false

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik there is no API or command that performs that DCUI functionality.

You can reset your network configuration through the Get-EsxCli cmdlet, but the problem there would be that you are cutting off the branch on which you are sitting.

See for example a set of esxcli commands in ESXCLI Basics: Troubleshooting Management Network Connection on ESXi 5.x

On a side note, isn't a restart of the ESXi node picking up the MAC change?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
flynmooney
Enthusiast
Enthusiast
Jump to solution

Here's the MAC before the machine reconfiguration.  We're doing DHCP reservations and while the machine is being reconfigured and firmware being updated I go and change the dhcp reservation. It seems though that vmk0 is holding onto the MAC address even after the boot. 

The host profile which was previously applied has a setting of "Prompt the user for the MAC address if no default is available" for the management interface.

C:\Users\xxxx>
C:\Users\xxxx> $esxhost = get-vmhost xxxxxxxxxx
C:\Users\xxxx> $mgmt = $esxhost | Get-VMHostNetworkAdapter | ? { $_.Name -eq "vmk0" }
C:\Users\xxxx> $mgmt

Name       Mac               DhcpEnabled IP              SubnetMask      DeviceName
----       ---               ----------- --              ----------      ----------
vmk0       xx:xx:xx:xx:00:5a True        xxx.xxx.xxx.141   255.255.255.0         vmk0

Machine Reconfig done within UCS (Firmware update and vnic changes)

C:\Users\xxxx> $mgmt = $vmhost | Get-VMHostNetworkAdapter | ? { $_.Name -eq "vmk0" }
C:\Users\xxxx> $mgmt

Name       Mac               DhcpEnabled IP              SubnetMask      DeviceName
---       ---               ----------- --              ----------      ----------
vmk0       xx:xx:xx:xx:00:5a True        xxx.xxx.xxx.24    255.255.255.0         vmk0

C:\Users\xxxx> Get-UcsServiceProfile xxxxxxxxxx| Get-UcsVnic | select name,addr

Name             Addr
----             ----
esx-vmnic0-mgmt  xx:xx:xx:xx:00:4B

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see.

Does the RefreshNetworkSystem method change that MAC?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

flynmooney
Enthusiast
Enthusiast
Jump to solution

The "RefreshNetworkSystem" call did not fix the mac but it led me to going through the network members again and I found "UpdateVirtualNic" which worked like a charm.  Code snip below.  Thanks for the help.

Variables needed:

$ipaddress

$esxcred

$mac

Connect-VIServer $ipaddress -credential $esxcred

$esxhost = Get-VMHost

$hostview = $esxhost | Get-View

$net = Get-View -Id $hostview.configmanager.networksystem

$vnic = $net.networkinfo.vnic | ? { $_.device -eq "vmk0" }
$spec = $vnic.spec

$spec.mac = $mac

$net.UpdateVirtualNic("vmk0",$spec)

disconnect-viserver * -confirm:$false

0 Kudos