I currently have a requirement to remove a specific NIC card ( We have 3 NICs assigned to each VM) from guest VMs . The VM numbers are so huge and the order of NIC card that needs to be removed may vary from VM to VM . So my current inputs would be VM Name and MAC address on a csv file probably . If the VM name is 'test1' and corresponding MAC is "xxx" then I need to remove the specific NIC corresponding to that MAC address (which can be any of this 1-3) . Even if the verification is mac address alone, it should be fine. I am much new to scripting and couldn't get this done . Can any of you support here
What do you already have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is the script that I am having and it removes the 2nd NIC interface
Import-module vmware.vimautomation.core
Import-module VMware.VimAutomation.Vds
$VMs = Get-Content <path for txt file>
$vcenter = <vcenter name>
Connect-VIServer -Server $vcenter
foreach ($VM in $VMs)
{
$Var = Get-VM -Name $VM
$nic = $Var.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq "Network adapter 2"}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.Device = $nic
$spec.DeviceChange += $dev
$Var.ExtensionData.ReconfigVM($spec)
}
I'm not sure how this fits in with your original request to remove vNICs with a specific MAC address.
If you have a CSV, I would do something like this
# CSV contains
#
# VM,Mac
#
Import-Csv -Path .\vm.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
Get-VM -Name $row.VM | Get-NetworkAdapter |
where{$_.MacAddress -eq $row.Mac} |
Remove-NetworkAdapter -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much LucD
Remove-NetworkAdapter -Confirm:$falseI think if we give this command, the VM power state must be powered off right ? Rest of the code is working as intended as instead of Remove I had set it to disconnect and that is working properly
Import-Csv -Path .\vm.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
Get-VM -Name $row.VM | Get-NetworkAdapter |
where{$_.MacAddress -eq $row.Mac} |
Set-NetworkAdapter -Connected:$false -Confirm:$false
}
That is correct, the cmdlet will require the VM to be powered off.
But with the API method, you can remove vNICs while the VM is powered on.
See Solved: Re: Removing Network Adapters from Powered On VM's - VMware Technology Network VMTN for some sample code.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD . Managed to get this working.
$VMs = Import-Csv -Path .\VM.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$VMList = Get-VM -Name $row.VM
$nic = $VMList | Get-NetworkAdapter | where{$_.MacAddress -eq $row.Mac}
$niccheck = $VMList.ExtensionData.Config.Hardware.Device | where {$_.DeviceInfo.Label -eq $nic}
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec
$dev.operation = "remove"
$dev.Device = $niccheck
$spec.DeviceChange += $dev
$VMList.ExtensionData.ReconfigVM($spec)
