VMware Cloud Community
ragnarok10
Enthusiast
Enthusiast
Jump to solution

remove network adapter

Hello Team

Can anyone help me with the below code it is getting executed but the VLAN is still seen in the configuration.

$VMs = Get-VM -Name server

$NICs = x-vlan-222
$i = 0

foreach($VM in $VMs){
 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
 
$Adapters = Get-NetworkAdapter -VM $VM|?{$_.NetworkName -like "x-vlan-222"}
 
foreach($Adapter in $Adapters){
   
if($NICs -contains $Adapter){
     
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
     
$devSpec.operation = "remove"
     
$devSpec.device += $Adapter.ExtensionData
     
$spec.deviceChange += $devSpec
    }
  }
 
$VM.ExtensionData.ReconfigVM_Task($spec)
}

 

Reply
0 Kudos
2 Solutions

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Since you already have the Where-clause there is no need for an additional If.
Also, the -like operator where the right operand has no meta characters (like *) is in fact an -eq.

You could do

$VMs = Get-VM -Name server

$NICs = 'x-vlan-222'

foreach($VM in $VMs){
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  Get-NetworkAdapter -VM $VM | Where-Object{$_.NetworkName -eq "x-vlan-222"} |
  ForEach-Object -Process {
      $devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $devSpec.operation = "remove"
      $devSpec.device += $_.ExtensionData
      $spec.deviceChange += $devSpec
    }
  $VM.ExtensionData.ReconfigVM_Task($spec)
}

 


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

View solution in original post

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you mean 2vNICs attached to different portgroups, you could do

$VMs = Get-VM -Name server

$NICs = 'pg1','pg2'

foreach($VM in $VMs){
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  Get-NetworkAdapter -VM $VM | Where-Object{$NICs -contains $_.NetworkName} |
  ForEach-Object -Process {
      $devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $devSpec.operation = "remove"
      $devSpec.device += $_.ExtensionData
      $spec.deviceChange += $devSpec
    }
  $VM.ExtensionData.ReconfigVM_Task($spec)
}


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

I have no idea what you are doing in that script?!?

In $NICs you have what I assume is a String with the name of the targetted Portgroup.
Then you select all vNIC on VM that are connected to a Portgroup named 'Dummy'.
Then you compare vNIC objects with the single string in $NICs.


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

Reply
0 Kudos
ragnarok10
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Apologies for the typo,I have corrected the script.

I want to remove the network adapter x-vlan-222 from multiple VM's how shall I achieve this, for safety I'm trying it on one VM.

 The condition I want is if $NICs matches $Adapters then only the network adapter should be removed.

Please suggest.

$VMs = Get-VM -Name server

$NICs = x-vlan-222
$i = 0

foreach($VM in $VMs){
 
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
 
$Adapters = Get-NetworkAdapter -VM $VM|?{$_.NetworkName -like "x-vlan-222"}
 
foreach($Adapter in $Adapters){
   
if($NICs -contains $Adapter){
     
$devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
     
$devSpec.operation = "remove"
     
$devSpec.device += $Adapter.ExtensionData
     
$spec.deviceChange += $devSpec
    }
  }
 
$VM.ExtensionData.ReconfigVM_Task($spec)
}

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since you already have the Where-clause there is no need for an additional If.
Also, the -like operator where the right operand has no meta characters (like *) is in fact an -eq.

You could do

$VMs = Get-VM -Name server

$NICs = 'x-vlan-222'

foreach($VM in $VMs){
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  Get-NetworkAdapter -VM $VM | Where-Object{$_.NetworkName -eq "x-vlan-222"} |
  ForEach-Object -Process {
      $devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $devSpec.operation = "remove"
      $devSpec.device += $_.ExtensionData
      $spec.deviceChange += $devSpec
    }
  $VM.ExtensionData.ReconfigVM_Task($spec)
}

 


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

Reply
0 Kudos
ragnarok10
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD that worked like a charm.

 

Reply
0 Kudos
ragnarok10
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Is there any way that I will be able to remove 2 or more network adapter at once using variable $NICs.

Thanks in advance

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you mean 2vNICs attached to different portgroups, you could do

$VMs = Get-VM -Name server

$NICs = 'pg1','pg2'

foreach($VM in $VMs){
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  Get-NetworkAdapter -VM $VM | Where-Object{$NICs -contains $_.NetworkName} |
  ForEach-Object -Process {
      $devSpec = New-Object VMware.Vim.VirtualDeviceConfigSpec
      $devSpec.operation = "remove"
      $devSpec.device += $_.ExtensionData
      $spec.deviceChange += $devSpec
    }
  $VM.ExtensionData.ReconfigVM_Task($spec)
}


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

ragnarok10
Enthusiast
Enthusiast
Jump to solution

Yess, that worked.

Thanks a lot.

Reply
0 Kudos
ragnarok10
Enthusiast
Enthusiast
Jump to solution

Hey LucD

How can I capture the difference in VM-reconfiguration of removing the Nics has done (like before & after printing as logs) 

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can send whatever text you want to a file via the Out-File cmdlet.
Or do you mean something else?


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

Reply
0 Kudos