You create a software metering rule, in System Center 2012 Configuration Manager, by creating an instance of the SMS_MeteredProductRule class and populating the properties.
To create software metering rule
-
Set up a connection to the SMS Provider.
-
Create the new software metering rule object by using the SMS_MeteredProductRule class.
-
Populate the new software metering rule properties.
-
Save the new software metering rule and properties.
Example
The following example method shows how to create a software metering rule by creating an instance of the SMS_MeteredProductRule class and populating the properties.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
Visual Basic Script | Copy Code |
---|---|
Sub CreateSWMRule(connection, _ newProductName, _ newFileName, _ newOriginalFileName, _ newFileVersion, _ newLanguageID, _ newSiteCode, _ newApplyToChildSites) ' Create the new MeteredProductRule object. Set newSWMRule = connection.Get("SMS_MeteredProductRule").SpawnInstance_ ' Populate the SMS_MeteredProductRule properties. newSWMRule.ProductName= newProductName newSWMRule.FileName = newFileName newSWMRule.OriginalFileName = newOriginalFileName newSWMRule.FileVersion = newFileVersion newSWMRule.LanguageID = newLanguageID newSWMRule.SiteCode = newSiteCode newSWMRule.ApplyToChildSites = newApplyToChildSites ' Save the new rule and properties. newSWMRule.Put_ ' Output new rule name. Wscript.Echo "Created new SWM Rule: " & newProductName End Sub |
C# | Copy Code |
---|---|
public void CreateSWMRule(WqlConnectionManager connection, string newProductName, string newFileName, string newOriginalFileName, string newFileVersion, int newLanguageID, string newSiteCode, bool newApplyToChildSites) { try { // Create the new SMS_AuthorizationList object. IResultObject newSWMRule = connection.CreateInstance("SMS_MeteredProductRule"); // Populate the new SMS_MeteredProductRule object properties. newSWMRule["ProductName"].StringValue = newProductName; newSWMRule["FileName"].StringValue = newFileName; newSWMRule["OriginalFileName"].StringValue = newOriginalFileName; newSWMRule["FileVersion"].StringValue = newFileVersion; newSWMRule["LanguageID"].IntegerValue = newLanguageID; newSWMRule["SiteCode"].StringValue = newSiteCode; newSWMRule["ApplyToChildSites"].BooleanValue = newApplyToChildSites; // Save changes. newSWMRule.Put(); Console.WriteLine(); Console.WriteLine("Created new SWM Rule: " + newProductName); } catch (SmsException ex) { Console.WriteLine("Failed to create SWM rule. Error: " + ex.Message); throw; } } |
The example method has the following parameters:
Parameter |
Type |
Description |
connection |
|
A valid connection to the SMS Provider. |
newProductName |
|
The new product name. |
newFileName |
|
The new file name. |
newOriginalFileName |
|
The new original file name. |
newFileVersion |
|
The new file version. |
newLanguageID |
|
The new language ID. |
newSiteCode |
|
The new site code. |
newApplyToChildSites |
|
Determines whether the rule will apply to child sites. |
Compiling the Code
This C# example requires:
Namespaces
System
System.Collections.Generic
System.Text
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
adminui.wqlqueryengine
microsoft.configurationmanagement.managementprovider
Robust Programming
For more information about error handling, see About Configuration Manager Errors.
Security
For more information about securing Configuration Manager applications, see Securing Configuration Manager Applications.