Hi,
I want to go over a list of virtual machines and remove their network adapter.
Is there a workflow that receives an array of virtual machines and removes the network adapter?
If no, someone create a workflow\script which do it?
Thanks!
Here is some sample code to remove network adapters from a single virtual machine (vm input parameter)
var nicsToRemove = [];
for each (var device in vm.config.hardware.device) {
if (device instanceof VcVirtualVmxnet
|| device instanceof VcVirtualVmxnet2
|| device instanceof VcVirtualVmxnet3
|| device instanceof VcVirtualE1000
|| device instanceof VcVirtualPCNet32
|| device instanceof VcVirtualEthernetCard) {
// Here you can add some custom logic to decide whether to remove the network adapter
// This sample code will remove all network adapters
nicsToRemove.push(device);
}
}
var vmConfigSpec = new VcVirtualMachineConfigSpec();
var deviceChanges = [];
var deviceConfigSpec = new VcVirtualDeviceConfigSpec() ;
for each (var nic in nicsToRemove) {
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;
deviceConfigSpec.device = nic;
deviceChanges.push(deviceConfigSpec);
}
vmConfigSpec.deviceChange = deviceChanges;
try {
myTask = vm.reconfigVM_Task(vmConfigSpec);
} catch (ex) {
System.warn("Error while reconfiguring VM: " + ex);
}
You could use the combination of Get-VM and Remove-networkAdapter cmdlets. First of you can create a list VMs in a csv file and use the below. Assuming the column header for the VMs is VMName.
$VMs = Import-Csv C:\VMs.csv
foreach ($VM in $VMs) {
Get-VM -Name $VM.VMName | Remove-NetworkAdapter
}
Get-VM - vSphere PowerCLI Cmdlets Reference
Remove-NetworkAdapter - vSphere PowerCLI Cmdlets Reference
Hope this helps.
Hi,
Thanks, but these are powershell commands, that can be use only with invoke script.
Is there a java script that can be used through the vmware orchestrator?
No, I wouldn't be aware of that as I have no experience with Orchestrator. Somebody on the community should be able to help.
Here is some sample code to remove network adapters from a single virtual machine (vm input parameter)
var nicsToRemove = [];
for each (var device in vm.config.hardware.device) {
if (device instanceof VcVirtualVmxnet
|| device instanceof VcVirtualVmxnet2
|| device instanceof VcVirtualVmxnet3
|| device instanceof VcVirtualE1000
|| device instanceof VcVirtualPCNet32
|| device instanceof VcVirtualEthernetCard) {
// Here you can add some custom logic to decide whether to remove the network adapter
// This sample code will remove all network adapters
nicsToRemove.push(device);
}
}
var vmConfigSpec = new VcVirtualMachineConfigSpec();
var deviceChanges = [];
var deviceConfigSpec = new VcVirtualDeviceConfigSpec() ;
for each (var nic in nicsToRemove) {
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.remove;
deviceConfigSpec.device = nic;
deviceChanges.push(deviceConfigSpec);
}
vmConfigSpec.deviceChange = deviceChanges;
try {
myTask = vm.reconfigVM_Task(vmConfigSpec);
} catch (ex) {
System.warn("Error while reconfiguring VM: " + ex);
}
Wow, Thank you for the code!
Do you have a similar code for adding a network adapter?
Here it is (you may want to experiment with deviceConfigSpec parameters eg. if you want to add different type of network adapter)
var vmConfigSpec = new VcVirtualMachineConfigSpec();
var deviceConfigSpec = new VcVirtualDeviceConfigSpec();
deviceConfigSpec.operation = VcVirtualDeviceConfigSpecOperation.add;
deviceConfigSpec.device = new VcVirtualE1000();
deviceConfigSpec.device.key = -100;
deviceConfigSpec.device.backing = new VcVirtualEthernetCardNetworkBackingInfo();
deviceConfigSpec.device.backing.deviceName = "VM Network";
deviceConfigSpec.device.connectable = new VcVirtualDeviceConnectInfo();
deviceConfigSpec.device.connectable.startConnected = true;
deviceConfigSpec.device.connectable.allowGuestControl = true;
deviceConfigSpec.device.connectable.connected = true;
deviceConfigSpec.device.controllerKey = 100;
deviceConfigSpec.device.addressType = "generated";
deviceConfigSpec.device.wakeOnLanEnabled = true;
vmConfigSpec.deviceChange = [deviceConfigSpec];
try {
myTask = vm.reconfigVM_Task(vmConfigSpec);
} catch (ex) {
System.warn("Error while reconfiguring VM: " + ex);
}
Thank you again!
I think I will love this product.
Another question:
I'm familiar with powershell commands, but I'm a beginner with VMware orechestrator and java script
How can I find easily these classes that you use in the last scripts? I know there is a search box, but when I search "network" I receive a lot of attribute\methods\scripts - can you give me some tips for an easy start?
another question:
Is there an easy way to configure IP address, subnet, default gateway for the new network adapter?
for now, I use Invoke-vmscript command, which take a while because of the connection progress to the vcenter.
Hi,
When I run the remove network code, I sometimes receive in the vSphere the following error:
Invalid configuration for device '1'.
What should I change in the code?
Thanks!