Created
December 7, 2012 19:15
-
-
Save MattMencel/4235695 to your computer and use it in GitHub Desktop.
Reset VM Network Adapter After Clone - Fix "Connect at power on" Bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygmes' | |
require 'rbvmomi' | |
vim = RbVmomi::VIM.connect host: vcenter_host_name, user: vcenter_user, password: vcenter_password, :insecure => true | |
dc = vim.serviceInstance.find_datacenter(my_datacenter) or fail "datacenter not found" | |
vm = dc.find_vm("folder_path_to_vm) or fail "VM not found" | |
dnic = vm.config.hardware.device.grep(RbVmomi::VIM::VirtualEthernetCard).find{|nic| nic.props} | |
port = dnic[:backing][:port] | |
if dnic[:connectable][:startConnected].eql?false | |
spec = RbVmomi::VIM.VirtualMachineConfigSpec({ | |
:deviceChange => [{ | |
:operation => :remove, | |
:device => dnic | |
}] | |
}) | |
puts "Removing NIC" | |
vm.ReconfigVM_Task(:spec => spec).wait_for_completion | |
puts "Adding NIC" | |
spec = RbVmomi::VIM.VirtualMachineConfigSpec( | |
:deviceChange => [{ | |
:operation => :add, | |
:device => RbVmomi::VIM::VirtualVmxnet3( | |
:key => 0, | |
:deviceInfo => { | |
:label => dev_info[:label], | |
:summary => dev_info[:summary] | |
}, | |
:backing => RbVmomi::VIM::VirtualEthernetCardDistributedVirtualPortBackingInfo( | |
:port => port | |
), | |
:addressType => 'generated' | |
) | |
}] | |
) | |
vm.ReconfigVM_Task(:spec => spec).wait_for_completion | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Matt,
This does the same without removing & adding