/// <summary> /// Remove a virtual machine from the host's inventory. /// </summary> /// <param name="fileName">Virtual Machine file, local .vmx or [storage] .vmx.</param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void Unregister(string fileName, int timeoutInSeconds) { if (_handle == null) { throw new InvalidOperationException("No connection established"); } switch (ConnectionType) { case ServiceProviderType.VirtualInfrastructureServer: case ServiceProviderType.Server: case ServiceProviderType.WorkstationShared: break; default: throw new NotSupportedException(string.Format("Unregister is not supported on {0}", ConnectionType)); } try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_handle.UnregisterVM(fileName, callback), callback)) { job.Wait(timeoutInSeconds); } } catch (Exception ex) { throw new Exception( string.Format("Failed to unregister virtual machine: fileName=\"{0}\" timeoutInSeconds={1}", fileName, timeoutInSeconds), ex); } }
/// <summary> /// Remove/delete this snapshot. /// </summary> /// <remarks> /// If the snapshot is a member of a collection, the latter is updated with orphaned /// snapshots appended to the parent. /// </remarks> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void RemoveSnapshot(int timeoutInSeconds) { try { // resolve child snapshots that will move one level up IEnumerable <VMWareSnapshot> childSnapshots = ChildSnapshots; // remove the snapshot VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.RemoveSnapshot(_handle, 0, callback), callback)) { job.Wait(timeoutInSeconds); } // remove from parent if (_parent != null) { // child snapshots from this snapshot have now moved one level up _parent.ChildSnapshots.Remove(this); } Close(); } catch (Exception ex) { throw new Exception( string.Format("Failed to remove snapshot: timeoutInSeconds={0}", timeoutInSeconds), ex); } }
/// <summary> /// Create a new snapshot, child of the current snapshot. /// </summary> /// <param name="name">Snapshot name.</param> /// <param name="description">Snapshot description.</param> /// <param name="flags">Flags, one of /// <list type="bullet"> /// <item>VIX_SNAPSHOT_INCLUDE_MEMORY: Captures the full state of a running virtual machine, including the memory</item> /// </list> /// </param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public VMWareSnapshot CreateSnapshot(string name, string description, int flags, int timeoutInSeconds) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.CreateSnapshot( name, description, flags, null, callback), callback)) { ISnapshot snapshot = (ISnapshot)job.Wait <ISnapshot>( Constants.VIX_PROPERTY_JOB_RESULT_HANDLE, timeoutInSeconds); RemoveAll(); return(new VMWareSnapshot(_vm, snapshot, null)); } } catch (Exception ex) { throw new Exception( string.Format("Failed to create snapshot: name=\"{0}\" description=\"{1}\" flags={2} timeoutInSeconds={3}", name, description, flags, timeoutInSeconds), ex); } }
/// <summary> /// Restores the virtual machine to the state when the specified snapshot was created. /// </summary> /// <param name="powerOnOptions"> /// Any applicable VixVMPowerOpOptions. If the virtual machine was powered on when the snapshot was created, /// then this will determine how the virtual machine is powered back on. To prevent the virtual machine from being /// powered on regardless of the power state when the snapshot was created, use the /// VIX_VMPOWEROP_SUPPRESS_SNAPSHOT_POWERON flag. VIX_VMPOWEROP_SUPPRESS_SNAPSHOT_POWERON is mutually exclusive to /// all other VixVMPowerOpOptions. /// </param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void RevertToSnapshot(int powerOnOptions, int timeoutInSeconds) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.RevertToSnapshot( _handle, powerOnOptions, null, callback), callback)) { job.Wait(timeoutInSeconds); } } catch (Exception ex) { throw new Exception( string.Format("Failed to revert to snapshot: powerOnOptions={0} timeoutInSeconds={1}", powerOnOptions, timeoutInSeconds), ex); } }
/// <summary> /// Stop replaying a virtual machine's recording. /// </summary> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void EndReplay(int timeoutInSeconds) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.EndReplay( 0, null, callback), callback)) { job.Wait(timeoutInSeconds); } } catch (Exception ex) { throw new Exception( string.Format("Failed to end replay: timeoutInSeconds={0}", timeoutInSeconds), ex); } }
/// <summary> /// Replay a recording of a virtual machine. /// </summary> /// <param name="powerOnOptions">One of VIX_VMPOWEROP_NORMAL or VIX_VMPOWEROP_LAUNCH_GUI.</param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void BeginReplay(int powerOnOptions, int timeoutInSeconds) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.BeginReplay( _handle, powerOnOptions, null, callback), callback)) { job.Wait(timeoutInSeconds); } } catch (Exception ex) { throw new Exception( string.Format("Failed to begin replay: powerOnOptions={0} timeoutInSeconds={1}", powerOnOptions, timeoutInSeconds), ex); } }
/// <summary> /// Creates a copy of the virtual machine at the state at which this snapshot was taken. /// </summary> /// <param name="cloneType">Virtual Machine clone type.</param> /// <param name="destConfigPathName">The path name of the virtual machine configuration file that will be created.</param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> public void Clone(VMWareVirtualMachineCloneType cloneType, string destConfigPathName, int timeoutInSeconds) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.Clone( _handle, (int)cloneType, destConfigPathName, 0, null, callback), callback)) { job.Wait(timeoutInSeconds); } } catch (Exception ex) { throw new Exception( string.Format("Failed to clone virtual machine snapshot: cloneType=\"{0}\" destConfigPathName=\"{1}\" timeoutInSeconds={2}", Enum.GetName(cloneType.GetType(), cloneType), destConfigPathName, timeoutInSeconds), ex); } }
/// <summary> /// Add (create) a shared folder. /// </summary> /// <param name="sharedFolder">The shared folder to add.</param> public void Add(VMWareSharedFolder sharedFolder) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.AddSharedFolder( sharedFolder.ShareName, sharedFolder.HostPath, sharedFolder.Flags, callback), callback)) { job.Wait(VMWareInterop.Timeouts.AddRemoveSharedFolderTimeout); } _sharedFolders = null; } catch (Exception ex) { throw new Exception( string.Format("Failed to add shared folder: shareName=\"{0}\" hostPath=\"{1}\" flags={2}", sharedFolder.ShareName, sharedFolder.HostPath, sharedFolder.Flags), ex); } }
/// <summary> /// Delete a shared folder. /// </summary> /// <param name="item">Shared folder to delete.</param> /// <returns>True if the folder was deleted.</returns> public bool Remove(VMWareSharedFolder item) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.RemoveSharedFolder( item.ShareName, 0, callback), callback)) { job.Wait(VMWareInterop.Timeouts.AddRemoveSharedFolderTimeout); } return(SharedFolders.Remove(item)); } catch (Exception ex) { throw new Exception( string.Format("Failed to remove shared folder: shareName=\"{0}\"", item.ShareName), ex); } }
/// <summary> /// Connects to a VMWare VI Server, VMWare Server or Workstation. /// </summary> private void Connect(ServiceProviderType serviceProviderType, string hostName, int hostPort, string username, string password, int timeout) { try { int serviceProvider = (int)serviceProviderType; VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(new VixLib().Connect( Constants.VIX_API_VERSION, serviceProvider, hostName, hostPort, username, password, 0, null, callback), callback)) { _handle = job.Wait <IHost>(Constants.VIX_PROPERTY_JOB_RESULT_HANDLE, timeout); } _serviceProviderType = serviceProviderType; } catch (Exception ex) { throw new Exception( string.Format("Failed to connect: serviceProviderType=\"{0}\" hostName=\"{1}\" hostPort={2} username=\"{3}\" timeout={4}", Enum.GetName(serviceProviderType.GetType(), serviceProviderType), hostName, hostPort, username, timeout), ex); } }
/// <summary> /// Connects to a VMWare VI Server, VMWare Server or Workstation. /// </summary> private void Connect(ServiceProviderType serviceProviderType, string hostName, int hostPort, string username, string password, int timeout) { try { // check if VmWare COM object could be loaded // if we try to load a COM object which does not exist, an RCW exception will occure on exit of Visual Studio 2013 using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32)) { using (var xReg = baseKey.OpenSubKey(@"CLSID\{6874E949-7186-4308-A1B9-D55A91F60728}", false)) { if (xReg == null) { throw new ApplicationException("No Vmware Library installed!"); } } } int serviceProvider = (int)serviceProviderType; VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(new VixLib().Connect( Constants.VIX_API_VERSION, serviceProvider, hostName, hostPort, username, password, 0, null, callback), callback)) { _handle = job.Wait <IHost>(Constants.VIX_PROPERTY_JOB_RESULT_HANDLE, timeout); } _serviceProviderType = serviceProviderType; } catch (ApplicationException) { throw; } catch (Exception ex) { throw new Exception( string.Format("Failed to connect: serviceProviderType=\"{0}\" hostName=\"{1}\" hostPort={2} username=\"{3}\" timeout={4}", Enum.GetName(serviceProviderType.GetType(), serviceProviderType), hostName, hostPort, username, timeout), ex); } }
/// <summary> /// Open a virtual machine. /// </summary> /// <param name="fileName">Virtual Machine file, local .vmx or [storage] .vmx.</param> /// <param name="timeoutInSeconds">Timeout in seconds.</param> /// <returns>An instance of a virtual machine.</returns> public VMWareVirtualMachine Open(string fileName, int timeoutInSeconds) { try { if (_handle == null) { throw new InvalidOperationException("No connection established"); } VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_handle.OpenVM(fileName, callback), callback)) { return(new VMWareVirtualMachine(job.Wait <IVM2>( Constants.VIX_PROPERTY_JOB_RESULT_HANDLE, timeoutInSeconds))); } } catch (Exception ex) { throw new Exception( string.Format("Failed to open virtual machine: fileName=\"{0}\" timeoutInSeconds={1}", fileName, timeoutInSeconds), ex); } }