VMware Cloud Community
User20000
Enthusiast
Enthusiast
Jump to solution

Remove vmnic from Host

I'm trying to remove a vmnic from a host, and the PowerCLI Cmdlets Reference looks like it's giving me the wrong information:  www.vmware.com/support/developer/PowerCLI/PowerCLI41/html/Remove-VMHostNetworkAdapter.html

Here's what I tried:

  $network = get-vmhostnetworkadapter -vmhost Host1 -name vmnic7

  Remove-VMHostNetworkAdapter -nic $network

Cannot bind parameter 'Nic'. Cannot convert the "vmnic7" value of type "VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.Nic.PhysicalNicImpl" to type "VMware.VimAutomation.ViCore.Types.V1.Host.Networking. Nic.HostVirtualNic".

I also tried:

  $network = get-vmhostnetwork -vmhost Host1

  remove-vmhostnetworkadapter -nic $network.virtualnic[7]

Cannot validate argument on parameter 'Nic'.  The argument is null or empty.

Any ideas?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

It can be done, just requires some code.

Try the following, it's based on the code in dvSwitch posts.

function Get-dvSwitch {
  param([parameter(Position = 0, Mandatory = $true)][string]$DatacenterName,
    [
parameter(Position = 1, Mandatory = $true)][string]$dvSwitchName)   function Get-dvSwitchInThisLocation {     param(       [parameter(Position = 0, Mandatory = $true)]       [VMware.Vim.ManagedObjectReference]$LocationMoRef,
      [
parameter(Position = 1, Mandatory = $true)][string]$dvSwitchName
    )     $location = Get-View $LocationMoRef
   
foreach($child in $location.ChildEntity){       if($child.Type -eq "Folder"){         Get-dvSwitchInThisLocation $child $dvSwitchName      }       elseif($child.Type -eq "VmwareDistributedVirtualSwitch"){         $temp = Get-View $child
        if($temp.Name -eq $dvSwitchName){           $temp
        }       }     }   }   Get-dvSwitchInThisLocation (Get-Datacenter $DatacenterName).Extensiondata.NetworkFolder $dvSwitchName} function Remove-dvSwHostNic {   param($dvSwitch, $hostMoRef, $pnic)   $spec = New-Object VMware.Vim.DVSConfigSpec
 
$tgthost = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberConfigSpec
  $tgthost.Host = $hostMoRef
 
$currentBacking = ($dvSwitch.Config.Host | where {$_.Config.Host -eq [string]$hostMoRef}).Config.Backing
  $tgthost.operation = "edit"
 
$tgthost.backing = New-Object VMware.Vim.DistributedVirtualSwitchHostMemberPnicBacking
 
$tgthost.Backing.PnicSpec = $currentBacking.PnicSpec | where {$_.PnicDevice -ne $pnic}   $spec.Host = $tgthost
 
$dvSwitch.UpdateViewData()   $spec.ConfigVersion = $dvSwitch.Config.ConfigVersion   $taskMoRef = $dvSwitch.ReconfigureDvs_Task($spec)   $task = Get-View $taskMoRef
  while("running","queued" -contains $task.Info.State){     $task.UpdateViewData("Info")   } } $esxName = "MyEsx"
$dcName = "MyDatacenter"
$dvSwName
= "dvSw1"
$removepnicName
= "vmnic3"
$esx
= Get-VMHost -Name $esxName $dvSw = Get-dvSwitch -datacenter $dcName -dvswitchname $dvSwName
Remove-dvSwHostNic
-dvSwitch $dvSw -hostMoRef $esx.ExtensionData.MoRef -pnic $removepnicName

The code is a quick-and-dirty thing, but it works in my test environment.

Provide the ESX(i) name, the name of the datacenter, the name of the dvSwitch and the pnic you want to remove.

I'll add that Remove-dvSwHostNic function to my collection.


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

View solution in original post

0 Kudos
25 Replies
mrksiddiqui
Enthusiast
Enthusiast
Jump to solution

Take -NIC out 


$network = get-vmhostnetworkadapter 
Remove-VMHostNetworkAdapter $network.VirtualNic[7] -Confirm
If this helps answer your question please consider awarding points!
0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

New error:

$network = get-vmhostnetworkadapter -vmhost Host1
Remove-VMHostNetworkAdapter $network.VirtualNic[7] -Confirm

