This script can be used to import drivers into the driver catalog for a Configuration Manager 2007 site. This script, which requires a Configuration Manager 2007 site server, was contributed by Jim Bradbury.

Visual Basic
Edit|Remove
'
' This script can be used to import drivers into the driver catalog for a ConfigMgr 2007 site.
' Usage:
'     cscript AddDrivers <UNC Path>
'     cscript AddDrivers <UNC Path> <INFFile>
'
' Verify Arguments
IF WScript.Arguments.Count <> 1 AND WScript.Arguments.Count <> 2  THEN
    WScript.Echo "Usage: cscript AddDrivers <UNC Path>"
    WScript.Echo "       cscript AddDrivers <UNC Path> <INFFile>"
    WScript.Echo "       cscript AddDrivers <UNC Path> txtsetup.oem"
    WScript.Quit 1
END IF
' Connect to the SMS namespace
siteNamespace = GetSiteNamespace()
WScript.Echo "Namespace = "&siteNamespace 
SET objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!"&siteNamespace)
IF WScript.Arguments.Count = 1 THEN
    ' Create a file scripting object and recursively add drivers to the driver catalog
    SET FSO=CreateObject("scripting.filesystemobject")
    SET objDir = FSO.GetFolder(WScript.Arguments.Item(0))
    AddDrivers(objDir)
ELSE
    AddDriver WScript.Arguments.Item(0), WScript.Arguments.Item(1)
END IF
'
' This function can be used to recursively add all drivers found in the 
' given directory to the ConfigMgr driver catalog
'
FUNCTION AddDrivers( byval currentDir )
    ' See if there is a txtsetup.oem file in this directory
    hasBootCriticalDriver = false
    FOR EACH file IN currentDir.Files
        IF LCase(file.Name) = "txtsetup.oem" THEN
            hasBootCriticalDriver = true
        END IF
    NEXT
    
    ' If we found a boot critical driver in this directory
    IF hasBootCriticalDriver = true THEN
        ' Just add this mass storage driver
        AddDriver currentDir.Path, "txtsetup.oem"
    ELSE     
        ' Import all INF files in this directory
        FOR EACH file IN currentDir.Files
            ' If this is an INF file
            IF LCase(Right(Cstr(file.Name), 3)) = "inf" THEN
                ' Add driver to the catalog
                AddDriver currentDir.Path, file.Name
            END IF
        NEXT
        ' Recurse on each sub directory
        FOR EACH subDir IN currentDir.SubFolders
            AddDrivers( subDir )
        NEXT
    END IF    
END FUNCTION
'
' This function adds a driver to the driver catalog
'
FUNCTION AddDriver( driverPath, fileName )
    ' Get in parms for the CreateFromINF function
    SET objSMSDriver = objWMIService.Get("SMS_Driver")
    
    IF LCase(Right(fileName, 3)) = "inf" THEN
        SET objInParam = objSMSDriver.Methods_("CreateFromINF").inParameters.SpawnInstance_()
        ' Set the driver path and INF file
        objInParam.Properties_.item("DriverPath") = driverPath
        objInParam.Properties_.item("INFFile") = fileName
        ' Execute the method
        On Error Resume Next
        SET objOutParams = objSMSDriver.ExecMethod_("CreateFromINF", objInParam)    
        
        IF Err <> 0 Then
            Wscript.Echo "Failed to add "&driverPath&"\"&fileName&" to the driver catalog ("&Err.number&")"
            PrintLastError        
        ELSE
            ' Set driver name and enabled flag then save it
            PopulateDriverName( objOutParams.Driver )
            objOutParams.Driver.Properties_.item("IsEnabled") = true            
            objOutParams.Driver.Put_             
            
            IF Err <> 0 Then
                Wscript.Echo "Failed to add "&driverPath&"\"&fileName&" to the driver catalog ("&Err.number&")"
                PrintLastError        
            ELSE
                WScript.Echo "Successfully added "&driverPath&"\"&fileName&" to the driver catalog"
            END IF
            
        END IF           
    ELSEIF LCase(fileName) = "txtsetup.oem" THEN
        ' Get in parms for the CreateFromINF function        
        SET objInParam = objSMSDriver.Methods_("CreateFromOEM").inParameters.SpawnInstance_()
        ' Set the driver path and INF file
        objInParam.Properties_.item("DriverPath") = driverPath
        objInParam.Properties_.item("OEMFile") = fileName
        ' Execute the method
        On Error Resume Next
        SET objOutParams = objSMSDriver.ExecMethod_("CreateFromOEM", objInParam)    
        
        IF Err <> 0 Then
            Wscript.Echo "Failed to add "&driverPath&"\"&fileName&" to the driver catalog ("&Err.number&")"
            PrintLastError        
        ELSE          
            WScript.Echo "Successfully parsed "&driverPath&"\"&fileName&"."
            
            FOR EACH driver IN objOutParams.Drivers
                ' Set driver name and enable the driver
                PopulateDriverName( driver )
                driver.Properties_.item("IsEnabled") = true
                driver.Put_ 
                WScript.Echo "    Successfully added driver referenced by "&driverPath&"\"&fileName& _
                    " to the driver catalog"
            NEXT            
        END IF
    ELSE
        WScript.Echo "Warning: Unknown driver type "&fileName&"."        
    END IF    
    
    ' Reset error handling
    On Error GoTo 0
    
