您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關VBS如何實現系統服務備份優化,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
代碼如下:
Option Explicit
'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○開始
Const blnVBSShowCaption=True
'生成的服務備份中是否添加說明
Const blnVBSShowDescription=True
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●結束
'文件讀寫標識常量
Const Forwriteing = 8
'讀取出來的自動模式類型
Const cstrAutoForRead="Auto"
'用于設定的自動模式類型
Const cstrAutoForWrite="Automatic"
'變更歷史狀態
Const cstrStateNotFound="Not Found"
Const cstrStateNotSetted="Not Setted"
Const cstrStateNotChanged="Not Changed"
Const cstrStateChanged="Changed"
'服務變更類型
Class clsServiceChange
'服務名稱
Public Name
'服務顯示名稱
Public Caption
'服務描述
Public Description
'服務初始啟動模式
Public StartModeFrom
'服務之后啟動模式
Public StartModeTo
'服務啟動變更標識
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
'文件系統
dim objFileSystem
'備份的VBS文件
Dim objVBSFile
'WshShell 對象
dim objWShell
'windows 系統管理模塊
Dim objWinManagment
'系統服務集
Dim objServices
'所操作的電腦標識
Dim strComputer
'備份文件路徑
Dim strVBSFilePath
'備份文件名
Dim strVBSFileName
'信息
Dim strMessage
'數組索引
Dim intIndex
'Log暫存
Dim astrLog()
'服務變更歷史
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"
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
'自己修改位置(結束)
'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
'設定電腦為本機
strComputer = "."
Set objWShell = CreateObject("Wscript.Shell")
'備份至我的文檔
strVBSFilePath = objWShell.SpecialFolders("MyDocuments") & "\ServiceBack\"
'以當前時間作為備份文件名
strVBSFileName = "Service" & Year(Date) & "-" & Month(Date) & "-" & Day(Date) & "-" & Hour(Time) & "-" & Minute(Time) &".vbs"
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
'備份文件路徑存在性驗證和添加文件夾
If Not objFileSystem.FolderExists(strVBSFilePath) Then
objFileSystem.CreateFolder(strVBSFilePath)
End If
'備份文件存在性驗證
If objFileSystem.FileExists(strVBSFilePath & strVBSFileName) Then
'文件存在的情況下
Msgbox "The File Has been in existence.",16,"Warning!"
Else
'本機管理模塊對象初始化
Set objWinManagment = GetObject("Winmgmts:\\"& strComputer &"\Root\Cimv2")
'本機服務集取得
Set objServices =objWinManagment.ExecQuery("Select * From Win32_Service")
'文件對象創建
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
'執行服務設置
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
'備份文件關閉
objVBSFile.Close
End if
'對象釋放
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)
'檢測當前數組值是否被初始化并初始化為空值
If Isempty(astrLog(Ubound(astrLog))) Then
astrLog(Ubound(astrLog))=""
End If
astrLog(Ubound(astrLog))=astrLog(Ubound(astrLog)) & strTemp
End Function
''''''''''''''''''''''''''''''''''
'按行記錄Log信息
''''''''''''''''''''''''''''''''''
Function WriteLineLog(strTemp)
'檢測當前數組值是否被初始化并初始化為空值
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()
'數組計數器
Dim intIndex
'Log輸出出錯計數
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
'遍歷所有已經存在服務以防止重復
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
'記錄服務名稱
aobjServiceChange(Ubound(aobjServiceChange)).Name=SRN
'記錄服務要變更成的啟動模式
aobjServiceChange(Ubound(aobjServiceChange)).StartModeTo=SRM
Redim Preserve aobjServiceChange(Ubound(aobjServiceChange)+1)
End Function
''''''''''''''''''''''''''''''
'服務狀態取得備份
''''''''''''''''''''''''''''''
Function GOT()
'系統服務
Dim objService
'服務名
Dim strServiceName
'服務顯示名
Dim strServiceCaption
'服務啟動模式
Dim strServiceMode
'服務描述
Dim strServiceDescription
'變更計數
Dim intChangeCount
'有效規則計數
Dim intRuleredCount
'規則計數
Dim intRuler
'數組索引
Dim intIndex
'與規則匹配上標識
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 "'"
'避免出現亂碼的情況出錯
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 &""""
'變更檢測(用于Log輸出)
For intIndex=0 To Ubound(aobjServiceChange)-1
'驗證是否名稱相同
If strServiceName=aobjServiceChange(intIndex).Name Then
'修改是否匹配上的標識
blnMatch=True
'記錄匹配上的數目
intRuleredCount=intRuleredCount+1
'記錄服務起始啟動方式
aobjServiceChange(intIndex).StartModeFrom=strServiceMode
'記錄服務顯示名稱
aobjServiceChange(intIndex).Caption=strServiceCaption
'記錄服務描述
aobjServiceChange(intIndex).Description=strServiceDescription
'驗證是否需要變更
If strServiceMode<>aobjServiceChange(intIndex).StartModeTo Then
'需要變更計數
intChangeCount=intChangeCount+1
WriteLineLog strServiceName & vbTab & "(" & strServiceCaption &")" & "[" & strServiceDescription &"]"
WriteLineLog strServiceMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo
End If
'查找到服務之后退出循環
Exit For
End If
Next
'在規則中未規定的服務添加
If blnMatch=False then
Set aobjServiceChange(Ubound(aobjServiceChange))=New clsServiceChange
'記錄服務名稱
aobjServiceChange(Ubound(aobjServiceChange)).Name=objService.Name
'記錄服務別名
aobjServiceChange(Ubound(aobjServiceChange)).Caption=objService.Caption
'記錄服務描述
aobjServiceChange(Ubound(aobjServiceChange)).Description=objService.Description
'記錄服務初始的啟動模式
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 "***********************************************************************************"
'輸出未做規定的
WriteLineLog vbcrlf
WriteLineLog "-----------------------------"
WriteLineLog "Not Rulered:"
WriteLineLog "-----------------------------"
'遍歷變更歷史
For intIndex=0 To Ubound(aobjServiceChange)-1
'服務變更狀態檢測
If aobjServiceChange(intIndex).State=cstrStateNotSetted Then
'未設定的服務信息輸出
WriteLineLog """" & aobjServiceChange(intIndex).Name & """" & vbTab & ",""" & aobjServiceChange(intIndex).StartModeFrom & """" & vbTab & "(" & aobjServiceChange(intIndex).Caption & ")" & "[" & aobjServiceChange(intIndex).Description & "]"
End If
Next
'清空對象
Set objService=Nothing
End Function
''''''''''''''''''''''''''''''''''''''''''''
'服務設定函數
''''''''''''''''''''''''''''''''''''''''''''
Function SRV()
'系統服務
Dim objService
'數組計數
Dim intIndex
'服務描述
Dim strServiceDescription
'要設定的服務啟動方式暫存
Dim strStartModeTemp
'服務啟動方式修改成功計數
Dim intChangeSuccessCount
'服務啟動方式修改不成功計數
Dim intChangeNotSuccessCount
intChangeSuccessCount=0
intChangeNotSuccessCount=0
WriteLineLog vbcrlf
WriteLineLog "============================="
WriteLineLog "Optimize Record:"
WriteLineLog "============================="
'修改失敗的Log標題
WriteLineLog "-----------------------------"
WriteLineLog "Change False:"
WriteLineLog "-----------------------------"
'遍歷服務集
For Each objService In objServices
For intIndex=0 To Ubound(aobjServiceChange)-1
If objService.Name=aobjServiceChange(intIndex).Name Then
'啟動方式判斷
If aobjServiceChange(intIndex).State=cstrStateChanged Then
'修改啟動方式
If aobjServiceChange(intIndex).StartModeTo=cstrAutoForRead Then
'要設定啟動類型為自啟動時
strStartModeTemp=cstrAutoForWrite
Else
'設定服務類型不是自啟動時
strStartModeTemp=aobjServiceChange(intIndex).StartModeTo
End If
'修改服務啟動方式
If objService.ChangeStartMode(strStartModeTemp)=0 Then
'修改成功計數
intChangeSuccessCount=intChangeSuccessCount+1
Else
'修改不成功計數
intChangeNotSuccessCount=intChangeNotSuccessCount+1
'記錄未修改成功的服務
WriteLineLog objService.Name & "(" & objService.Caption & ")" & "[" & objService.Description & "]"
'記錄該服務所應該做的變更
WriteLineLog objService.StartMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo
End If
End If
'查找到服務之后退出循環
Exit For
End If
Next
Next
SRV="Successed:" & intChangeSuccessCount & ",NotSuccessed:" & intChangeNotSuccessCount
WriteLineLog "***********************************************************************************"
WriteLineLog SRV
WriteLineLog "***********************************************************************************"
'清空對象
Set objService=Nothing
End Function
關于“VBS如何實現系統服務備份優化”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。