Cannot index into a null array.




0 Kudos
mrksiddiqui
Enthusiast
Enthusiast
Jump to solution

Can you provide the out of this

$network = get-vmhostnetworkadapter -vmhost Host1

Just type $network on command prompt should give a list like this

Name Mac IP DhcpEnabled Id

-


--- --

If this helps answer your question please consider awarding points!
0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Name       Mac               DhcpEnabled IP              SubnetMask      Device
                                                                           Name
----       ---               ----------- --              ----------      ------
vmnic0     80:c1:6e:73:1e:bc False                                       vmnic0
vmnic1     80:c1:6e:73:1e:bd False                                       vmnic1
vmnic2     80:c1:6e:73:1e:be False                                       vmnic2
vmnic3     80:c1:6e:73:1e:bf False                                       vmnic3
vmnic4     e8:39:35:0f:f8:e2 False                                       vmnic4
vmnic5     e8:39:35:0f:f8:e3 False                                       vmnic5
vmnic6     e8:39:35:0f:f0:76 False                                       vmnic6
vmnic7     e8:39:35:0f:f0:77 False                                       vmnic7

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try to do

$network = get-vmhostnetwork -vmhost Host1

remove-vmhostnetworkadapter -nic $network[7]


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Same error as the first attempt (Cannot bind parameter...)

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Get-VMHostNetworkAdapter cmdlet returns a HostNic object, but there are  2 types of these, PhysicalNic (the vmnicx) and HostVirtualNic (the vmkx).

With the Remove-VMHostNetworkAdapter you have to provide a HostVirtualNic on the Nic parameter.

If you don't, you get an error.

The Get-VMHostNetwork returns a VMHostNetworkInfo object.

Under the VirtualNic property of that object you will find all the HostVirtualNic of that ESX(i) server.

So the documentation is correct, you just have to make sure you are passing a HostVirtualNic to the Remove-VMHostNetworkAdapter cmdlet.


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Cool.  How do I do that?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can't remove a physical NIC from a host with PowerCLI cmdlets.

You can remove a physical NIC from a virtual switch with the Set-VirtualSwitch cmdlet.

Have a look at Re: Script that removes vmnic from vswitch ?


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Hm.  Well, it's connected to a vDS, so that's a little bit more difficult.  Does anyone know what the Remove-VMHostNetworkAdapter command actually does?  Because it sure doesn't look like it removes a VM host network adapter.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Remove-VMHostNetworkAdapter cmdlet removes a Virtual NIC (vmkx) from a host, not a physical (vmnicx).

Look at the type of object that is expected on the NIC parameter.

A dvSwitch is a totally other concept.

You can use the cmdlets from the VDSPowerCli fling for that.


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Doesn't seem to like the vDS commands:

PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> Add-PSSnapin V
Mware.VimAutomation.VdsComponent
PowerCLI C:\Program Files\VMware\Infrastructure\vSphere PowerCLI> get-vds
Get-Vds : 9/20/2012 3:29:00 PM    Get-Vds        Method not found: 'System.Coll
ections.Generic.List`1<!!0> VMware.VimAutomation.ViCore.Util10Ps.CommonUtil.Fil
terCollection(System.Collections.Generic.IEnumerable`1<!!0>, System.String, Sys
tem.Collections.Generic.IEnumerable`1<System.String>)'.
At line:1 char:8
+ get-vds <<<<
    + CategoryInfo          : NotSpecified: (:) [Get-Vds], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
   n.VdsComponent.Commands.Cmdlets.GetDistributedSwitch

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which PowerShell version are you using ?

Do a

$Host.Version


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

5.1 Release 1.  Here's the output of the command:

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Last try, are you running this from a 64-bit PowerCLI session ?

There were some problems reported with 64-bit, perhaps try from a 32-bit PowerCLI session in that case.


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

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

It's a 32-bit.

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

Any other ideas, anyone?

0 Kudos
User20000
Enthusiast
Enthusiast
Jump to solution

I finally got the VDS fling working on another computer, but I'm still having trouble removing the uplink.  Is anyone familiar with the set-vds command?  I haven't found a good reference for these commands yet, unfortunately.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you already try

Get-Help Get-Vds -Full


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

0 Kudos