END FUNCTION
'
' Logs extended error information
'
FUNCTION PrintLastError
    SET ExtendedStatus = CreateObject("WbemScripting.SWBEMLastError")
     
    ' Determine the type of error
    IF ExtendedStatus.Path_.Class = "__ExtendedStatus" Then
        WScript.Echo "    WMI Error: "&ExtendedStatus.Description            
    ELSEIF ExtendedStatus.Path_.Class = "SMS_ExtendedStatus" Then
        WScript.Echo "    SMS Provider Error"
        
        IF ExtendedStatus.ErrorCode = 183 THEN
            WScript.Echo "        Error Code: 183 (driver exists)"
            WScript.Echo "        Skipping Add operation - the driver is already available for use by " & _
                "ConfigMgr 2007."
        ELSE
            WScript.Echo "        Description: "&ExtendedStatus.Description
            WScript.Echo "        Error Code:  "&ExtendedStatus.ErrorCode
        END IF
    END IF
    
    WScript.Echo " "
    
END FUNCTION 
'
' GetXMLAttributeValue – Get the attribute value from a specific element in an XML document.
'
FUNCTION GetXMLAttributeValue(xmlContent, nodeName, attributeName)
    ' Load the XML Document
    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
    GetXMLAttributeValue = attrValue
END FUNCTION
'
' Set the LocalizedInformation for the driver
'
FUNCTION PopulateDriverName( driver )
    ' Set the driver name
    DIM LocalizedSettings(0)
    SET LocalizedSettings(0) = objWMIService.Get("SMS_CI_LocalizedProperties").SpawnInstance_()
    LocalizedSettings(0).Properties_.item("LocaleID") = GetSiteLocaleID()
    LocalizedSettings(0).Properties_.item("DisplayName")  = GetXMLAttributeValue(driver.Properties_.item _
        ("SDMPackageXML"), "//DisplayName", "Text")     
    LocalizedSettings(0).Properties_.item("Description") = ""        
    driver.Properties_.item("LocalizedInformation") = LocalizedSettings
END FUNCTION
'
' Get the site's locale Id
'
FUNCTION GetSiteLocaleID()
    LocaleID = ""
    
    ' Get the SMS_Identification object
    SET objIdentificationSet = objWMIService.InstancesOf("SMS_Identification")
    
    FOR EACH objIdentification IN objIdentificationSet       
        ' Read the LocaleID
        LocaleID = objIdentification.LocaleID
    NEXT
    
    ' Return the Locale ID
    GetSiteLocaleID = LocaleID
END FUNCTION
'
' Utility function to search for the site code
'
FUNCTION GetSiteNamespace()
    ' Find SMS Provider
    WScript.Echo "Searching for SMS Provider for local site..."
    SET objSMSNamespace = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\sms")
    SET results = objSMSNamespace.ExecQuery("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")
    ' Process the results
    FOR EACH r IN results    
        namespacePath = r.NamespacePath
    NEXT
    ' Fail if we did not find the site
    IF namespacePath = "" THEN
        WScript.Echo "Failed to locate SMS provider."
        WScript.Quit 1
    END IF
    
    ' Return
    GetSiteNamespace = namespacePath
END FUNCTION