寻找使用批处理脚本更新Windows驱动程序的方法

时间:2016-02-01 21:05:45

标签: windows batch-file cmd

我试图找到一种使用批处理脚本更新驱动程序的方法。我拼凑了一些不同的东西来获取系统中各种设备的硬件ID,但我找不到让Windows命令devcon使用脚本更新驱动程序的方法:

@ECHO OFF
cls

REM *** Checking for admin permissions ***
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM *** If error flag set, we do not have admin ***
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( 
    goto gotAdmin
)

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"


REM *** Variables ***
SET loc=\\artsfac.arts.ualberta.ca\install$\testing
SET IDFile=%loc%\All.txt
SET IDFileNum=%loc%\AllNum.txt
SET HWIDFile=%loc%\HWIDs.txt
SET SearchText="Hardware ID"

REM *** Checking connection to artsfac ***
net use \\artsfac.arts.ualberta.ca
cls

REM *** Checking if devcon is in System32 ***
IF NOT EXIST C:\Windows\System32\devcon.exe copy %loc%\devcon.exe c:\Windows\System32 > NUL 2>&1

REM *** Getting proper Hardware ID's ***
setlocal enabledelayedexpansion
ECHO.
ECHO Retrieving current Hardware ID's. Please wait...
devcon hwids * | findstr /n .* >> %IDFile%
for /f "tokens=1* delims=:" %%a in ('findstr /c:%SearchText% %IDFile%') do (
    set /a next=%%a+1
    ECHO !next! >> %IDFileNum%
)
for /f %%a in (%IDFileNum%) do (
    for /f "tokens=1* delims=:" %%b in (%IDFile%) do (
        if %%a==%%b (
            SET HWID=%%c
            SET HWID=!HWID: =!
            ECHO !HWID! >> %HWIDFile%
        )
    )   
)
endlocal
cls

REM *** Get current driver folder location ***
ECHO.
ECHO Enter full path for driver folder location (local drive only).
SET /P driverdir=": "

REM *** Update all listed devices ***
ECHO.
ECHO Updating drivers. This may take a while...
for /f "tokens=*" %%a in (%HWIDFile%) do (
    for /f "tokens=*" %%b in ('dir "%driverdir%\*.inf" /b /s') do (
        devcon updateni "%%a" "%%b" > NUL 2>&1
    )
)

REM *** Remove temp files ***
del %IDFile%
del %IDFileNum%
del %HWIDFile%

shutdown -r -f -t 20

因此,除了我称之为 devcon updateni 的部分之外,此代码中的所有内容都能正常工作(尽管方式相当丑陋)。所有我得到的(如果我删除重定向)是一大堆" devcon失败"消息,没有任何更新。但是,如果我使用其中一个硬件ID和它需要的特定INF文件并将它们提供给devcon updateni命令,它就可以工作。那么,我在这里做错了什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

@Mofi有使用Driver Package Installer(DPInst.exe)安装驱动程序包的解决方案。

要彻底,我最后编写的脚本可以运行:

@ECHO OFF
SET loc=<location of the files>

REM *** Checking for 32bit or 64bit ***
IF EXIST "%PROGRAMFILES(X86)%" (
    set drivercmd=%loc%\dpinst64.exe
) ELSE (
    set drivercmd=%loc%\dpinst32.exe
)

ECHO Please type the full path to the driver folder.
SET /P folder=": "
ECHO.

ECHO Installing drivers. Please wait...
for /f "tokens=*" %%a in ('dir /a:d /s /b "%folder%"') do (
    %drivercmd% /s /sh /path "%%a"
) 

shutdown -r -f -t 0