VBS實現(xiàn)的系統(tǒng)服務備份優(yōu)化代碼
來源:易賢網(wǎng) 閱讀:948 次 日期:2016-06-30 11:06:06
溫馨提示:易賢網(wǎng)小編為您整理了“VBS實現(xiàn)的系統(tǒng)服務備份優(yōu)化代碼”,方便廣大網(wǎng)友查閱!

將下面代碼保存為VBS執(zhí)行即可,服務優(yōu)化規(guī)則可以自由修改(上面是我現(xiàn)在使用的優(yōu)化規(guī)則),并且每次優(yōu)化都會在我的文檔里面生成一個對應日期的服務備份文件.可以直接還原

代碼如下:

Option Explicit

'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○開始

Const blnVBSShowCaption=True

'生成的服務備份中是否添加說明

Const blnVBSShowDescription=True

'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●結(jié)束

'文件讀寫標識常量

Const Forwriteing = 8

'讀取出來的自動模式類型

Const cstrAutoForRead="Auto"

'用于設定的自動模式類型

Const cstrAutoForWrite="Automatic"

'變更歷史狀態(tài)

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

'文件系統(tǒng)

dim objFileSystem

'備份的VBS文件

Dim objVBSFile

'WshShell 對象

dim objWShell

'windows 系統(tǒng)管理模塊

Dim objWinManagment

'系統(tǒng)服務集

Dim objServices

'所操作的電腦標識

Dim strComputer

'備份文件路徑

Dim strVBSFilePath

'備份文件名

Dim strVBSFileName

'信息

Dim strMessage

'數(shù)組索引

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"

'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

'自己修改位置(結(jié)束)

'●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

'設定電腦為本機

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")

    '文件對象創(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í)行服務設置

        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)

    '檢測當前數(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)

    '檢測當前數(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ù)組計數(shù)器

    Dim intIndex

    'Log輸出出錯計數(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)存在服務以防止重復

    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

''''''''''''''''''''''''''''''

'服務狀態(tài)取得備份

''''''''''''''''''''''''''''''

Function GOT()

    '系統(tǒng)服務

    Dim objService

    '服務名

    Dim strServiceName

    '服務顯示名

    Dim strServiceCaption

    '服務啟動模式

    Dim strServiceMode

    '服務描述

    Dim strServiceDescription

    '變更計數(shù)

    Dim intChangeCount

    '有效規(guī)則計數(shù)

    Dim intRuleredCount

    '規(guī)則計數(shù)

    Dim intRuler

    '數(shù)組索引

    Dim intIndex

    '與規(guī)則匹配上標識

    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)亂碼的情況出錯

            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

                '記錄匹配上的數(shù)目

                intRuleredCount=intRuleredCount+1

                '記錄服務起始啟動方式

                aobjServiceChange(intIndex).StartModeFrom=strServiceMode

                '記錄服務顯示名稱

                aobjServiceChange(intIndex).Caption=strServiceCaption

                '記錄服務描述

                aobjServiceChange(intIndex).Description=strServiceDescription

                '驗證是否需要變更

                If strServiceMode<>aobjServiceChange(intIndex).StartModeTo Then

                    '需要變更計數(shù)

                    intChangeCount=intChangeCount+1

                    WriteLineLog strServiceName & vbTab & "(" & strServiceCaption &")"  & "[" & strServiceDescription &"]"

                    WriteLineLog strServiceMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo

                End If

                '查找到服務之后退出循環(huán)

                Exit For

            End If

        Next

        '在規(guī)則中未規(guī)定的服務添加

        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 "***********************************************************************************"

    '輸出未做規(guī)定的

    WriteLineLog vbcrlf 

    WriteLineLog "-----------------------------"

    WriteLineLog "Not Rulered:"

    WriteLineLog "-----------------------------"

    '遍歷變更歷史

    For intIndex=0 To Ubound(aobjServiceChange)-1

        '服務變更狀態(tài)檢測

        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

''''''''''''''''''''''''''''''''''''''''''''

'服務設定函數(shù)

''''''''''''''''''''''''''''''''''''''''''''

Function SRV()

    '系統(tǒng)服務

    Dim objService

    '數(shù)組計數(shù)

    Dim intIndex

    '服務描述

    Dim strServiceDescription

    '要設定的服務啟動方式暫存

    Dim strStartModeTemp

    '服務啟動方式修改成功計數(shù)

    Dim intChangeSuccessCount

    '服務啟動方式修改不成功計數(shù)

    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

                        '修改成功計數(shù)

                        intChangeSuccessCount=intChangeSuccessCount+1

                    Else

                        '修改不成功計數(shù)

                        intChangeNotSuccessCount=intChangeNotSuccessCount+1

                        '記錄未修改成功的服務

                        WriteLineLog objService.Name & "(" & objService.Caption & ")" & "[" & objService.Description & "]"

                        '記錄該服務所應該做的變更

                        WriteLineLog objService.StartMode & vbTab & "--->" & vbTab & aobjServiceChange(intIndex).StartModeTo

                    End If

                End If

                '查找到服務之后退出循環(huán)

                Exit For

            End If

        Next

    Next

    SRV="Successed:" & intChangeSuccessCount & ",NotSuccessed:" & intChangeNotSuccessCount

    WriteLineLog "***********************************************************************************"

    WriteLineLog SRV

    WriteLineLog "***********************************************************************************"

    '清空對象

    Set objService=Nothing

End Function

更多信息請查看腳本欄目
易賢網(wǎng)手機網(wǎng)站地址:VBS實現(xiàn)的系統(tǒng)服務備份優(yōu)化代碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權所有:易賢網(wǎng)