共用方式為


如何將 INF 檔案所描述的 Windows 驅動程式匯入 Configuration Manager

您可以使用類別SMS_Driver中的 CreateFromINF 方法,在 Configuration Manager 中匯入 (.inf) 檔案所描述的 Windows 驅動程式。

匯入 Windows 驅動程式

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

  2. 類別SMS_Driver中呼叫 CreateFromINF 方法 ,以取得初始 SMS_Driver伺服器 WMI 類別 管理基底物件。

  3. 使用管理基底物件建立 SMS_Driver 的實例。

  4. 填入 SMS_Driver 物件。

  5. SMS_Driver認可物件。

範例

下列範例方法 SMS_Driver 會使用提供的路徑和檔名,建立 Windows 驅動程序的物件。 此範例也會藉由將 屬性的 IsEnabled 值設定為 來 true啟用驅動程式。 協助程式函 GetDriverName 式是用來從驅動程式套件 XML 取得驅動程式的名稱。

注意事項

參數 path 必須以通用命名約定 (UNC) 網路路徑提供,例如 \\localhost\Drivers\ATIVideo\。

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

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

Sub ImportINFDriver(connection, path, name)  

    Dim driverClass  
    Dim inParams  
    Dim outParams  

    On Error Resume Next  

    ' Obtain an instance of the class   
    ' using a key property value.  

    Set driverClass = connection.Get("SMS_Driver")  

    ' Obtain an InParameters object specific  
    ' to the method.  
    Set inParams = driverClass.Methods_("CreateFromINF"). _  
        inParameters.SpawnInstance_()  

    ' Add the input parameters.  
    inParams.Properties_.Item("DriverPath") =  path  
    inParams.Properties_.Item("INFFile") =  name  

    ' Call the method.  
    ' The OutParameters object in outParams  
    ' is created by the provider.  
    Set outParams = connection.ExecMethod("SMS_Driver", "CreateFromINF", inParams)  

   If Err <> 0 Then  
        Wscript.Echo "Failed to add to the driver catalog: " + path + "\" + name  
        Exit Sub  
    End If      

    outParams.Driver.IsEnabled =  True  

    Dim LocalizedSettings(0)  
    Set LocalizedSettings(0) = connection.Get("SMS_CI_LocalizedProperties").SpawnInstance_()  
    LocalizedSettings(0).Properties_.item("LocaleID") =  1033   
    LocalizedSettings(0).Properties_.item("DisplayName") = _  
            GetDriverName(outParams.Driver.SDMPackageXML, "//DisplayName", "Text")  

    LocalizedSettings(0).Properties_.item("Description") = ""          
    outParams.Driver.LocalizedInformation = LocalizedSettings  

    ' Save the driver.  
    outParams.Driver.Put_  

End Sub  

Function GetDriverName(xmlContent, nodeName, attributeName)  
    ' Load the XML Document  
    Dim attrValue  
    Dim XMLDoc  
    Dim objNode  
    Dim displayNameNode  

    attrValue = ""  
    Set XMLDoc = CreateObject("Microsoft.XMLDOM")       
    XMLDoc.async = False  
    XMLDoc.loadXML(xmlContent)  

    'Check for a successful load of the XML Document.  
    If xmlDoc.parseError.errorCode <> 0 Then  
        WScript.Echo vbcrlf & "Error loading XML Document. Error Code : 0x" & hex(xmldoc.parseerror.errorcode)  
        WScript.Echo "Reason: " & xmldoc.parseerror.reason  
        WScript.Echo "Parse Error line " & xmldoc.parseError.line & ", character " & _  
                      xmldoc.parseError.linePos & vbCrLf & xmldoc.parseError.srcText  

        GetXMLAttributeValue = ""          
    Else  
        ' Select the node  
        Set objNode = xmlDoc.SelectSingleNode(nodeName)  

        If Not objNode Is Nothing Then  
            ' Found the element, now just pick up the Text attribute value  
            Set displayNameNode = objNode.attributes.getNamedItem(attributeName)  
            If Not displayNameNode Is Nothing Then  
               attrValue = displayNameNode.value  
            Else  
               WScript.Echo "Attribute not found"  
            End If  
        Else  
            WScript.Echo "Failed to locate " & nodeName & " element."  
        End If  
    End If  

    ' Save the results  
    GetDriverName = attrValue  
End Function  
public void ImportInfDriver(  
    WqlConnectionManager connection,   
    string path,   
    string name)  
{  
    try  
    {  
        Dictionary<string, object> inParams = new Dictionary<string, object>();  

        // Set up parameters for the path and file name.  
        inParams.Add("DriverPath", path);  
        inParams.Add("INFFile", name);  

        // Import the INF file.  
        IResultObject result = connection.ExecuteMethod("SMS_Driver", "CreateFromINF", inParams);  

        // Create the SMS_Driver driver instance from the management base object returned in result["Driver"].  
        IResultObject driver = connection.CreateInstance(result["Driver"].ObjectValue);  

        // Enable the driver.  
        driver["IsEnabled"].BooleanValue = true;  

        List<IResultObject> driverInformationList = driver.GetArrayItems("LocalizedInformation");  

        // Set up the display name and other information.  
        IResultObject driverInfo = connection.CreateEmbeddedObjectInstance("SMS_CI_LocalizedProperties");  
        driverInfo["DisplayName"].StringValue = GetDriverName(driver);  
        driverInfo["LocaleID"].IntegerValue = 1033;  
        driverInfo["Description"].StringValue = "";  

        driverInformationList.Add(driverInfo);  

        driver.SetArrayItems("LocalizedInformation", driverInformationList);  

        // Commit the SMS_Driver object.  
        driver.Put();  
    }  
    catch (SmsException e)  
    {  
        Console.WriteLine("Failed to import driver: " + e.Message);  
        throw;  
    }  
}  

public string GetDriverName(IResultObject driver)          
{  
    // Extract   
    XmlDocument sdmpackage = new XmlDocument();  

    sdmpackage.LoadXml(driver.Properties["SDMPackageXML"].StringValue);  

    // Iterate over all the <DisplayName/> tags.  
    foreach (XmlNode displayName in sdmpackage.GetElementsByTagName("DisplayName"))           
    {                  
    // Grab the first one with a Text attribute not equal to null.  
        if (displayName != null && displayName.Attributes["Text"] != null    
            && !string.IsNullOrEmpty(displayName.Attributes["Text"].Value))    
        {                        
                // Return the DisplayName text.  
                return displayName.Attributes["Text"].Value;      
        }              
    }   
    // Default the driverName to the UniqueID.  
    return driver["CI_UniqueID"].StringValue;         
 }  

範例方法具有下列參數:

參數 Type 描述
connection -管理: WqlConnectionManager
- VBScript: SWbemServices
SMS 提供者的有效連線。
path -管理: String
- VBScript: String
包含驅動程式內容之資料夾的有效 UNC 網路路徑。 例如,\\Servers\Driver\VideoDriver。
name -管理: String
- VBScript: String
.inf 檔案的名稱。 例如 ATI.inf。

正在編譯程式碼

此 C# 範例需要:

命名空間

系統

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

組件

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

健全的程式設計

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

.NET Framework 安全性

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

另請參閱

類別SMS_Driver中的 CreateFromINF 方法
SMS_Driver伺服器 WMI 類別
如何指定驅動程式支持的平臺