RBC201110141
Contributor
Contributor

PowerCLI Remove-NetworkAdapter issues - need to remove single adapter

Jump to solution

I need to remove a single adapter via PowerCLI:

"Get-NetworkAdapter -VM myvm" returns:

Name                 Type       NetworkName  MacAddress         WakeOnLan
                                                                  Enabled
----                 ----       -----------  ----------         ---------
Network adapter 1    e1000      VM Network   00:50:56:09:09:09      False
Network adapter 2    e1000      VM Network   00:50:56:01:08:07      False

I only want to remove Network adapter 2, so I tried:

"Remove-NetworkAdapter -networkadapter "Network adapter 2" which returns:

Remove-NetworkAdapter : Cannot bind parameter 'NetworkAdapter'. Cannot convert
the "Network adapter 2" value of type "System.String" to type "VMware.VimAutoma
tion.ViCore.Types.V1.VirtualDevice.NetworkAdapter".
At line:1 char:38
+ remove-networkadapter -networkadapter <<<<  "Network adapter 2"
    + CategoryInfo          : InvalidArgument: (:) [Remove-NetworkAdapter], Pa
   rameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.RemoveNetworkAdapter

So I tried:

$network = get-networkadapter -VM myvm

Remove-NetworkAdapter -NetworkAdapter $network

Which wants to delete both network adapters:

Confirm
Are you sure you want to perform this action?
Performing operation "Remove network adapter" on Target "Network adapter 1,
Network adapter 2".

So I tried

$network = get-networkadapter -VM myvm

Remove-NetworkAdapter -NetworkAdapter $network.networkadapter[2]

Which returns:

Cannot index into a null array.
At line:1 char:63
+ remove-networkadapter -networkadapter $network.networkadapter[ <<<< 2]
    + CategoryInfo          : InvalidOperation: (2:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

So, what am I missing?  I need to remove only network adapter 2.

Any help would be greatly appreciated.

-stephen

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can do

Get-NetworkAdapter -VM myvm | where {$_.Name -eq "Network adapter 2"} | Remove-NetworkAdapter

Note that a PS array starts indexing at 0.

So your last attempt should have been

$network = get-networkadapter -VM myvm

Remove-NetworkAdapter -NetworkAdapter $network.networkadapter[1]


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

You can do

Get-NetworkAdapter -VM myvm | where {$_.Name -eq "Network adapter 2"} | Remove-NetworkAdapter

Note that a PS array starts indexing at 0.

So your last attempt should have been

$network = get-networkadapter -VM myvm

Remove-NetworkAdapter -NetworkAdapter $network.networkadapter[1]


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

View solution in original post

0 Kudos
RBC201110141
Contributor
Contributor

This worked great.  Thank you.

0 Kudos