Sunday, February 16, 2014

Identify the only drivers a machine needs for the OSD driver import process in Configuration Manager

Last week I talked about using a regex expression to find a syntax error in a directory containing all of my scripts. This week, I'll use the same method to find all of the driver INF files, needed for a given machine. This will allow me to provide a specific list of driver INF files to use for importing into Configuration Manager.


The Problem

One of the more tedious aspects of preparing new hardware for OS deployment with Configuration Manager is the driver import process. The process involves identifying the driver, downloading or extracting it to a central location, and then importing it. Rinse and repeat until all of the exclamation marks disappear in the Device Manager. This can make for a long morning and the temptation to grab a full driver cab and import the whole thing and be done with it is great. But then you are left with a bloated driver package where most of the drivers aren't even required. 

The Solution

I can't help you avoid having to download a bunch of drivers but with the help of the script discussed here, we can identify precisely what your computer needs and only import those drivers. Thus creating a much smaller and manageable driver store. 

In order to do this, the script needs to:
  1. Identify all of the hardware Names and DeciveIDs in Win32_PNPEntity
  2. Toggle the ability to only search for unknown devices
  3. Extract a string from the DeviceID to be used as our regex espression
  4. Recursively search a given directory for INF files that contain our regex expression
  5. Return a device object with an array of INF file locations for each found device INF
Here's the output:
PS C:\> & "D:\Sandbox\Driver_INF_File_Search\Driver_INF_File_Search.ps1" C:\Dell
DeviceName : Intel(R) Mobile Express Chipset SATA RAID Controller SearchPattern : DEV_282A FileArray : {C:\Dell\E6530\win8\x64\storage\2PWDK_A01-00\Drivers\AHCI\x64\iaStorAC.inf, C:\Dell\E6530\win8\x86\storage\2PWDK_A01-00\Drivers\AHCI\x32\iaStorAC.inf}

Preparation

The one caveat to this process is that in order to be able to search for the driver, it must be present in the given directory or subdirectory. This means we'll still have to download the drivers manually before we use the script.
Some vendors provide full driver cabs for their machines. If your vendor provides them, download and extract them to the file system, then use the script to identify only those that your machine needs. 

Querying Win32_PNPEntity

To get the deviceIDs we use the Win32_PNPEntity class in WMI. Using this class also allows us to find devices that are marked unknown through the use of the ConfigManagerErrorCode property. The WMI query isn't terribly complex so let's move on to the file search routine.

File search routine

Once we get the list of IDs to search for we need to extract a string from them to use for the search because there is no place in the INF file that contains the entire deviceID string. On top of that, there are a few different forms that the ID can take. This script manages to find IDs that contain "&DEV" and "DLL". I haven't worked out the other possibilities but these two values cover a lot of ground.

The following routine utilizes regex to first find an eligible value to extract a string from and then uses another regex to identify the sting to extract. It then assigns that string to the $Pattern variable to be used in the select-string statement. All matched deviceIDs are then searched for, discarding those that were not modified by the regex statements.

#--Assign the deviceid to the pattern for evaluation            
$Pattern = $Device.DeviceID            
                
#--Evaluate $Pattern for occurrence of "&DEV" or "DLL"            
switch -regex ($Pattern) {            
   "&DEV" {$Pattern -match 'DEV_\w+[^&]' | out-null;$PatternModified = $True}
   "DLL"  {$Pattern -match 'DLL\w+[^\\]' | out-null;$PatternModified = $True} 
}           
#--Search directory for INF files with the occurance of $Pattern            
If ($PatternModified) {            
   #--Set new regex pattern            
   $Pattern = $matches[0]            
   #--Find files that contain the extracted pattern            
   $Result = Get-ChildItem $Directory -include *.inf -recurse | select-string -pattern $Pattern | Select-Object -Unique Path            
}

Returning unique file locations

While the derived search term is very successful in being found, it occurs in the INF file many times. This means that every time it is found, it will be returned to the query resulting in many references to the same file for a single device. We only want to return unique file locations. The way we accomplish this is by piping the returned result through the Select-Object cmdlet like this:
Select-Object -Unique Path
In preparation for writing an automated import process for Configuration Manager, I return objects rather than writing the results to the host. "When in doubt, use PSobjects"  - Abe Lincoln

That's it for this week. Hopefully some Configuration Manager admins will find this method useful in taming the driver beast.

Here is a link to the script on Github:
Driver_INF_File_Search.ps1 

