protected bool IsRunning(string vmx) { IVM2 vm = GetResult <IVM2>(WaitForResults(_host.OpenVM(vmx, null))); bool is_running = GetVMProperty <bool>(vm, Constants.VIX_PROPERTY_VM_IS_RUNNING); CloseVixObject(vm); return(is_running); }
bool IVMController.PauseVMX(string vmx) { if (!IsRunning(vmx)) { return(false); } IVM2 vm = GetResult <IVM2>(WaitForResults(_host.OpenVM(vmx, null))); bool success = DoneJob(vm.Pause(0, null, null)); CloseVixObject(vm); return(success); }
bool IVMController.StopVMX(string vmx) { if (!CanStop(vmx)) { return(false); } IVM2 vm = GetResult <IVM2>(WaitForResults(_host.OpenVM(vmx, null))); bool success = DoneJob(vm.PowerOff(Constants.VIX_VMPOWEROP_FROM_GUEST, null)); CloseVixObject(vm); return(success && !IsRunning(vmx)); }
bool IVMController.StartVMX(string vmx) { if (IsRunning(vmx)) { return(false); } IVM2 vm = GetResult <IVM2>(WaitForResults(_host.OpenVM(vmx, null))); bool success = DoneJob(vm.PowerOn(Constants.VIX_VMPOWEROP_NORMAL, null, null)); CloseVixObject(vm); return(success && IsRunning(vmx)); }
/// <summary> /// A VMWare snapshot constructor. /// </summary> /// <param name="vm">Virtual machine.</param> /// <param name="snapshot">Snapshot.</param> /// <param name="parent">Parent snapshot.</param> public VMWareSnapshot(IVM2 vm, ISnapshot snapshot, VMWareSnapshot parent) : base(snapshot) { _vm = vm; _parent = parent; }
/// <summary> /// Connect to both VMWare VI and ESX demo, see http://communities.vmware.com/thread/186655. /// </summary> /// <param name="args"></param> static void Main(string[] args) { try { VixCOM.VixLib vix = new VixLib(); { // connect to a VI host Console.WriteLine("Connecting to VI host"); IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER, "https://linc.nycapt35k.com/sdk/", 0, "vmuser", "admin123", 0, null, null); object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object hosts = null; ulong rc = connectJob.Wait(connectProperties, ref hosts); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } IHost host = (IHost)((object[])hosts)[0]; // open a vm Console.WriteLine("Opening VM"); IJob openJob = host.OpenVM("[dbprotect-1] ddoub-red/ddoub-red.vmx", null); object[] openProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object openResults = null; rc = openJob.Wait(openProperties, ref openResults); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } IVM2 vm = (IVM2)((object[])openResults)[0]; // number of snapshots int nSnapshots = 0; vm.GetNumRootSnapshots(out nSnapshots); Console.WriteLine("Root snapshots: {0}", nSnapshots); List <ISnapshot> snapshots = new List <ISnapshot>(); for (int i = 0; i < nSnapshots; i++) { Console.WriteLine("Fetching snapshot: {0}", i); ISnapshot snapshot = null; rc = vm.GetRootSnapshot(i, out snapshot); snapshots.Add(snapshot); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } } // create a snapshot Console.WriteLine("Creating snapshot"); VMWareJobCallback jobDoneCallback = new VMWareJobCallback(); IJob createSnapshotJob = vm.CreateSnapshot( Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, null, jobDoneCallback); jobDoneCallback.WaitForCompletion(10000); rc = createSnapshotJob.WaitWithoutResults(); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } snapshots = null; GC.Collect(); GC.WaitForPendingFinalizers(); // disconnect Console.WriteLine("Disconnecting"); host.Disconnect(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// A collection of snapshots that belong to a virtual machine. /// </summary> /// <param name="vm">A virtual machine instance.</param> public VMWareRootSnapshotCollection(IVM2 vm) : base(vm, null) { }
private static void ThreadProc(object param) { ConnectionInfo connectionInfo = (ConnectionInfo)param; while (true) { try { // connect to a VI host ConsoleOutput.WriteLine("Connecting to {0}", string.IsNullOrEmpty(connectionInfo.Uri) ? "VMWare Workstation" : connectionInfo.Uri); IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, connectionInfo.HostType, connectionInfo.Uri, 0, connectionInfo.Username, connectionInfo.Password, 0, null, null); object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object hosts = null; ulong rc = connectJob.Wait(connectProperties, ref hosts); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)connectJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } IHost host = (IHost)((object[])hosts)[0]; { // open a vm ConsoleOutput.WriteLine("Opening {0}", connectionInfo.Vmx); IJob openJob = host.OpenVM(connectionInfo.Vmx, null); object[] openProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object openResults = null; rc = openJob.Wait(openProperties, ref openResults); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)openJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } ConsoleOutput.WriteLine("Opened {0}", connectionInfo.Vmx); IVM2 vm = (IVM2)((object[])openResults)[0]; ((IVixHandle2)openJob).Close(); // get root snapshot ConsoleOutput.WriteLine("Fetching root snapshot"); ISnapshot snapshot = null; rc = vm.GetRootSnapshot(0, out snapshot); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } ConsoleOutput.WriteLine("Reverting to snapshot"); // revert to the snapshot IJob revertJob = vm.RevertToSnapshot(snapshot, Constants.VIX_VMPOWEROP_NORMAL, null, null); rc = revertJob.WaitWithoutResults(); ((IVixHandle2)snapshot).Close(); ((IVixHandle2)revertJob).Close(); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } // power on ConsoleOutput.WriteLine("Powering on"); IJob powerOnJob = vm.PowerOn(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null, null); rc = powerOnJob.WaitWithoutResults(); ((IVixHandle2)powerOnJob).Close(); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } // wait for tools in guest ConsoleOutput.WriteLine("Waiting for tools"); IJob waitForToolsJob = vm.WaitForToolsInGuest(240, null); rc = waitForToolsJob.WaitWithoutResults(); ((IVixHandle2)waitForToolsJob).Close(); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } // power off ConsoleOutput.WriteLine("Powering off"); IJob powerOffJob = vm.PowerOff(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null); rc = powerOffJob.WaitWithoutResults(); ((IVixHandle2)powerOffJob).Close(); if (vix.ErrorIndicatesFailure(rc)) { throw new Exception(vix.GetErrorText(rc, "en-US")); } ((IVixHandle2)vm).Close(); } // disconnect ConsoleOutput.WriteLine("Disconnecting"); host.Disconnect(); } catch (Exception ex) { ConsoleOutput.WriteLine("ERROR: {0}", ex.Message); ConsoleOutput.WriteLine("{0}", ex.StackTrace); } } }
/// <summary> /// AV demo, see http://communities.vmware.com//thread/187177?tstart=0. /// </summary> /// <param name="args"></param> static void Main(string[] args) { int iterationNo = 1; while (true) { IHost host = null; IVM2 vm = null; try { VixCOM.VixLib vix = new VixLib(); Console.WriteLine("**** Iteration: " + iterationNo + "*******"); iterationNo++; // connect to a VI host Console.WriteLine("Connecting to host"); IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER, "https://linc.nycapt35k.com/sdk/", 0, "vmuser", "admin123", 0, null, null); object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object hosts = null; ulong rc = connectJob.Wait(connectProperties, ref hosts); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)connectJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } host = (IHost)((object[])hosts)[0]; ((IVixHandle2)connectJob).Close(); { // open a vm Console.WriteLine("Opening VM"); IJob openJob = host.OpenVM("[dbprotect-1] ddoub-purple/ddoub-purple.vmx", null); object[] openProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object openResults = null; rc = openJob.Wait(openProperties, ref openResults); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)openJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } vm = (IVM2)((object[])openResults)[0]; ((IVixHandle2)openJob).Close(); // get root snapshot Console.WriteLine("Fetching root snapshot"); ISnapshot snapshot = null; rc = vm.GetRootSnapshot(0, out snapshot); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)openJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } Console.WriteLine("Reverting to snapshot"); // revert to the snapshot IJob revertJob = vm.RevertToSnapshot(snapshot, Constants.VIX_VMPOWEROP_NORMAL, null, null); revertJob.WaitWithoutResults(); ((IVixHandle2)revertJob).Close(); ((IVixHandle2)snapshot).Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (null != vm) { Console.WriteLine("Closing VM"); ((IVixHandle2)vm).Close(); } if (null != host) { Console.WriteLine("Disconnecting"); host.Disconnect(); } Console.WriteLine("GC"); GC.Collect(); GC.WaitForPendingFinalizers(); } } }
/// <summary> /// AV demo /// </summary> /// <param name="args"></param> static void Main(string[] args) { try { string uri = "https://tubbs.nycapt35k.com/sdk"; string vmx = "[adpro-1] snowtest-w2k8/snowtest-w2k8.vmx"; string username = "******"; string password = "******"; // connect to a VI host Console.WriteLine("Connecting to {0}", uri); IJob connectJob = vix.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_VI_SERVER, uri, 0, username, password, 0, null, null); object[] connectProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object hosts = null; ulong rc = connectJob.Wait(connectProperties, ref hosts); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)connectJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } IHost host = (IHost)((object[])hosts)[0]; { // open a vm Console.WriteLine("Opening {0}", vmx); IJob openJob = host.OpenVM(vmx, null); object[] openProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object openResults = null; rc = openJob.Wait(openProperties, ref openResults); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)openJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } Console.WriteLine("Opened {0}", vmx); IVM2 vm = (IVM2)((object[])openResults)[0]; ((IVixHandle2)openJob).Close(); // create a snapshot string snapshotName = Guid.NewGuid().ToString(); Console.WriteLine("Creating snapshot {0}", snapshotName); VMWareJobCallback callback = new VMWareJobCallback(); IJob createSnapshotJob = vm.CreateSnapshot(snapshotName, snapshotName, 0, null, callback); object[] createSnapshotProperties = { Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object createSnapshotResults = null; rc = createSnapshotJob.Wait(createSnapshotProperties, ref createSnapshotResults); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)createSnapshotJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } ISnapshot createdSnapshot = (ISnapshot)((object[])createSnapshotResults)[0]; ((IVixHandle2)createSnapshotJob).Close(); // delete snapshot IJob deleteSnapshotJob = vm.RemoveSnapshot(createdSnapshot, 0, callback); Console.WriteLine("Deleting snapshot {0}", snapshotName); rc = deleteSnapshotJob.WaitWithoutResults(); if (vix.ErrorIndicatesFailure(rc)) { ((IVixHandle2)deleteSnapshotJob).Close(); throw new Exception(vix.GetErrorText(rc, "en-US")); } ((IVixHandle2)deleteSnapshotJob).Close(); ((IVixHandle2)createdSnapshot).Close(); ((IVixHandle2)vm).Close(); } // disconnect Console.WriteLine("Done."); host.Disconnect(); GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex.Message); } }
/// <summary> /// Snapshot collection constructor. /// </summary> /// <param name="vm">Virtual machine.</param> /// <param name="parent">Snapshot parent.</param> public VMWareSnapshotCollection(IVM2 vm, VMWareSnapshot parent) { _vm = vm; _parent = parent; }
/// <summary> /// Dispose the collection. /// </summary> public void Dispose() { RemoveAll(); _parent = null; _vm = null; }