QueryMetricCollectionEnabledForVirtualMachine(
            string name)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // Retrieve the Msvm_ComputerSystem and the CIM_BaseMetricDefinition derived instance
            // that we want to query the MetricCollectionEnabled state for.
            //
            SelectQuery  metricDefForMeQuery;
            const string metricDefinitionName = "Aggregated Average CPU Utilization";

            using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope))
                using (ManagementObject metricDefinition =
                           MetricUtilities.GetMetricDefinition(metricDefinitionName, scope))
                {
                    //
                    // Build the WQL query used to retrieve the Msvm_MetricDefForME association between
                    // these two objects. It is the one that contains the MetricCollectionEnabled
                    // property.
                    //
                    string metricDefForMeQueryWql = string.Format(CultureInfo.InvariantCulture,
                                                                  "SELECT * FROM Msvm_MetricDefForME WHERE Antecedent=\"{0}\" AND Dependent=\"{1}\"",
                                                                  WmiUtilities.EscapeObjectPath(vm.Path.Path),
                                                                  WmiUtilities.EscapeObjectPath(metricDefinition.Path.Path));

                    metricDefForMeQuery = new SelectQuery(metricDefForMeQueryWql);
                }

            using (ManagementObjectSearcher metricDefForMeSearcher =
                       new ManagementObjectSearcher(scope, metricDefForMeQuery))
                using (ManagementObjectCollection metricDefForMeCollection =
                           metricDefForMeSearcher.Get())
                {
                    //
                    // There will always only be one Msvm_MetricDefForME for a given managed element and
                    // metric definition.
                    //
                    if (metricDefForMeCollection.Count != 1)
                    {
                        throw new ManagementException(string.Format(CultureInfo.CurrentCulture,
                                                                    "A single Msvm_MetricDefForME could not be found for virtual machine " +
                                                                    "\"{0}\" and metric definition \"{1}\"", name, metricDefinitionName));
                    }

                    foreach (ManagementObject metricDefForMe in metricDefForMeCollection)
                    {
                        using (metricDefForMe)
                        {
                            string metricCollectionState = Enum.Parse(typeof(MetricEnabledState),
                                                                      metricDefForMe["MetricCollectionEnabled"].ToString()).ToString();

                            Console.WriteLine("MetricCollectionEnabled = {0}", metricCollectionState);
                        }
                    }
                }
        }
        DisableMetricsForNetworkAdapter(
            string macAddress,
            string ipAddress)
        {
            ManagementScope scope = new ManagementScope(@"root\virtualization\v2");

            //
            // 1. Retrieve the Msvm_SyntheticEthernetPortSettingData associated with the specified
            //    MAC address.
            // 2. Retrieve the associated Msvm_EthernetPortAllocationSettingData, which
            //    corresponds to the connection.
            // 3. Retrieve the associated Msvm_EthernetSwitchPortAclSettingData with the specified
            //    IP address. This is the object that metrics are associated with.
            //
            string portAclSettingDataPath;

            using (ManagementObject portSettingData =
                       MetricUtilities.GetSyntheticEthernetPortSettingData(macAddress, scope))
                using (ManagementObject connectionSettingData =
                           MetricUtilities.GetEthernetPortAllocationSettingData(portSettingData, scope))
                    using (ManagementObject portAclSettingData =
                               MetricUtilities.GetEthernetSwitchPortAclSettingData(connectionSettingData, ipAddress, scope))
                    {
                        portAclSettingDataPath = portAclSettingData.Path.Path;
                    }

            //
            // Retrieve the Msvm_BaseMetricDefinition for the Filtered Incoming Network Traffic.
            //
            string metricDefinitionPath;

            using (ManagementObject metricDefinition =
                       MetricUtilities.GetMetricDefinition("Filtered Incoming Network Traffic", scope))
            {
                metricDefinitionPath = metricDefinition.Path.Path;
            }

            //
            // Call the Msvm_MetricService::ControlMetrics method.
            //
            ControlMetrics(portAclSettingDataPath, metricDefinitionPath,
                           MetricOperation.Disable, scope);

            Console.WriteLine(string.Format(CultureInfo.CurrentCulture,
                                            "The metric was successfully disabled for network adapter \"{0}\" (\"{1}\")",
                                            macAddress, ipAddress));
        }