And here is a link to the script on the Technet gallery:

http://gallery.technet.microsoft.com/Driver-Inf-File-Search-for-fe8a95cc


The Script

#========================================================================            
# Date              : 2/16/2014 4:49 PM            
# Author            : Jeff Pollock            
# Website           : http://lifeinpowershell.blogspot.com/            
#             
# Description       : Assists in identifying driver INF files for import            
#                     into Configuration Manager 2007 & 2012. It does this            
#                     by searching a given computer for all PNP devices            
#                     and then searching a given directory for the related            
#                     INF files.             
#========================================================================            
Param (            
    [parameter(Mandatory=$true,ValueFromPipeline=$True)]            
    [string]$SearchDir,  #Directory to search            
            
    [parameter(ValueFromPipeline=$True)]            
    [string]$Computer = ".",  #Computer to search            
            
    [parameter(ValueFromPipeline=$True)]            
    [switch]$UnknownOnly  #Determines whether to return all devices or just unknown            
)            
            
#----------------------------------------------            
#region Functions            
#----------------------------------------------            
Function Get-PNPDevices {            
    [cmdletbinding()]            
    Param (            
        [bool]$UnknownOnly  #-determines whether to return all devices or just unknown                  
    )            
            
    #--Query Win32_PNPEntity for devices            
    If (!$UnknownOnly) {            
        $Devices = Get-WmiObject -ComputerName $Computer Win32_PNPEntity |
        Select Name, DeviceID            
    } Else {            
        $Devices = Get-WmiObject -ComputerName $Computer Win32_PNPEntity |
        Where-Object{$_.ConfigManagerErrorCode -ne 0} | Select Name, DeviceID    
    }            
            
    #--Return device objects            
    ForEach ($Device in $Devices) {            
        $DeviceObj = New-Object -Type PSObject            
        $DeviceObj | Add-Member -MemberType NoteProperty -Force -Name DeviceName -Value $Device.Name
        $DeviceObj | Add-Member -MemberType NoteProperty -Force -Name DeviceID -Value $Device.DeviceID
        $DeviceObj                    
    }            
}            
            
Function Search-DeviceINF {            
    [cmdletbinding()]            
 Param(            
     [parameter(Mandatory=$true,ValueFromPipeline=$True)]            
     [string]$Directory,  #Directory to search

     [parameter(Mandatory=$true,ValueFromPipeline=$True)]            
     [object]$Device  #Device object            
 )            
                
    #--Create array to hold returned file names            
    $FileArray = @()            
                
    #--Assign the deviceid to the pattern for evaluation            
    $Pattern = $Device.DeviceID            
                
    #--Evaluate $Pattern for occurance of "&DEV" or "DLL"            
    switch -regex ($Pattern) {            
        "&DEV" {$Pattern -match 'DEV_\w+[^&]' | out-null;$PatternModified = $True}            
        "DLL"  {$Pattern -match 'DLL\w+[^\\]' | out-null;$PatternModified = $True}            
    }            
            
    #--Search directory for INF files with the occurance of $Pattern            
    If ($PatternModified) {            
        #--Set new regex pattern            
        $Pattern = $matches[0]            
        #--Find files that contain the extracted pattern            
        $Result = Get-ChildItem $Directory -include *.inf -recurse |
        select-string -pattern $Pattern | Select-Object -Unique Path            
    }            
            
    #--Output results            
    If($Result) {            
        #--Add returned files to the FileArray            
        $Result | ForEach-Object {$FileArray += $_.Path}            
            
        #Create object and output            
        $DeviceObj = New-Object -Type PSObject            
        $DeviceObj | Add-Member -MemberType NoteProperty -Force -Name DeviceName -Value $Device.DeviceName
        $DeviceObj | Add-Member -MemberType NoteProperty -Force -Name SearchPattern -Value $Pattern
        $DeviceObj | Add-Member -MemberType NoteProperty -Force -Name FileArray -Value $FileArray            
        $DeviceObj | format-list            
    }             
}            
#endregion Application Functions            
            
#----------------------------------------------            
# region Script            
#----------------------------------------------            
#--Set Unknown switch            
If ($UnknownOnly) {            
    [bool]$Unknown = $True            
} Else {            
    [bool]$Unknown = $False            
}            
            
#--Perform query            
Get-PNPDevices $Unknown | ForEach-Object {              
   Search-DeviceINF $SearchDir $_             
}            
#endregion Script

No comments:

Post a Comment