I wanted to remove Network Adapters from multiple VM's, so I thought a simple script would do it:
Get-NetworkAdapter -vm test* | ?{$_.NetworkName -like "dummy"} |Remove-NetworkAdapter
But this only results in error because the VM is powered on:
Remove-NetworkAdapter : 2013-08-13 15:58:48 Remove-NetworkAdapter The
VM must be in the following state: PoweredOff.
Is it possible to get around this?
Since this is possible through the vSphere Client it gave me the chance to try out Onyx. Below is the result from Onyx:
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec $spec.changeVersion = "2013-08-14T07:04:15.86208Z" $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1) $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec $spec.deviceChange[0].operation = "remove" $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3 $spec.deviceChange[0].device.key = 4000 $spec.deviceChange[0].device.deviceInfo = New-Object VMware.Vim.Description $spec.deviceChange[0].device.deviceInfo.label = "Network adapter 1" $spec.deviceChange[0].device.deviceInfo.summary = "dummy" $spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualEthernetCardNetworkBackingInfo $spec.deviceChange[0].device.backing.deviceName = "dummy" $spec.deviceChange[0].device.backing.useAutoDetect = $false $spec.deviceChange[0].device.backing.inPassthroughMode = $false $spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo $spec.deviceChange[0].device.connectable.startConnected = $true $spec.deviceChange[0].device.connectable.allowGuestControl = $false $spec.deviceChange[0].device.connectable.connected = $true $spec.deviceChange[0].device.connectable.status = "ok" $spec.deviceChange[0].device.controllerKey = 100 $spec.deviceChange[0].device.unitNumber = 7 $spec.deviceChange[0].device.addressType = "assigned" $spec.deviceChange[0].device.macAddress = "00:50:56:8f:74:52" $spec.deviceChange[0].device.wakeOnLanEnabled = $false $_this = Get-View -Id 'VirtualMachine-vm-646' $_this.ReconfigVM_Task($spec)
After stripping this down a bit I were left with:
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec $spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1) $spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec $spec.deviceChange[0].operation = "remove" $spec.deviceChange[0].device = New-Object VMware.Vim.VirtualVmxnet3 $spec.deviceChange[0].device.key = 4000 $spec.deviceChange[0].device.deviceInfo = New-Object VMware.Vim.Description $spec.deviceChange[0].device.deviceInfo.label = "Network adapter 1" $spec.deviceChange[0].device.deviceInfo.summary = "dummy" $_this = Get-View -Id 'VirtualMachine-vm-646' $_this.ReconfigVM_Task($spec)
Running this doesn't give any errors, but still all Network Adapters remain.
Is there any easy/better way to script this?