Saturday, August 27, 2016

OSD - Removing Unknown Devices


Unknown Devices.  A common plague everyone who deals with OSD has seen quite a few times.  Now usually the fix is to identify the missing drivers through the HWID and then prepare them in SCCM for an injection into the OSD process.  Other times you need to get a mallet instead.  This post will be about the mallet.

So, I ran across a client that recently created a Windows 10 image through Hyper-V with a Build and Capture process.  It was verified that there was no Hyper-V Integration Services in the image but there was still a number of Unknown Devices that were related to Hyper-V when checking the HWIDs.

The quickest resolution is to simply remove them.  Microsoft has a utility named "Devcon.exe" that can be run from command-line to work with drivers.  If you download and install the Windows Driver Kit for Windows 10 you will get access to "Devcon.exe" as well as various other utilities.  If you browse to "C:\Program Files(x86)\Windows Kits\10\Tools\X64" you will find the utility we need.  Save it in a folder.

The second piece is we need to utilize a VBscript to gather all unknown devices and then remove them utilizing "Devcon.exe".  Below is said VBscript:

-------------------------------------------------------------------------------------------------------------------------

On Error Resume Next
Dim objWMIService, objItem, colItems, strComputer
Dim wshShell: Set wshShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

StartupPath = objFSO.GetParentFolderName(wscript.ScriptFullName)

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_PNPEntity where Status=" & chr(34) & "Error" & chr(34))

For Each objItem in colItems
    wshShell.run StartupPath & "\devcon.exe remove @" & ObjItem.DeviceID, 0, True
Next

WScript.Quit

--------------------------------------------------------------------------------------------------------------------------

The real meat of this VBscript is in the middle.  The line "Set colItems = objWMIService.ExecQuery("Select * from Win32_PNPEntity where Status=" & chr(34) & "Error" & chr(34))" uses WMI to query all devices with a status of Error.  You can test this out manually by running "WBEMTEST", connecting to a namespace, and then running the query "Select * from Win32_PNPEntity where Status="Error"".  Or you can even run that query without the "where Status="Error"" if you were just curious to see all listings.  From that listing you can double click on one and then scroll down until you see Status.

In the next lines of the script above we have it run through each item we found earlier and run the command "devcon.exe remove @" and after the "@" symbol it will place the device ID it gathered.  Again, you can run this manually by gathering that device ID first and then running that command.

Now, save this VBscript with the name "UnknownDevices-X64.vbs" in the same folder that you saved the utility "Devcon.exe".  Now lets just put it all together to make it usable in your OSD task sequence.  Create a package in SCCM and point the source location to the folder where you placed the utility and the script.  You do not need to create a program to be associated with this package as we will run it from the Task Sequence in SCCM.

Near the end of the task sequence (after applying OS and drivers, while its in the Windows Operating System not WINPE) create a new step for "Run Command Line".  In the "Command Line:" section entered the code "Cscript.exe UnknownDevices-X64.vbs".  Check the box labeled "Package" and point it to the package that you created earlier.  And thats that.


Now when you run the Task Sequence it will image, inject drivers, and do all the usual steps that you've previously configured and at the very end it will run the removal script.  What you will notice is that, assuming all your other drivers inject properly, you will no longer have any unknown devices in device manager.


No comments:

Post a Comment