VMware Cloud Community
BScott66
Enthusiast
Enthusiast
Jump to solution

New-NetworkAdapter not working! Nor is Set-NetworkAdapter...

Hello all,

Needless to say I had a large issue with the vmxnet3 adapter for a customer and they requested all NICs be rolled over to the e1000e (Ugly!) but it was what they knew worked for them.

So I started to write a script using the latest PowerCLI.

This include cmdlets Net-NetworkAdapter, but this has limitation of it functionality which is not correct.

Specifically on MAC Addresses.

You see to make sure the systems which are using DHCP get back their original address so that there are no changes required to the Load Balancers we have to make sure they maintain the MAC's.

the MAC on a test system was 00:50:56:88:f7:78 which was originally auto-assigned.

Well that address is invalid if you try to pass it to the new adapter with the below error:

===============================================================================================================================================

New-NetworkAdapter : Cannot bind parameter 'MacAddress' to the target. Exception setting "MacAddress": "The MAC

address is invalid or is not in the valid range 00:50:56:00:00:00 - 00:50:56:3F:FF:FF."

At C:\SwapNics.ps1:41 char:55

+ New-NetworkAdapter -vm $VM -Type $NICType -MacAddress $MACAdd -StartConnected -N ...

+                                                       ~~~~~~~

    + CategoryInfo          : WriteError: (:) [New-NetworkAdapter], ParameterBindingException

    + FullyQualifiedErrorId : ParameterBindingFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.NewNet

   workAdapter

===============================================================================================================================================

It seems that whomever created the cmdlet has incorrect validation in their code. as 00:50:56:88:f7:78 is in between 00:50:56:00:00:00 - 00:50:56:3F:FF:FF

likewise for Set-NetworkAdapter...

And with Set-NetworkAdapter you cannot go from -type:Vmxnet3 to -type:e1000 or e1000e, which would have made this whole situation so much less complicated!

Anyhow, figured I would share my fndings and if this is not corrected save someone the multiple hours trying to figure out if it was a context error or something in my code.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Afaik, you can't change the Type in all cases via the Set-NetworkAdapter cmdlet.

I normally use Remove-NetworkAdapter and New-NetworkAdapter together.

Or I use one call to the API ReconfigVM method, where you can combine this in one method call.

Like this for example:

$vm = Get-vm MyVM

$nic = Get-NetworkAdapter -VM $vm

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$oldSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec

$oldSpec.Operation = 'remove'

$oldSpec.Device = $nic.ExtensionData

$spec.DeviceChange += $oldSpec

$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec

$devSpec.Operation = 'add'

$devSpec.Device = New-Object VMware.Vim.VirtualE1000e

$devSpec.Device.Backing = $nic.ExtensionData.Backing

$devSpec.Device.AddressType = $nic.ExtensionData.AddressType

$devSpec.Device.MacAddress = $nic.MacAddress

$devSpec.Device.WakeOnLanEnabled = $nic.WakeOnLanEnabled

$devSpec.Device.UptCompatibilityEnabled = $false

$devSpec.Device.Key = -1

$devSpec.Device.DeviceInfo = $nic.ExtensionData.DeviceInfo

$devSpec.Device.Connectable = $nic.ExtensionData.Connectable

$devSpec.Device.ControllerKey = $nic.ExtensionData.ControllerKey

$devSpec.Device.UnitNumber = $nic.ExtensionData.UnitNumber

$spec.DeviceChange += $devSpec

$vm.ExtensionData.ReconfigVM($spec)

On the MAC address, notice that the permitted range on the PowerCLI cmdlet to set this manually is 00:50:56:00:00:00 - 00:50:56:3F:FF:FF,

while the accepted range seems to be 00:50:56:00:00:00 - 00:50:56:FF:FF:FF.

That might be a bug in the cmdlet.


With the script I show above, that problem does not exist.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik, you can't change the Type in all cases via the Set-NetworkAdapter cmdlet.

I normally use Remove-NetworkAdapter and New-NetworkAdapter together.

Or I use one call to the API ReconfigVM method, where you can combine this in one method call.

Like this for example:

$vm = Get-vm MyVM

$nic = Get-NetworkAdapter -VM $vm

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$oldSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec

$oldSpec.Operation = 'remove'

$oldSpec.Device = $nic.ExtensionData

$spec.DeviceChange += $oldSpec

$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec

$devSpec.Operation = 'add'

$devSpec.Device = New-Object VMware.Vim.VirtualE1000e

$devSpec.Device.Backing = $nic.ExtensionData.Backing

$devSpec.Device.AddressType = $nic.ExtensionData.AddressType

$devSpec.Device.MacAddress = $nic.MacAddress

$devSpec.Device.WakeOnLanEnabled = $nic.WakeOnLanEnabled

