共用方式為


如何設定自動軟體計量規則產生

您可以藉由修改月臺控制檔案,在 Configuration Manager 中設定自動軟體計量規則產生設定。

重要事項

此設定會跨整個階層共用,而且只能在 CAS 或獨立主要站臺上設定。

設定自動軟體計量規則產生

  1. 設定與SMS提供者的連線。

  2. 使用 SMS_SCI_ClientComp 類別, 連線到月臺控制檔案的 [軟體計量用戶端代理程式] 區段。

  3. Loop 可用屬性的陣列,視需要進行變更。

  4. 將屬性變更認可至月臺控制檔案。

範例

下列範例方法會使用 SMS_SCI_ClientComp 類別來連線到月臺控制檔案並變更屬性,以設定各種軟體計量規則產生設定。

如需呼叫範例程式代碼的相關信息,請參閱呼叫 Configuration Manager 代碼段


Sub ConfigureAutomaticSWMRuleGeneration(swbemServices,                  _
                                        swbemContext,                   _
                                        siteCode,                       _
                                        enableAutoCreateDisabledRule,   _
                                        newAutoCreatePercentage,        _
                                        newAutoCreateThreshold)

    ' Load site control file and get the SMS_SCI_ClientComp section.
    swbemServices.ExecMethod "SMS_SiteControlFile.Filetype=1,Sitecode=""" & siteCode & """", "Refresh", , , swbemContext

    Query = "SELECT * FROM SMS_SCI_ClientComp " &   _
    "WHERE ClientComponentName  = 'Software Metering Agent'" & _
    "AND SiteCode = '" & siteCode & "'"

     Set SCIComponentSet = swbemServices.ExecQuery(Query, ,wbemFlagForwardOnly Or wbemFlagReturnImmediately, swbemContext)

    ' Only one instance is returned from the query.
    For Each SCIComponent In SCIComponentSet

        'Loop through the array of embedded SMS_EmbeddedProperty instances.
        For Each vProperty In SCIComponent.Props

            ' Setting: Auto Create Disabled Rule
            If vProperty.PropertyName = "Auto Create Disabled Rule" Then
                wscript.echo " "
                wscript.echo vProperty.PropertyName
                wscript.echo "Current value " &  vProperty.Value

                'Modify the value.
                vProperty.Value = enableAutoCreateDisabledRule
                wscript.echo "New value " & enableAutoCreateDisabledRule
            End If

            ' Setting: Auto Create Percentage
            If vProperty.PropertyName = "Auto Create Percentage" Then
                wscript.echo " "
                wscript.echo vProperty.PropertyName
                wscript.echo "Current value " &  vProperty.Value

                ' Modify the value.
                vProperty.Value = newAutoCreatePercentage
                wscript.echo "New value " & newAutoCreatePercentage
            End If

            ' Setting: Auto Create Threshold
            If vProperty.PropertyName = "Auto Create Threshold" Then
                wscript.echo " "
                wscript.echo vProperty.PropertyName
                wscript.echo "Current value " &  vProperty.Value

                ' Modify the value.
                vProperty.Value = newAutoCreateThreshold
                wscript.echo "New value " & newAutoCreateThreshold
            End If

        Next

        ' Update the component in your copy of the site control file. Get the path
        ' to the updated object, which could be used later to retrieve the instance.
        Set SCICompPath = SCIComponent.Put_(wbemChangeFlagUpdateOnly, swbemContext)

    Next

    ' Commit the change to the actual site control file.
    Set InParams = swbemServices.Get("SMS_SiteControlFile").Methods_("CommitSCF").InParameters.SpawnInstance_
    InParams.SiteCode = siteCode
    swbemServices.ExecMethod "SMS_SiteControlFile", "CommitSCF", InParams, , swbemContext

End Sub


public void ConfigureAutomaticSWMRuleGeneration(WqlConnectionManager connection,
                                                string siteCode,
                                                string enableAutoCreateDisabledRule,
                                                string newAutoCreatePercentage,
                                                string newAutoCreateThreshold)
{
    try
    {
        IResultObject siteDefinition = connection.GetInstance(@"SMS_SCI_ClientComp.FileType=1,ItemType='Client Component',SiteCode='" + siteCode + "',ItemName='Software Metering Agent'");

        foreach (KeyValuePair<string, IResultObject> kvp in siteDefinition.EmbeddedProperties)
        {
            // Create temporary working copy of embedded properties.
            Dictionary<string, IResultObject> embeddedProperties = siteDefinition.EmbeddedProperties;

            //Console.WriteLine(kvp.Value.PropertyList["PropertyName"]);

            // Setting: Auto Create Disabled Rule
            if (kvp.Value.PropertyList["PropertyName"] == "Auto Create Disabled Rule")
            {
                Console.WriteLine();
                Console.WriteLine(kvp.Value.PropertyList["PropertyName"]);
                Console.WriteLine("Current value: " + embeddedProperties["Auto Create Disabled Rule"]["Value"].StringValue);

                // Change value using the enableAutoCreateDisabledRule value passed in.
                embeddedProperties["Auto Create Disabled Rule"]["Value"].StringValue = enableAutoCreateDisabledRule;
                Console.WriteLine("New value    : " + enableAutoCreateDisabledRule);
            }

            // Setting: Auto Create Percentage
            if (kvp.Value.PropertyList["PropertyName"] == "Auto Create Percentage")
            {
                Console.WriteLine();
                Console.WriteLine(kvp.Value.PropertyList["PropertyName"]);
                Console.WriteLine("Current value: " + embeddedProperties["Auto Create Percentage"]["Value"].StringValue);

                // Change value using the newAutoCreatePercentage value passed in.
                embeddedProperties["Auto Create Percentage"]["Value"].StringValue = newAutoCreatePercentage;
                Console.WriteLine("New value    : " + newAutoCreatePercentage);
            }

            // Setting: Auto Create Threshold
            if (kvp.Value.PropertyList["PropertyName"] == "Auto Create Threshold")
            {
                Console.WriteLine();
                Console.WriteLine(kvp.Value.PropertyList["PropertyName"]);
                Console.WriteLine("Current value: " + embeddedProperties["Auto Create Threshold"]["Value"].StringValue);

                // Change value using the newAutoCreateThreshold value passed in.
                embeddedProperties["Auto Create Threshold"]["Value"].StringValue = newAutoCreateThreshold;
                Console.WriteLine("New value    : " + newAutoCreateThreshold);
            }

            // Store the settings that have changed.
            siteDefinition.EmbeddedProperties = embeddedProperties;
        }

        // Save the settings.
        siteDefinition.Put();

    }

    catch (SmsException ex)
    {
        Console.WriteLine("Failed. Error: " + ex.InnerException.Message);
        throw;
    }
}

範例方法具有下列參數:

參數 Type 描述
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
SMS 提供者的有效連線。
swbemContext - VBScript: SWbemContext 有效的內容物件。 如需詳細資訊,請參閱如何使用WMI新增 Configuration Manager 內容限定符
siteCode -管理: String
- VBScript: String
月臺碼。
enableAutoCreateDisabledRule -管理: String
- VBScript: String
啟用或停用軟體計量自動規則建立。

- 0 - 已停用
- 1 - 已啟用
newAutoCreatePercentage -管理: String
- VBScript: String
設定自動建立百分比。

0 -100
newAutoCreateThreshold -管理: String
- VBScript: String
設定自動建立閾值。

正在編譯程式碼

此 C# 範例需要:

命名空間

系統

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

健全的程式設計

如需錯誤處理的詳細資訊,請參閱關於 Configuration Manager 錯誤

.NET Framework 安全性

如需保護 Configuration Manager 應用程式的詳細資訊,請參閱 Configuration Manager 角色型系統管理

另請參閱

Configuration Manager 軟體開發工具包:關於 Configuration Manager 月臺控制檔案如何使用 Managed 程式代碼讀取和寫入 Configuration Manager 網站控制檔案如何讀取和寫入至使用 WMI SMS_SCI_Component伺服器 WMI 類別 Configuration Manager 月臺控制檔案