共用方式為


如何使用 WMI 讀取和寫入 Configuration Manager 月臺控制檔案

在 Configuration Manager 中,您可以使用類別方法,使用 Windows Management Instrumentation (WMI) 寫入SMS_SiteControlFile月臺控制檔案。

使用 WMI 寫入月臺控制檔案時,您可以使用工作階段句柄來識別您的應用程式。 這是用來管理檔案的並行更新。

當您完成寫入月臺控制檔案時,必須認可您的變更。

SMS_SiteControlFile 具有下列方法來管理網站控制檔案的變更。

方法 描述
CommitSCF 將您的變更套用至 Configuration Manager 資料庫。
RefreshSCF 使用來自 Configuration Manager 資料庫的任何最近變更,重新整理月臺控制檔案的記憶體內部複本。
GetSessionHandle 取得月臺控制檔案和會話句柄的記憶體內部複本。 您會將會話句柄 IWbemContext 放在傳遞至所有 IWbemServices 方法的物件中。
ReleaseSessionHandle 釋放月臺控制檔案的記憶體內部複本,以及與您的會話句柄相關聯的任何資源。

注意

在使用SMS提供者類別來修改月臺設定之前,您應該要有管理月臺設定的經驗。 變更一些可設定的專案,可能會對網站造成極大的傷害。 您應該特別小心,或完全避免使用 SMS_SCI_FileDefinitionSMS_SCI_SiteDefinition 類別。 這些類別會管理月臺控制檔案本身。 如果您不小心,可以將網站轉譯為無用。

寫入月臺控制檔案

  1. 設定與SMS提供者的連線。 如需詳細資訊,請 參閱SMS提供者基本概念

  2. 建立值 SWbemNameValue 集以保存內容數據。

  3. SMS_SiteControlFile 類別取得會話句柄 GetSessionHandle

  4. 將會話句柄新增至內容數據。

  5. SMS_SiteControlFile呼叫 物件RefreshSCF以取得月臺控制檔案的最新複本。 在呼叫中使用內容數據。

  6. 使用內容數據查詢您想要更新的月臺控制檔案資源。

  7. 使用您的內容數據更新資源。

  8. 使用物件CommitSCF方法將變更認可至SMS_SiteControlFile月臺控制檔案。

  9. SMS_SiteControlFile呼叫 物件ReleaseSessionHandle方法以釋放您的會話句柄。

範例

下列 VBScript 範例會存取月臺控制檔案的用戶端代理程式元件,並建立虛擬屬性、屬性清單和多字串清單。 然後,它會移除已進行的更新。 此範例示範如何設定會話句柄、取得月臺控制檔案、查詢月臺控制檔案、對月臺控制檔案進行更新和認可變更。

在此範例中 LocaleID , 屬性會硬式編碼為英文 (美國 ) 。 如果您需要非美國地區設定安裝時,您可以從 SMS_Identification Server WMI ClassLocaleID 屬性取得它。

如需呼叫範例程式代碼的資訊,請參閱呼叫 Configuration Manager 代碼段

Sub ReadWriteScf(connection, siteCode)

    Dim context
    Dim query
    Dim resource
    Dim resources
    Dim inParams

    Set context = CreateObject("WbemScripting.SWbemNamedValueSet")

    ' Add the standard SMS context qualifiers to the context object.
    context.Add "LocaleID", "MS\1033"
    context.Add "MachineName", "MyMachine"
    context.Add "ApplicationName", "MyApp"

    ' Add the session handle.
    context.Add "SessionHandle", _
         connection.ExecMethod("SMS_SiteControlFile", "GetSessionHandle").SessionHandle

   ' Load site control file.
       Set inParams = connection.Get("SMS_SiteControlFile").Methods_("RefreshSCF").InParameters.SpawnInstance_
