將下面代碼保存為VBS執(zhí)行即可,服務(wù)優(yōu)化規(guī)則可以自由修改(上面是我現(xiàn)在使用的優(yōu)化規(guī)則),并且每次優(yōu)化都會(huì)在我的文檔里面生成一個(gè)對(duì)應(yīng)日期的服務(wù)備份文件.可以直接還原
代碼如下:
Option Explicit
'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○開始
Const blnVBSShowCaption=True
'生成的服務(wù)備份中是否添加說明
Const blnVBSShowDescription=True
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●結(jié)束
'文件讀寫標(biāo)識(shí)常量
Const Forwriteing = 8
'讀取出來的自動(dòng)模式類型
Const cstrAutoForRead="Auto"
'用于設(shè)定的自動(dòng)模式類型
Const cstrAutoForWrite="Automatic"
'變更歷史狀態(tài)
Const cstrStateNotFound="Not Found"
Const cstrStateNotSetted="Not Setted"
Const cstrStateNotChanged="Not Changed"
Const cstrStateChanged="Changed"
'服務(wù)變更類型
Class clsServiceChange
'服務(wù)名稱
Public Name
'服務(wù)顯示名稱
Public Caption
'服務(wù)描述
Public Description
'服務(wù)初始啟動(dòng)模式
Public StartModeFrom
'服務(wù)之后啟動(dòng)模式
Public StartModeTo
'服務(wù)啟動(dòng)變更標(biāo)識(shí)
Private Sub Class_Initialize()
Name=""
Caption=""
Description=""
StartModeFrom=""
StartModeTo=""
End Sub
Public Property get State
If StartModeFrom="" Then
State="Not Found"
Exit Property
End If
If StartModeTo="" Then
State="Not Setted"
Exit Property
End If
If StartModeFrom=StartModeTo Then
State="Not Changed"
Else
State="Changed"
End If
End Property
End Class
'文件系統(tǒng)
dim objFileSystem
'備份的VBS文件
Dim objVBSFile
'WshShell 對(duì)象
dim objWShell
'windows 系統(tǒng)管理模塊
Dim objWinManagment
'系統(tǒng)服務(wù)集
Dim objServices
'所操作的電腦標(biāo)識(shí)
Dim strComputer
'備份文件路徑
Dim strVBSFilePath
'備份文件名
Dim strVBSFileName
'信息
Dim strMessage
'數(shù)組索引
Dim intIndex
'Log暫存
Dim astrLog()
'服務(wù)變更歷史
Dim aobjServiceChange()
Redim aobjServiceChange(0)
Redim astrLog(0)
'添加多條變更原則
'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
'自己修改位置(開始)
'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
AddRuler "Alerter" ,"Disabled"
AddRuler "ALG" ,"Manual"
AddRuler "AppMgmt" ,"Manual"
AddRuler "aspnet_state" ,"Disabled"
AddRuler "Ati HotKey Poller" ,"Disabled"
AddRuler "AudioAddRuler" ,"Auto"
AddRuler "BITS" ,"Manual"
AddRuler "Browser" ,"Disabled"
AddRuler "CiSvc" ,"Disabled"
AddRuler "ClipSrv" ,"Disabled"
AddRuler "ClipAddRuler" ,"Disabled"
AddRuler "COMSysApp" ,"Disabled"
AddRuler "CryptSvc" ,"Auto"
AddRuler "DcomLaunch" ,"Auto"
AddRuler "DF5Serv" ,"Auto"
AddRuler "Dhcp" ,"Auto"
AddRuler "dmadmin" ,"Manual"
AddRuler "dmserver" ,"Manual"
AddRuler "Dnscache" ,"Disabled"
AddRuler "ERSvc" ,"Disabled"
AddRuler "Eventlog" ,"Auto"
AddRuler "EventSystem" ,"Auto"
AddRuler "FastUserSwitchingCompatibility" ,"Disabled"
AddRuler "helpsvc" ,"Manual"
AddRuler "HidServ" ,"Disabled"
AddRuler "HTTPFilter" ,"Manual"
AddRuler "ImapiService" ,"Disabled"
AddRuler "lanmanserver" ,"Manual"
AddRuler "lanmanworkstation" ,"Auto"
AddRuler "LmHosts" ,"Disabled"
AddRuler "MDM" ,"Disabled"
AddRuler "Messenger" ,"Disabled"
AddRuler "mnmAddRulerc" ,"Manual"
AddRuler "mnmsrvc" , "Disabled"
AddRuler "MSDTC" ,"Disabled"
AddRuler "MSIServer" ,"Manual"
AddRuler "NetDDE" ,"Disabled"
AddRuler "NetDDEdsdm" ,"Disabled"
AddRuler "Netlogon" ,"Manual"
AddRuler "Netman" ,"Auto"
AddRuler "Nla" ,"Disabled"
AddRuler "NtLmSsp" ,"Manual"
AddRuler "NtmsSvc" ,"Disabled"
AddRuler "NVSvc" ,"Disabled"
AddRuler "O&O Defrag" ,"Manual"
AddRuler "ose" ,"Manual"
AddRuler "PlugPlay" ,"Auto"
AddRuler "PolicyAgent" ,"Disabled"
AddRuler "ProtectedStorage" ,"Auto"
AddRuler "RasAuto" ,"Disabled"
AddRuler "RasMan" ,"Disabled"
AddRuler "RDSessMgr" ,"Disabled"
AddRuler "RemoteAccess" ,"Disabled"
AddRuler "RemoteRegistry" ,"Disabled"
AddRuler "RpcLocator" ,"Manual"
AddRuler "RpcSs" ,"Auto"
AddRuler "RSVP" ,"Disabled"
AddRuler "SamSs" ,"Auto"
AddRuler "SCardSvr" ,"Disabled"
AddRuler "Schedule" ,"Disabled"
AddRuler "seclogon" ,"Auto"
AddRuler "SENS" ,"Disabled"
AddRuler "SharedAccess" ,"Disabled"
AddRuler "ShellHWDetection" ,"Manual"
AddRuler "sicentnetsync" ,"Auto"
AddRuler "Spooler" ,"Manual"
AddRuler "srservice" ,"Disabled"
AddRuler "SSDPAddRuler" ,"Disabled"
AddRuler "SSDPSRV" ,"Disabled"
AddRuler "stisvc" ,"Manual"
AddRuler "SwPrv" ,"Disabled"
AddRuler "SysmonLog" ,"Manual"
AddRuler "TapiAddRuler" ,"Manual"
AddRuler "TermService" ,"Disabled"
AddRuler "Themes" ,"Auto"
AddRuler "TlntSvr" ,"Disabled"
AddRuler "TrkWks" ,"Disabled"
AddRuler "TapiSrv" ,"Disabled"
AddRuler "UMWdf" ,"Auto"
AddRuler "upnphost" ,"Disabled"
AddRuler "UPS" ,"Disabled"
AddRuler "usnjsvc" ,"Manual"
AddRuler "VSS" ,"Disabled"
AddRuler "VMAuthdService" ,"Manual"
AddRuler "VMnetDHCP" ,"Manual"
AddRuler "VMware NAT Service" ,"Manual"
AddRuler "W32Time" ,"Disabled"
AddRuler "WebClient" ,"Disabled"
AddRuler "winmgmt" ,"Auto"
AddRuler "WinVNC4" ,"Manual"
AddRuler "WmdmPmSN" ,"Disabled"
AddRuler "Wmi" ,"Manual"
AddRuler "WmiApAddRuler" ,"Manual"
AddRuler "wscsvc" ,"Manual"
AddRuler "wuauserv" ,"Disabled"
AddRuler "WZCSVC" ,"Auto"
AddRuler "xmlprov" ,"Manual"
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
'自己修改位置(結(jié)束)
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
'設(shè)定電腦為本機(jī)
strComputer = "."
Set objWShell = CreateObject("Wscript.Shell")
'備份至我的文檔
strVBSFilePath = objWShell.SpecialFolders("MyDocuments") & "\ServiceBack\"
'以當(dāng)前時(shí)間作為備份文件名
strVBSFileName = "Service" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "-" & Hour(Time) & "-" & Minute(Time) &".vbs"
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
'備份文件路徑存在性驗(yàn)證和添加文件夾
If Not objFileSystem.FolderExists(strVBSFilePath) Then
objFileSystem.CreateFolder(strVBSFilePath)
End If
'備份文件存在性驗(yàn)證
If objFileSystem.FileExists(strVBSFilePath & strVBSFileName) Then
'文件存在的情況下
Msgbox "The File Has been in existence.",16,"Warning!"
Else
'本機(jī)管理模塊對(duì)象初始化
Set objWinManagment = GetObject("Winmgmts:\\"& strComputer &"\Root\Cimv2")
'本機(jī)服務(wù)集取得
Set objServices =objWinManagment.ExecQuery("Select * From Win32_Service")
'文件對(duì)象創(chuàng)建
Set objVBSFile = objFileSystem.CreateTextFile(strVBSFilePath & strVBSFileName ,ForWriteing)
objVBSFile.WriteLine vbcrlf
objVBSFile.WriteLine "'Runable Backup:"
objVBSFile.WriteLine "'============================="
objVBSFile.WriteLine "Const cstrAutoForRead=""Auto"""
objVBSFile.WriteLine "Const cstrAutoForWrite=""Automatic"""
objVBSFile.WriteLine "intChangeCount=0"
objVBSFile.WriteLine "intChangeSuccessCount=0"
objVBSFile.WriteLine "strComputer = ""."""
objVBSFile.WriteLine "Const Forwriteing = 8"
objVBSFile.WriteLine "If Msgbox(""Are You Sure You Want To Restore Your Service ?"",VBYesNo+vbInformation,""Restore Service"")=vbYes Then"
objVBSFile.WriteLine " Set objWinManagment = GetObject(""Winmgmts:\\""& strComputer &""\Root\Cimv2"")"
objVBSFile.WriteLine " Set objServices =objWinManagment.ExecQuery(""Select * From Win32_Service"")"
strMessage = GOT()
objVBSFile.WriteLine " Set objWinManagment = Nothing"
objVBSFile.WriteLine " Set objServices =Nothing"
objVBSFile.WriteLine "End If"
objVBSFile.WriteLine "Function SRV(SRN,SRM)"
objVBSFile.WriteLine " For Each S In objServices"
objVBSFile.WriteLine " If s.Name = Srn And S.StartMode<>SRM Then"
objVBSFile.WriteLine " intChangeCount=intChangeCount+1 "
objVBSFile.WriteLine " If SRM=cstrAutoForRead Then "
objVBSFile.WriteLine " strStartModeTemp=cstrAutoForWrite "
objVBSFile.WriteLine " Else "
objVBSFile.WriteLine " strStartModeTemp=SRM "
objVBSFile.WriteLine " End If "
objVBSFile.WriteLine " If S.ChangeStartMode(strStartModeTemp)=0 Then "
objVBSFile.WriteLine " intChangeSuccessCount=intChangeSuccessCount+1 "
objVBSFile.WriteLine " End If "
objVBSFile.WriteLine " End if"
objVBSFile.WriteLine " Next"
objVBSFile.WriteLine "End Function"
If Msgbox ("Backup services Completed" & Chr(13) & strVBSFilePath & strVBSFileName & Chr(13) & "'(" & strMessage & ")" &"Begin optimization?",VBYesNo+vbInformation,"Complete" ) = vbYes Then
'執(zhí)行服務(wù)設(shè)置
strMessage=SRV
Msgbox "Optimization completed!restart Your computer?" & Chr(13) & "'(" & strMessage & ")",vbOKOnly+vbInformation,"OK"
' If Msgbox("Optimization completed!restart Your computer?" & Chr(13) & "'(" & strMessage & ")",VBYesNo+vbInformation,"OK") = vbYes Then
' objWShell.Run "Shutdown.exe -r -t 5"
' End if
End if
'Log輸出
Call OutputLog
'備份文件關(guān)閉
objVBSFile.Close
End if
'對(duì)象釋放
For intIndex=0 To Ubound(aobjServiceChange)
Set aobjServiceChange(intIndex)= Nothing
Next
Erase aobjServiceChange
Erase astrLog
Set objFileSystem=Nothing
Set objVBSFile=Nothing
Set objWShell=Nothing
Set objWinManagment=Nothing
Set objServices=Nothing
Wscript.quit
''''''''''''''''''''''''''''''''''
'記錄Log信息
''''''''''''''''''''''''''''''''''
Function WriteLog(strTemp)
'檢測(cè)當(dāng)前數(shù)組值是否被初始化并初始化為空值
If Isempty(astrLog(Ubound(astrLog))) Then
astrLog(Ubound(astrLog))=""
End If
astrLog(Ubound(astrLog))=astrLog(Ubound(astrLog)) & strTemp
End Function
''''''''''''''''''''''''''''''''''
'按行記錄Log信息
''''''''''''''''''''''''''''''''''
Function WriteLineLog(strTemp)
'檢測(cè)當(dāng)前數(shù)組值是否被初始化并初始化為空值
If Isempty(astrLog(Ubound(astrLog))) Then
astrLog(Ubound(astrLog))=""
End If
astrLog(Ubound(astrLog))=astrLog(Ubound(astrLog)) & strTemp
Redim Preserve astrLog(Ubound(astrLog)+1)
astrLog(Ubound(astrLog))=""
End Function
''''''''''''''''''''''''''''''''''
'輸出Log信息
''''''''''''''''''''''''''''''''''
Function OutputLog()
'數(shù)組計(jì)數(shù)器
Dim intIndex
'Log輸出出錯(cuò)計(jì)數(shù)
Dim intLogOutputError
intLogOutputError=0
For intIndex=0 To Ubound(astrLog)
On Error Resume Next
objVBSFile.WriteLine "'" & astrLog(intIndex)
If Err.Number<>0 Then
intLogOutputError=intLogOutputError+1
objVBSFile.WriteLine "'???????????????Illegal Code????????????????"
End If
On Error Goto 0
Next
If intLogOutputError<>0 Then
objVBSFile.WriteLine "'???????????????Log Output Error(" & intLogOutputError & ")????????????????"
End If
End Function
''''''''''''''''''''''''''''''
'添加變更原則
'''''''''''''''''''''''''''''''''
Function AddRuler(SRN,SRM)
Dim intIndex
'遍歷所有已經(jīng)存在服務(wù)以防止重復(fù)
For intIndex=0 To Ubound(aobjServiceChange)-1
If aobjServiceChange(intIndex).Name=SRN Then
WriteLineLog "???????????????" & SRN & "'s Ruler duplicated ????????????????"
Exit Function
End If
Next
Set aobjServiceChange(Ubound(aobjServiceChange))=New clsServiceChange
'記錄服務(wù)名稱
aobjServiceChange(Ubound(aobjServiceChange)).Name=SRN
'記錄服務(wù)要變更成的啟動(dòng)模式
aobjServiceChange(Ubound(aobjServiceChange)).StartModeTo=SRM
Redim Preserve aobjServiceChange(Ubound(aobjServiceChange)+1)
End Function
''''''''''''''''''''''''''''''
'服務(wù)狀態(tài)取得備份
''''''''''''''''''''''''''''''
Function GOT()
'系統(tǒng)服務(wù)
Dim objService
'服務(wù)名
Dim strServiceName
'服務(wù)顯示名
Dim strServiceCaption
'服務(wù)啟動(dòng)模式
Dim strServiceMode
'服務(wù)描述
Dim strServiceDescription
'變更計(jì)數(shù)
Dim intChangeCount
'有效規(guī)則計(jì)數(shù)
Dim intRuleredCount
'規(guī)則計(jì)數(shù)
Dim intRuler
'數(shù)組索引
Dim intIndex
'與規(guī)則匹配上標(biāo)識(shí)
Dim blnMatch
intChangeCount=0
intRuleredCount=0
intRuler=Ubound(aobjServiceChange)-1
WriteLineLog vbcrlf
WriteLineLog "Optimize Needed:"
WriteLineLog "============================="
For Each objService in objServices
blnMatch=False
strServiceName = objService.Name
strServiceCaption=objService.Caption
strServiceMode = objService.StartMode
strServiceDescription=objService.Description
If IsNull(strServiceDescription) Then
strServiceDescription=""
End If
strServiceDescription=Replace(strServiceDescription,vbcrlf,"")
'是否在備份代碼中顯示說明
If blnVBSShowCaption Then
objVBSFile.Write "'"
'避免出現(xiàn)亂碼的情況出錯(cuò)
On Error Resume Next
objVBSFile.Write "(" & strServiceCaption & ") "
'是否在備份代碼中顯示說明
If blnShowDescription Then
objVBSFile.Write " [" & strServiceDescription & "]"
End If
objVBSFile.Writeline ""
Err.clear
On Error Goto 0
End If
objVBSFile.WriteLine " SRV """& strServiceName &""","""& strServiceMode &""""
'變更檢測(cè)(用于Log輸出)
For intIndex=0 To Ubound(aobjServiceChange)-1
'驗(yàn)證是否名稱相同
If strServiceName=aobjServiceChange(intIndex).Name Then
'修改是否匹配上的標(biāo)識(shí)
blnMatch=True
'記錄匹配上的數(shù)目
intRuleredCount=intRuleredCount+1
'記錄服務(wù)起始啟動(dòng)方式
aobjServiceChange(intIndex).StartModeFrom=strServiceMode
'記錄服務(wù)顯示名稱
aobjServiceChange(intIndex).Caption=strServiceCaption
'記錄服務(wù)描述
aobjServiceChange(intIndex).Description=strServiceDescription
'驗(yàn)證是否需要變更
If strServiceMode<>aobjServiceChange(intIndex).StartModeTo Then
'需要變更計(jì)數(shù)
intChangeCount=intChangeCount+1
WriteLineLog strServiceName & vbTab & "(" & strServiceCaption &")" & "[" & strServiceDescription &"]"
WriteLineLog strServiceMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo
End If
'查找到服務(wù)之后退出循環(huán)
Exit For
End If
Next
'在規(guī)則中未規(guī)定的服務(wù)添加
If blnMatch=False then
Set aobjServiceChange(Ubound(aobjServiceChange))=New clsServiceChange
'記錄服務(wù)名稱
aobjServiceChange(Ubound(aobjServiceChange)).Name=objService.Name
'記錄服務(wù)別名
aobjServiceChange(Ubound(aobjServiceChange)).Caption=objService.Caption
'記錄服務(wù)描述
aobjServiceChange(Ubound(aobjServiceChange)).Description=objService.Description
'記錄服務(wù)初始的啟動(dòng)模式
aobjServiceChange(Ubound(aobjServiceChange)).StartModeFrom=objService.StartMode
Redim Preserve aobjServiceChange(Ubound(aobjServiceChange)+1)
End If
Next
objVBSFile.WriteLine " Msgbox ""Service Reset Completed"" & ""("" & intChangeSuccessCount &""/"" & intChangeCount & "")"" "
GOT="Service:" & objServices.Count & ",Ruler:" & intRuler & ",Rulered:" & intRuleredCount & ",NotRulered:" & objServices.Count-intRuleredCount & ",ChangeNeed:" & intChangeCount & ""
WriteLineLog "***********************************************************************************"
WriteLineLog GOT
WriteLineLog "***********************************************************************************"
'輸出未做規(guī)定的
WriteLineLog vbcrlf
WriteLineLog "-----------------------------"
WriteLineLog "Not Rulered:"
WriteLineLog "-----------------------------"
'遍歷變更歷史
For intIndex=0 To Ubound(aobjServiceChange)-1
'服務(wù)變更狀態(tài)檢測(cè)
If aobjServiceChange(intIndex).State=cstrStateNotSetted Then
'未設(shè)定的服務(wù)信息輸出
WriteLineLog """" & aobjServiceChange(intIndex).Name & """" & vbTab & ",""" & aobjServiceChange(intIndex).StartModeFrom & """" & vbTab & "(" & aobjServiceChange(intIndex).Caption & ")" & "[" & aobjServiceChange(intIndex).Description & "]"
End If
Next
'清空對(duì)象
Set objService=Nothing
End Function
''''''''''''''''''''''''''''''''''''''''''''
'服務(wù)設(shè)定函數(shù)
''''''''''''''''''''''''''''''''''''''''''''
Function SRV()
'系統(tǒng)服務(wù)
Dim objService
'數(shù)組計(jì)數(shù)
Dim intIndex
'服務(wù)描述
Dim strServiceDescription
'要設(shè)定的服務(wù)啟動(dòng)方式暫存
Dim strStartModeTemp
'服務(wù)啟動(dòng)方式修改成功計(jì)數(shù)
Dim intChangeSuccessCount
'服務(wù)啟動(dòng)方式修改不成功計(jì)數(shù)
Dim intChangeNotSuccessCount
intChangeSuccessCount=0
intChangeNotSuccessCount=0
WriteLineLog vbcrlf
WriteLineLog "============================="
WriteLineLog "Optimize Record:"
WriteLineLog "============================="
'修改失敗的Log標(biāo)題
WriteLineLog "-----------------------------"
WriteLineLog "Change False:"
WriteLineLog "-----------------------------"
'遍歷服務(wù)集
For Each objService In objServices
For intIndex=0 To Ubound(aobjServiceChange)-1
If objService.Name=aobjServiceChange(intIndex).Name Then
'啟動(dòng)方式判斷
If aobjServiceChange(intIndex).State=cstrStateChanged Then
'修改啟動(dòng)方式
If aobjServiceChange(intIndex).StartModeTo=cstrAutoForRead Then
'要設(shè)定啟動(dòng)類型為自啟動(dòng)時(shí)
strStartModeTemp=cstrAutoForWrite
Else
'設(shè)定服務(wù)類型不是自啟動(dòng)時(shí)
strStartModeTemp=aobjServiceChange(intIndex).StartModeTo
End If
'修改服務(wù)啟動(dòng)方式
If objService.ChangeStartMode(strStartModeTemp)=0 Then
'修改成功計(jì)數(shù)
intChangeSuccessCount=intChangeSuccessCount+1
Else
'修改不成功計(jì)數(shù)
intChangeNotSuccessCount=intChangeNotSuccessCount+1
'記錄未修改成功的服務(wù)
WriteLineLog objService.Name & "(" & objService.Caption & ")" & "[" & objService.Description & "]"
'記錄該服務(wù)所應(yīng)該做的變更
WriteLineLog objService.StartMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo
End If
End If
'查找到服務(wù)之后退出循環(huán)
Exit For
End If
Next
Next
SRV="Successed:" & intChangeSuccessCount & ",NotSuccessed:" & intChangeNotSuccessCount
WriteLineLog "***********************************************************************************"
WriteLineLog SRV
WriteLineLog "***********************************************************************************"
'清空對(duì)象
Set objService=Nothing
End Function