Skip to content

Instantly share code, notes, and snippets.

@grier
Created February 13, 2012 07:11
Show Gist options
  • Save grier/1814550 to your computer and use it in GitHub Desktop.
Save grier/1814550 to your computer and use it in GitHub Desktop.
vSphere API - Snapshot.java
public static void doSnapshot(String url, String username, String password, String vmname, String snapshotname) 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;
}
Task task = vm.createSnapshot_Task(snapshotname, "auto", false, false);
if(task.waitForMe()==Task.SUCCESS)
{
System.out.println("Successfully created snapshot " + snapshotname + " on " + vmname);
}
else
{
throw new Exception("Error creating snapshot!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment