2010年1月21日 星期四

如何移除「服務」

移除服務的方式有下列幾種:
1. 撰寫 Script 程式:
若您知道您要移除什麼服務的話,建議使用這個方式,但請注意:
若您移除了核心的服務,將可能造成系統嚴重的問題

---程式碼開始---
Const title = "服務移除工具"

Set oWS = CreateObject("Wscript.Shell")
sService = inputbox("請輸入欲移除的服務名稱",title,"Service_name")

If sService = "" then
msgbox "程式停止執行,沒有移除任何服務。", vbInformation, title
wscript.quit
End If

'//確認是否移除服務
result = MsgBox ("即將移除 " & sService & " 這個服務,您確定嗎?", vbQuestion + vbYesno, title)
If result = vbNo Then
Msgbox "程式停止執行,沒有移除任何服務。", vbInformation, title
wscript.quit
End If

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'//檢查要移除的服務是否存在
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = '" & sService & "' or
displayName = '" & sService & "'")
If colListOfServices.count > 0 Then
For Each objService In colListOfServices
objService.StopService()
'//暫停 5 秒鐘,以確保服務已停止
wscript.Sleep 5000
objService.Delete()
Msgbox sService & " 已移除或已被標示成刪除。", vbInformation, title
Next
Else
Msgbox "找不到 " & sService & " 這個服務。", vbInformation, title
End If
---程式碼結束---

2. 使用 Windows 2000/2003 Resource Kit:
Resource Kit 裡有兩個程式可以將服務移除:instsrv.exe 與 srvinstw.exe 。

srvany.exe 需要透過 srvinstw.exe 或 instsrv.exe 把它安裝成服務,而且還要手動編輯機碼。
srvinstw.exe 或 instsrv.exe 可以安裝和刪除服務,而 srvany.exe 可以讓程式以系統服務方式運行。


其命令列語法如下 :

  • instsrv <服務名稱> <可執行檔的完整路徑與檔案名稱>

  • instsrv <服務名稱> remove


  • 這兩種語法分別用於安裝及刪除服務,其中:
    <服務名稱> 為要建立的服務之名稱 ,若名稱內含空白字元,須以雙引號(")將整個名稱包起來 。
    <可執行檔的完整路徑與檔案名稱> 為欲安裝的服務其可執行檔的完整路徑與檔案名稱 ,請注意:
    一定要輸入完整路徑

    remove 指定要進行移除服務的程式,記得
    在移除前請先停止該服務

    若經此命令安裝的服務尚須透過控制台的服務圖示或使用 NET START 指令將服務手動啟動,而使用這種方式安裝的服務,預設會使用指定帳號 (this account) 啟動、而非系統帳號(system account),因此須透過控制台的啟動設定鈕設定該服務的啟動帳號。

    下面的範例為安裝一個名稱為 Alex Service 的服務 :
    instsrv "Alex Service" c:\alex\alexsrv.exe

    下面的指令則將移除此服務 :
    instsrv "Alex Service" remove


    3. 刪除機碼:

    找到下列的機碼位置並刪除: HK_LocalMachine\System\CurrentControlSet\Services\<服務名稱>


    其他參考資源:

    沒有留言: