How to Enable or Disable a Software Metering Rule

 

Updated: November 1, 2013

Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager

You enable or disable a software metering rule, in System Center 2012 R2 Configuration Manager, by loading the instance of the software metering rule that is identified by the software metering rule ID and then setting the Enabled value.

To enable or disable a software metering rule

  1. Set up a connection to the SMS Provider.

  2. Load the software metering rule object by using the SMS_MeteredProductRule class and a known software metering rule ID.

  3. Set the Enabled property to true or false.

Example

The following example method shows how to enable or disable a software metering rule by loading the instance of the software metering rule that is identified by the software metering rule ID and setting the Enabled property.

System_CAPS_importantImportant

The rule ID corresponds to the value that is stored in the property RuleID. The Configuration Manager console displays a Rule ID column, which actually corresponds to the value that is stored in the property SecurityID.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.


' Enable or disable a software metering rule.
 Sub EnableDisableSoftwareMeteringRule(connection,          _
                                      existingSWMRuleID,    _
                                      enableDisableSWMRule)                         

    ' Get an existing software metering rule to enable or disable. 
    Set existingSWMRule = connection.Get("SMS_MeteredProductRule.RuleID='" & existingSWMRuleID & "'")  

    ' Get file name for output.
    fileName = existingSWMRule.FileName

    ' Enable or disable the rule.
    existingSWMRule.Enabled = enableDisableSWMRule

    ' Save the new rule and properties.
    existingSWMRule.Put_ 

    ' Output a success message.
    Wscript.Echo "SWM rule ID:    " & existingSWMRuleID
    Wscript.Echo "Rule name:      " & fileName
    Wscript.Echo "Set enabled to: " & enableDisableSWMRule

 End Sub

public void EnableDisableSoftwareMeteringRule(WqlConnectionManager connection,
                                              string existingSWMRuleID,
                                              bool enableDisableSWMRule)
{
    try
    {
        // Get the specific software metering rule to enable or disable.
        IResultObject existingSWMRule = connection.GetInstance(@"SMS_MeteredProductRule.RuleID='" + existingSWMRuleID + "'");

        // Get rule name for output message.
        string productName = existingSWMRule["ProductName"].StringValue;

        // Set the software metering rule.
        existingSWMRule["Enabled"].BooleanValue = enableDisableSWMRule;

        // Save changes.
        existingSWMRule.Put();

        // Output a success message.
        Console.WriteLine("SWM rule ID: " + existingSWMRuleID);
        Console.WriteLine("Rule name: " + productName);
        Console.WriteLine("Set enabled to: " + enableDisableSWMRule);
    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed to modify software metering rule. Error: " + ex.Message);
        throw;
    }
}

The example method has the following parameters:

Parameter

Type

Description

connection

  • Managed: WqlConnectionManager

  • VBScript: SWbemServices

A valid connection to the SMS Provider.

existingSWMRuleID

  • Managed: String

  • VBScript: String

Identifies a specific software metering rule. In this case, identifies the specific software metering rule that will be enabled or disabled.

enableDisableSWMRule

  • Managed: Boolean

  • VBScript: Boolean

Enables or disables the software metering rule.

true - Enabled

false - Disabled

Compiling the Code

This C# example requires:

System

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

.NET Framework Security

For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.

Show: