Installing a driver via pnputil

machine picture machine · Jun 1, 2016 · Viewed 26k times · Source

I'm trying to install an .inf file via NSIS like (Installing a driver in NSIS script).

Installation itself works smooth, but Windows installs the driver with its internal published name (an incrementing number oemxxx.inf).

How can I get pnputil.exe to give me the published name as return value (for later usage)?

Answer

machine picture machine · Jun 6, 2016

What I did to get the published drivername in nsis is this hell of a workaround:

  1. put the list of installed drivers to a text-file via pnputil /e > driverlist_before.txt
  2. install new driver via pnputil /i /a mydriver.inf
  3. put the list of installed drivers to a text-file via pnputil /e > driverlist_after.txt
  4. put following code in a .cmd file and execute it via nsExec

content of GetPublishedDrivername.cmd

@echo off
:: look at differences between files and just keep the line with the oem info
fc mydriverlist_before.txt mydriverlist_after.txt | findstr /C:"oem" > diff.txt
:: cut result and keep second part               "           oem##.inf"
for /f "tokens1,2 delims=:" %%a in (diff.txt) do (
  if "%%a"=="Published name " set info=%%b
)
:: get rid of leading spaces                     "oem##.inf"
for /f "tokens=* delims= " %%a in ("%info%") do set info=%%a
:: split "oem##.inf" and keep first part         "oem##"
for /f "tokens=1,2 delims=." %%a in ("%info%") do set info=%%a 
:: get of the oem part                           "##"
set info=%info:oem=%
:: convert string into int value
set /a info=%info%
del diff.txt
:: return number as result
exit /b %info%

This script can surely be optimized, every input is welcome.