Windows Batch + VBS: 自動安裝 / 移除軟體
參考
http://jamyy.dyndns.org/blog/author/admin
by Jamyy on 四月.28, 2012, under Coding
執行 action.bat 自動移除 FortiClient Lite 防毒軟體, 重開機後自動安裝 Avira Free AntiVirus
↓ action.bat
view source
print?
01 @echo off
02 echo Please wait…
03
04 REM 複製小紅傘安裝程式到 temp 資料夾
05 copy "%~dp0avira_free_antivirus_zhtw.exe" %temp% >nul
06
07 REM 匯入防止小紅傘出現廣告視窗的機碼; 這部分的機碼是在群組原則 (gpedit.msc) 設置路徑規則所得
08 reg import "%~dp0policies.reg" >nul
09
10 REM 開機後自動安裝小紅傘防毒軟體; 若不以 cmd /c start 方式執行則必須等安裝完成後才能進入桌面
11 reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /f /v installer /t REG_SZ /d "cmd /c start %temp%\avira_free_antivirus_zhtw.exe" >nul
12
13 REM 匯出 Uninstall 清單 (控制台 → "新增或移除程式" 內容的機碼)
14 reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /s > %temp%\uninstall.txt
15
16 REM 以 VBScript 接手處理剛剛匯出的檔案
17 wscript "%~dp0script.vbs"
————————————————————————–
script.vbs
view source
print?
01 set objShell = CreateObject("WScript.Shell")
02 set objFSO = CreateObject("Scripting.FileSystemObject")
03 strTempPath = objShell.ExpandEnvironmentStrings("%temp%") ‘取得環境變數
04
05 ‘讀入 uninstall.txt
06 set objFile = objFSO.OpenTextFile(strTempPath & "\uninstall.txt", 1) ’0=Append, 1=Read, 2=Write
07
08 ‘尋找 FortiClient Lite 的移除資訊 (UninstallString)
09 strLine = "" ‘存放逐行讀入的內容
10 strRegistry = "" ‘組合單一機碼底下的所有內容
11 boolFlag = False ‘是否已經找到 DisplayName = FortiClient Lite
12
13 Do Until objFile.AtEndOfStream
14 ‘逐行讀入檔案內容
15 strLine = objFile.ReadLine
16 If InStr(strLine, "HKEY_LOCAL_MACHINE") = 1 Then
17 ‘如果已經找到 FortiClient Lite 機碼就結束迴圈
18 If boolFlag Then Exit Do
19 ‘清空 strRegistry 以便開始組合該機碼底下的所有數值項目
20 strRegistry = ""
21 ElseIf InStr(strLine, "DisplayName") > 0 and InStr(strLine, "FortiClient Lite") > 0 Then
22 ‘找到 FortiClient Lite, 將 boolFlag 設為 True
23 boolFlag = True
24 End If
25 ‘以 Chr(13) 作為字串轉陣列的分隔字元
26 strRegistry = strRegistry & strLine & Chr(13)
27 Loop
28
29 strUninst = "" ‘存放反安裝指令
30 If boolFlag Then
31 ‘boolFlag = True 表示系統裝有 FortiClient Lite, 找出反安裝指令來進行移除作業
32 aryRegistry = Split(strRegistry, Chr(13))
33 For i = 0 To Ubound(aryRegistry) – 2
34 If InStr(aryRegistry(i), "UninstallString") > 0 Then
35 strUninst = Mid(aryRegistry(i), InStr(aryRegistry(i), "REG_EXPAND_SZ") + 14)
36 ‘將原本 /I{機碼} 改成 /x{機碼} /quiet /forcerestart 默默的在背景進行移除作業並強制重開機
37 strUninst = Replace(strUninst, "/I", "/x") & " /quiet /forcerestart"
38 Exit For
39 End If
40 Next
41 ‘執行反安裝指令
42 objShell.Run "cmd /c " & strUninst, 0, True ’0=背景執行, True=等候作業完成
43 Else
44 ‘不需移除 FortiClient Lite 就直接安裝小紅傘防毒軟體
45 objShell.Run "cmd /c reg delete HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v installer /f", 0 ’0=背景執行
46 objShell.Run strTempPath & "\avira_free_antivirus_zhtw.exe"
47 End If
————————————————————————–
policies.reg
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths]
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths\{2cb84e55-d2e8-4dd7-b317-8ea6e897c630}]
"LastModified"=hex(b):00,b1,3d,da,8c,23,cd,01
"Description"=""
"SaferFlags"=dword:00000000
"ItemData"="C:\\Program Files\\Avira\\AntiVir Desktop\\avnotify.exe"
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers\0\Paths\{7caba076-33a4-4e0d-9e65-9fa7ce1fbe5c}]
"LastModified"=hex(b):b0,07,3d,e8,8c,23,cd,01
"Description"=""
"SaferFlags"=dword:00000000
"ItemData"="C:\\Program Files\\Avira\\AntiVir Desktop\\avnotify.dll"
olicies.reg 額外說明:
匯入後雖然無法在 gpedit.msc 看到這兩個路徑規則, 但確實是有效的
可應用在 Windows XP Pro (32位元) & Windows 7 Ultimate (32位元), 其餘版本沒試過
—
參考
http://jamyy.dyndns.org/blog/author/admin