1、安装NSIS
2、安装HM NIS Edit
3、按向导生成安装包
一切都很简单。开始正文,主要是我踩的坑。
一、由于是安装驱动。需要管理员权限。
在脚本MUI end----下面加上:
RequestExecutionLevel admin ;
二、运行报错了把?
记得用管理员权限启动HM NIS Edit
三、感谢大神:huanggang982,第一次看到链接没有学会。最后还是参考的您的链接。
四、pnputil /add-driver *.inf /install报错。请留意两个地方:
1、是否用管理员权限运行
2、驱动是否已签名。如果未签名,管理员权限运行 bcdedit /set testsigning on。
五、大神的脚本,记得在目录下创建createDiff.cmd
@echo off
pnputil /enum-drivers > driverlist_before.txt
pnputil /add-driver *.inf /install
pnputil /enum-drivers > driverlist_after.txt
fc driverlist_after.txt driverlist_before.txt | findstr /C:"oem" > diff.txt
六、安装脚本:
nsExec::ExecToLog '$INSTDIR\createDiff.cmd'
特别提醒:
1、记得这个目录不要是带空格的。
2、64位系统需要加上:
${DisableX64FSRedirection}
${EnableX64FSRedirection}
七、如果不需要卸载,就够用了。如果需要,加上下面的脚本。找到安装好的驱动的名字。
;store three driver oem name
var driver1
var driver2
var driver3
!include "TextFunc.nsh"
...
Function findTarget
${LineSum} "$INSTDIR\diff.txt" $R9
;MessageBox MB_OK $R9
StrCpy $R8 1
${While} $R8
${LineRead} "$INSTDIR\diff.txt" $R8 $R7
;MessageBox MB_OK $R7
;first just push
${If} $R8 == 1
push $R7
;MessageBox MB_OK 'first push $R7'
;then pop first compare with new, if not equal push two
${Else}
pop $0
;MessageBox MB_OK 'pop $0'
StrCmp $0 $R7 +3 0
push $0
push $R7
;for debug
;StrCmp $0 $R7 +5 0
;push $0
;MessageBox MB_OK '$0 $R7 not equal push $0'
;push $R7
;MessageBox MB_OK '$0 $R7 not equal push $R7'
${EndIf}
IntOp $R8 $R8 + 1
${EndWhile}
;pop and get "oem#.inf\r\n" string
pop $driver1
${WordFind} $driver1 " " "-1}" $R0
StrCpy $driver1 $R0
;MessageBox MB_OK 'final pop $str1'
pop $driver2
${WordFind} $driver2 " " "-1}" $R0
StrCpy $driver2 $R0
;MessageBox MB_OK 'final pop $str2'
pop $driver3
${WordFind} $driver3 " " "-1}" $R0
StrCpy $driver3 $R0
;MessageBox MB_OK 'final pop $driver3'
;write to file InstallInfo.txt
ClearErrors
FileOpen $0 "$INSTDIR\InstallInfo.txt" w
IfErrors done
FileWrite $0 $driver1
FileWrite $0 $driver2
FileWrite $0 $driver3
FileClose $0
done:
FunctionEnd
八、卸载脚本:
!include "WordFunc.nsh"
...
Function un.uninstallDrivers
ClearErrors
FileOpen $0 "$INSTDIR\InstallInfo.txt" r
IfErrors done
FileRead $0 $driver1
FileRead $0 $driver2
FileRead $0 $driver3
FileClose $0
;cut off last "\r\n" string
${WordReplace} $driver1 "$\r$\n" "" "-" $driver1
${WordReplace} $driver2 "$\r$\n" "" "-" $driver2
${WordReplace} $driver3 "$\r$\n" "" "-" $driver3
${DisableX64FSRedirection}
nsExec::ExecToLog '"$SYSDIR\PnPutil.exe" /delete-driver $driver1 /uninstall /force'
nsExec::ExecToLog '"$SYSDIR\PnPutil.exe" /delete-driver $driver2 /uninstall /force'
nsExec::ExecToLog '"$SYSDIR\PnPutil.exe" /delete-driver $driver3 /uninstall /force'
${EnableX64FSRedirection}
done:
FunctionEnd