$devSpec.Device.UptCompatibilityEnabled = $false

$devSpec.Device.Key = -1

$devSpec.Device.DeviceInfo = $nic.ExtensionData.DeviceInfo

$devSpec.Device.Connectable = $nic.ExtensionData.Connectable

$devSpec.Device.ControllerKey = $nic.ExtensionData.ControllerKey

$devSpec.Device.UnitNumber = $nic.ExtensionData.UnitNumber

$spec.DeviceChange += $devSpec

$vm.ExtensionData.ReconfigVM($spec)

On the MAC address, notice that the permitted range on the PowerCLI cmdlet to set this manually is 00:50:56:00:00:00 - 00:50:56:3F:FF:FF,

while the accepted range seems to be 00:50:56:00:00:00 - 00:50:56:FF:FF:FF.

That might be a bug in the cmdlet.


With the script I show above, that problem does not exist.


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

0 Kudos
BScott66
Enthusiast
Enthusiast
Jump to solution

LucD,

Been a while... Thank you for the response!

Below is the code I am using to do the actual remove and add/new NIC whcih I specify the MAC address.

The test system this was run against in my lab had te auto-assigned MAC of '00:50:56:88:f7:78' which is outside the validation range.and I get the error provided.

Code Start:

######################################################

$Cred = $Args[0]

Write-Host "Usr Credential: " $Cred

$vCenter = $Args[1]

Write-Host "VC: " $vCenter

$VM = $Args[2]

Write-Host "VM: " $VM

$NIC = $Args[3]

Write-Host "NIC: " $NIC

$Network = $Args[4]

Write-Host "NW: " $Network

$MACAdd = "'" + $Args[5] + "'"

Write-Host "MA: " $MACAdd

$NICType = $Args[6]

Write-Host "NT" $NICType

$Key = [byte]1..16

$encrypted = Get-Content c:\pcred.key | ConvertTo-SecureString -Key $Key

$credential = New-Object System.Management.Automation.PsCredential($Cred, $encrypted)

Connect-VIServer $vCenter -Credential $credential

$Outp1 = "Preparing to remove old NIC with details of: " + $NIC + ", nwname: " + $Network + ", mac: " + $MACAdd + ", newnictype: " + $NICType + " On VM: " + $VM

Write-Host $Outp1

$VM2e = get-vm -name $vm

####Shutdown VM

Shutdown-VMGuest -VM $VM -confirm:$false

$PwrState = get-vm -name $VM | Select PowerState

Do{

    Start-Sleep -s 10

    $PwrState = get-vm -name $VM | Select PowerState

    Write-Host $PwrState.PowerState

}

Until ($PwrState.PowerState -eq "PoweredOff")

Write-Host $VM " Powered Off!"

Write-Host "Removing NIC: " $NIC

$nic2rem = Get-NetworkAdapter -VM $VM -Name $NIC

Remove-NetworkAdapter -NetworkAdapter $nic2rem -confirm:$false

Write-Host "Adding New NIC: " $NIC

New-NetworkAdapter -vm $VM -Type $NICType -MacAddress $MACAdd -StartConnected -NetworkName $Network -confirm:$false

Write-Host "Add completed."

Write-Host "Powering up VM..."

Start-VM -VM $VM -confirm:$false

Start-Sleep -s 60

######################################################

Code End


This is a secondary script that is called from another so that this is multi-threaded as there are 1,000's of systems requiring the change.

Is this something that can be corrected in the cmdlet?


0 Kudos
LucD
Leadership
Leadership
Jump to solution

I don't work for VMware.

For requests or corrections on PowerCLI cmdlets you will have to contact the PM (alanrenouf)

In fact to circumvent the obvious error in the accepted range for the MAC parameter, I use the direct API calls.


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

0 Kudos
opscc
Contributor
Contributor
Jump to solution

Thank you for the code sample.

Would you happen to know if I were to manually assign a MAC in the 00:50:56:3F:FF:FF - 00:50:56:FF:FF:FF  range, which seems to be the range for automatic MAC Address Generation, would the vsphere "know" not to reuse it? 

The online documentation I have seen is not clear on that [to me].

i.e Does the uniqueness algorithm look at all assigned MACs [manual and automatic], or just automatic

e.g.

I use this on Monday

     ...

   $devSpec.Device.MacAddress = "00:50:56:FF:FF:Fa"

     ...


Is it possible that this command, on Tuesday, would also assign  "00:50:56:FF:FF:Fa"

   New-NetworkAdapter -vm "theVM" -network "theNetwork"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The range "00:50:56" is reserved for manually assigned MAC addresses.

This range is not used by vSphere, so it's the responsability of the admin to make sure there are no duplicates.

See also KB219


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

0 Kudos