如何将 INF 文件描述的 Windows 驱动程序导入Configuration Manager
可以使用类SMS_Driver中的 CreateFromINF 方法导入 Configuration Manager (.inf) 文件中的信息描述的 Windows 驱动程序。
导入 Windows 驱动程序
设置与 SMS 提供程序的连接。 有关详细信息,请参阅 SMS 提供程序基础知识。
在类SMS_Driver中调用 CreateFromINF 方法以获取初始SMS_Driver服务器 WMI 类管理基对象。
使用管理基对象创建 SMS_Driver 实例。
填充
SMS_Driver
对象。SMS_Driver
提交 对象。
示例
以下示例方法使用提供的路径和文件名为 Windows 驱动程序创建 SMS_Driver
对象。 该示例还通过将 属性的值 IsEnabled
设置为 来 true
启用驱动程序。 帮助程序函数 GetDriverName
用于从驱动程序包 XML 获取驱动程序的名称。
注意
参数 path
必须作为通用命名约定 (UNC) 网络路径提供,例如 \\localhost\Drivers\ATIVideo\。
在此示例中,属性 LocaleID
硬编码为英语 (美国) 。 如果需要非美国区域设置。安装,可以从 SMS_Identification 服务器 WMI 类LocaleID
属性获取它。
有关调用示例代码的信息,请参阅调用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;
}
示例方法具有以下参数:
参数 | 类型 | 说明 |
---|---|---|
connection |
-管理: WqlConnectionManager - VBScript: SWbemServices |
与 SMS 提供程序的有效连接。 |
path |
-管理: String - VBScript: String |
包含驱动程序内容的文件夹的有效 UNC 网络路径。 例如,\\Servers\Driver\VideoDriver。 |
name |
-管理: String - VBScript: String |
.inf 文件的名称。 例如 ATI.inf。 |
编译代码
此 C# 示例需要:
命名空间
System
System.Collections.Generic
System.Text
Microsoft。ConfigurationManagement.ManagementProvider
Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine
Assembly
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
可靠编程
有关错误处理的详细信息,请参阅关于Configuration Manager错误。
.NET Framework 安全性
有关保护Configuration Manager应用程序的详细信息,请参阅Configuration Manager基于角色的管理。
另请参阅
类SMS_Driver中的 CreateFromINF 方法
SMS_Driver服务器 WMI 类
如何为驱动程序指定受支持的平台