InParams.SiteCode = siteCode
connection.ExecMethod "SMS_SiteControlFile", "RefreshSCF", inParams, , context

    ' Query for the client agent component.
    query = "SELECT * FROM SMS_SCI_ClientComp " & _
            "WHERE ClientComponentName = 'Client Agent' " & _
           "AND SiteCode = '" & siteCode & "'"

    Set resources = connection.ExecQuery(query, , , context)

    For each resource in resources

    ' Embedded property.

        WScript.Echo "Embedded property"
        Wscript.Echo "-----------------"

        Dim value
        Dim value1
        Dim value2

        Call WriteScfEmbeddedProperty(connection,context,resource,"Test2",20,"Hello","World")

        If  GetScfEmbeddedProperty(resource,"Test2",value,value1,value2) = True Then
            Wscript.Echo "Value: " + CStr(value)
            WScript.Echo "Value1: " + value1
            WScript.Echo "Value2: " + value2
        End If

        WScript.Echo
        dim n,l
        dim updatedProps
        Dim scfProp

        n = 0
        ' Remove the property.
        For l = 0 To UBound (resource.Props)

            ' Copy each element except the one to delete.
            If resource.Props(l).PropertyName <> "Test2" Then
                Dim embeddedProperty
                Set embeddedProperty = connection.Get("SMS_EmbeddedProperty").Spawninstance_()
                If l = 0 Then
                    ' Create an array to copy to.
                    updatedProps = array(embeddedProperty)
                    Redim updatedProps(Ubound(resource.Props)-1)
                End If
                ' Copy the element.
                embeddedProperty.PropertyName = resource.Props(l).PropertyName
                embeddedProperty.Value = resource.Props(l).value
                embeddedProperty.Value1 = resource.Props(l).value1
                embeddedProperty.Value2 = resource.Props(l).value2

                Set updatedProps(n) = embeddedProperty
                n = n + 1
          End If
        Next

        ' Update
        resource.Props = updatedProps
        resource.Put_, context

        WScript.Echo

        ' Check that the property has been deleted.
        If  GetScfEmbeddedProperty(resource,"Test2",value,value1,value2) = True Then
            WScript.Echo "Property found"
        Else
            WScript.Echo "Property not found"
        End If

        WScript.Echo

    ' Embedded property list.

        WScript.Echo "Embedded property list"
        WScript.Echo "----------------------"

        Dim values
        values = Array("Tiger","Wolf")

        Call WriteScfEmbeddedPropertyList(connection,context,resource,"Animals",values)

        Dim retrievedValues

        If GetScfEmbeddedPropertyList(resource,"Animals",retrievedValues) = True Then
            Dim i,c
            Dim updatedValues

            c = 0

            ' Display the list and remove the property Tiger.
            updatedValues = Array(UBound(retrievedValues)-1)
            For i = 0 To  UBound (retrievedValues)
                 Wscript.Echo retrievedValues(i)
                 If retrievedValues(i) <> "Tiger" Then

                    updatedValues(c) = retrievedValues(i)
                    c = c + 1
                 End If
            Next

            WScript.Echo
            ' Update the property list.
            Call WriteScfEmbeddedPropertyList(connection,context,resource,"Animals",updatedValues)

            ' Get the property list and display.
            Call GetScfEmbeddedPropertyList(resource,"Animals",retrievedValues)

            For i = 0 To  UBound (retrievedValues)
                 Wscript.Echo retrievedValues(i)
             Next
        Else
            WScript.Echo "Not found"
        End If

        WScript.Echo

    ' RegMultiString list.

        WScript.Echo "Embedded RegMultiString list"
        WScript.Echo "----------------------------"

        Dim valueStrings
        valueStrings= Array("Lisa","Julie")

        ' Write the RegMultiString list.
        Call WriteScfRegMultiStringList(connection,context,resource,"Names2",valueStrings)

        Dim retrievedValueStrings

        ' Get the RegMultiString list.
        If GetScfRegMultiStringList(resource,"Names2",retrievedValueStrings) = True Then

            Dim updatedValueStrings

            c = 0
            updatedValueStrings = Array(Ubound(retrievedValueStrings)-1)
            For i = 0 To UBound (retrievedValueStrings)
                 Wscript.Echo retrievedValueStrings(i)
                 if retrievedValueStrings(i) <> "Lisa" Then
                    updatedValueStrings(c) = retrievedValueStrings(i)
                 End If
            Next

            Call WriteScfRegMultiStringList(connection,context,resource,"Names",updatedValueStrings)

            WScript.Echo

            Call GetScfRegMultiStringList(resource,"Names",retrievedValueStrings)

            For i = 0 To UBound (retrievedValueStrings)
                 Wscript.Echo retrievedValueStrings(i)
             Next
        Else
            WScript.Echo "Not found"
        End If
    Next

    ' Commit the changes.
    Set inParams = connection.Get("SMS_SiteControlFile").Methods_("CommitSCF").InParameters.SpawnInstance_
    inParams.SiteCode = siteCode
    connection.ExecMethod "SMS_SiteControlFile", "CommitSCF", inParams, , context

    ' Release the session handle.
    Set inParams = connection.Get("SMS_SiteControlFile").Methods_("ReleaseSessionHandle").InParameters.SpawnInstance_
    inParams.SessionHandle = context.Item("SessionHandle")
    connection.ExecMethod "SMS_SiteControlFile", "ReleaseSessionHandle", inParams
End Sub

範例方法具有下列參數:

參數 Type 描述
connection - SWbemServices SMS 提供者的有效連線。
siteCode - String Configuration Manager 月臺的月臺碼。

正在編譯程式碼

此 C# 範例需要:

命名空間

系統

System.Collections.Generic

System.Collections

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

健全的程式設計

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

.NET Framework 安全性

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

另請參閱

Windows Management Instrumentation關於 Configuration Manager 月臺控件檔案如何讀取 Configuration Manager 月臺控件檔案內嵌屬性列表