Skip to content

Instantly share code, notes, and snippets.

@grier
Created February 13, 2012 07:10
Show Gist options
  • Save grier/1814544 to your computer and use it in GitHub Desktop.
Save grier/1814544 to your computer and use it in GitHub Desktop.
vSphere API - Change VNC settings
public static void doChangeVnc(String url, String username, String password, String vmname, String vncport) throws Exception
{
ServiceInstance si = new ServiceInstance(new URL(url), username, password, true);
Folder rootFolder = si.getRootFolder();
VirtualMachine vm = null;
vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
if(vm==null)
{
System.out.println("VM not found");
return;
}
VirtualMachineConfigSpec vms = new VirtualMachineConfigSpec();
OptionValue opt = new OptionValue();
opt.setKey("remotedisplay.vnc.port");
opt.setValue(vncport);
OptionValue opt2 = new OptionValue();
opt2.setKey("remotedisplay.vnc.enabled");
opt2.setValue("TRUE");
vms.extraConfig = new OptionValue[] { opt, opt2 };
Task task = vm.reconfigVM_Task(vms);
String result = task.waitForMe();
System.out.println("Successfully changed VNC port on " + vmname + " to " + vncport);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment