private void ShutdownVirtualMachine(MachineDescription environment, ITestSectionBuilder sectionBuilder)
        {
            var specification = environment as HypervMachineDescription;
            if (specification == null)
            {
                throw new InvalidEnvironmentSpecificationException();
            }

            // See if the host machine is alive
            var host = specification.HostMachineId;
            var hostSpecification = m_EnvironmentById(host);
            if (hostSpecification == null)
            {
                sectionBuilder.AddErrorMessage(
                            string.Format(
                                CultureInfo.InvariantCulture,
                                "Could not locate VM host: {0}",
                                hostSpecification.NetworkName));
                sectionBuilder.FinalizeAndStore(false);

                throw new CouldNotLoadEnvironmentException();
            }

            Diagnostics.Log(
                LevelToLog.Debug,
                HyperVConstants.LogPrefix,
                string.Format(
                    CultureInfo.InvariantCulture,
                    "Terminating Hyper-V virtual machine {0} [image: {1}] on host {2}",
                    specification.Name,
                    specification.Image,
                    hostSpecification.Name));

            // Connect to the Hyper-V host for the given environment
            var virtualMachine = new HypervVirtualMachine(specification.Image);
            try
            {
                virtualMachine.Terminate();
            }
            catch (FailedToRestoreEnvironmentException)
            {
                sectionBuilder.AddErrorMessage(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Failed to shut down virtual machine: {0}",
                        specification.NetworkName));
                sectionBuilder.FinalizeAndStore(false);

                throw;
            }

            var killTime = DateTimeOffset.Now + TimeSpan.FromMilliseconds(GlobalConstants.DefaultMaximumMachineShutdownTime);
            while ((virtualMachine.State != HypervVirtualMachineState.TurnedOff) && (DateTimeOffset.Now <= killTime))
            {
                Thread.Sleep(10);
            }

            try
            {
                virtualMachine.RestoreToSnapshot(specification.SnapshotToReturnTo);
            }
            catch (FailedToRestoreEnvironmentException)
            {
                sectionBuilder.AddErrorMessage(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Failed to restore virtual machine snapshot [{0}] for machine {1}",
                        specification.SnapshotToReturnTo,
                        specification.NetworkName));
                sectionBuilder.FinalizeAndStore(false);

                throw;
            }

            Diagnostics.Log(
                LevelToLog.Debug,
                HyperVConstants.LogPrefix,
                string.Format(
                    CultureInfo.InvariantCulture,
                    "Hyper-V virtual machine {0} terminated",
                    specification.Name));

            sectionBuilder.AddInformationMessage(
                string.Format(
                    CultureInfo.InvariantCulture,
                    "Virtual machine shut down: {0}",
                    specification.NetworkName));
            sectionBuilder.FinalizeAndStore(true);
        }
        private static void KillAndResetVirtualMachine(HypervVirtualMachine virtualMachine, HypervMachineDescription specification)
        {
            virtualMachine.Terminate();

            DateTimeOffset killTime = DateTimeOffset.Now + TimeSpan.FromMilliseconds(GlobalConstants.DefaultMaximumMachineShutdownTime);
            while ((virtualMachine.State != HypervVirtualMachineState.TurnedOff) && (DateTimeOffset.Now <= killTime))
            {
                Thread.Sleep(10);
            }

            virtualMachine.RestoreToSnapshot(specification.SnapshotToReturnTo);
        }