VBS implementation of system service backup optimization code

Time:2022-5-2

Copy codeThe code is as follows:


Option Explicit
‘0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Const blnVBSShowCaption=True
‘add a description to the generated service backup
Const blnVBSShowDescription=True
‘end of ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
‘file read / write identity constant
Const Forwriteing = 8
‘type of automatic mode read out
Const cstrAutoForRead=”Auto”
‘automatic mode type for setting
Const cstrAutoForWrite=”Automatic”
‘change history status
Const cstrStateNotFound=”Not Found”
Const cstrStateNotSetted=”Not Setted”
Const cstrStateNotChanged=”Not Changed”
Const cstrStateChanged=”Changed”
‘service change type
Class clsServiceChange
‘service name
    Public Name
‘service display name
    Public Caption
‘service description
    Public Description
‘service initial startup mode
    Public StartModeFrom
‘Start mode after service
    Public StartModeTo
‘service start change ID ‘
    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
‘file system
dim objFileSystem
‘backed up VBS files
Dim objVBSFile
‘WSHShell object
dim objWShell
‘windows system management module
Dim objWinManagment
‘system service set ‘
Dim objServices
‘identification of the computer being operated
Dim strComputer
‘backup file path
Dim strVBSFilePath
‘backup file name
Dim strVBSFileName
‘information
Dim strMessage
‘array index
Dim intIndex
‘log staging
Dim astrLog()
‘service change history
Dim aobjServiceChange()
Redim aobjServiceChange(0)
Redim astrLog(0)
‘add multiple change principles
‘○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
‘modify the position yourself (start)
‘○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○
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”
‘●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
‘modify the position yourself (end)
‘●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
‘set computer as local
strComputer = “.”
Set objWShell = CreateObject(“Wscript.Shell”)
‘backup to my documents
strVBSFilePath = objWShell.SpecialFolders(“MyDocuments”) & “\ServiceBack\”
‘Take the current time as the backup file name
strVBSFileName = “Service” & Year(Date) & “-” & Month(Date) & “-” & Day(Date) & “-” & Hour(Time) & “-” & Minute(Time) &”.vbs
Set objFileSystem = CreateObject(“Scripting.FileSystemObject”)
‘verify the existence of backup file path and add folder
If Not objFileSystem.FolderExists(strVBSFilePath) Then
    objFileSystem.CreateFolder(strVBSFilePath)
End If
‘backup file existence verification
If objFileSystem.FileExists(strVBSFilePath & strVBSFileName) Then
‘if the file exists
    Msgbox “The File Has been in existence.”,16,”Warning!”
Else
‘native management module object initialization
    Set objWinManagment = GetObject(“Winmgmts:\\”& strComputer &”\Root\Cimv2”)
‘local service set acquisition
    Set objServices =objWinManagment.ExecQuery(“Select * From Win32_Service”)
‘file object creation
    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
‘execute service settings
        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 output
    Call OutputLog
‘backup file closed
    objVBSFile.Close
End if
‘object release
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 information
”””””””””””””””””
Function WriteLog(strTemp)
‘check whether the current array value is initialized and initialized to null value
    If Isempty(astrLog(Ubound(astrLog))) Then
        astrLog(Ubound(astrLog))=””
    End If
    astrLog(Ubound(astrLog))=astrLog(Ubound(astrLog)) & strTemp
End Function
”””””””””””””””””
‘log information by line
”””””””””””””””””
Function WriteLineLog(strTemp)
‘check whether the current array value is initialized and initialized to null value
    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
”””””””””””””””””
‘output log information
”””””””””””””””””
Function OutputLog()
‘array counter
    Dim intIndex
‘log output error count
    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
”””””””””””””””
‘add change policy
””””””””””””””””’
Function AddRuler(SRN,SRM)
    Dim intIndex
‘traverse all existing services to prevent duplication
    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
‘record service name
    aobjServiceChange(Ubound(aobjServiceChange)).Name=SRN
‘record the startup mode to which the service is to be changed
    aobjServiceChange(Ubound(aobjServiceChange)).StartModeTo=SRM
    Redim Preserve aobjServiceChange(Ubound(aobjServiceChange)+1)
End Function
”””””””””””””””
‘get backup from service status
”””””””””””””””
Function GOT()
‘system services
    Dim objService
‘service name
    Dim strServiceName
‘service display name
    Dim strServiceCaption
‘service startup mode
    Dim strServiceMode
‘service description
    Dim strServiceDescription
‘change count
    Dim intChangeCount
‘valid rule count
    Dim intRuleredCount
‘rule count
    Dim intRuler
‘array index
    Dim intIndex
‘match with rule ‘
    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,””)
‘show description in backup code
        If blnVBSShowCaption Then
            objVBSFile.Write “‘”
‘avoid garbled code and errors
            On Error Resume Next
                objVBSFile.Write “(” & strServiceCaption & “) “
‘show description in backup code
                If blnShowDescription Then
                    objVBSFile.Write ” [” & strServiceDescription & “]”
                End If
                objVBSFile.Writeline “”
                Err.clear
            On Error Goto 0
        End If
        objVBSFile.WriteLine ”    SRV “””& strServiceName &”””,”””& strServiceMode &””””
‘change detection (for log output)
        For intIndex=0 To Ubound(aobjServiceChange)-1
‘verify that the names are the same
            If strServiceName=aobjServiceChange(intIndex).Name Then
‘modify whether to match the identification on
                blnMatch=True
‘number of records matching
                intRuleredCount=intRuleredCount+1
‘record the starting method of the service
                aobjServiceChange(intIndex).StartModeFrom=strServiceMode
‘record service display name
                aobjServiceChange(intIndex).Caption=strServiceCaption
‘record service description
                aobjServiceChange(intIndex).Description=strServiceDescription
‘verify if changes are required
                If strServiceMode<>aobjServiceChange(intIndex).StartModeTo Then
‘change count required
                    intChangeCount=intChangeCount+1
                    WriteLineLog strServiceName & vbTab & “(” & strServiceCaption &”)”  & “[” & strServiceDescription &”]”
                    WriteLineLog strServiceMode & vbTab & “—>” & vbTab & aobjServiceChange(intIndex).StartModeTo
                End If
‘exit the loop after finding the service
                Exit For
            End If
        Next
‘add services not specified in the rule
        If blnMatch=False then
            Set aobjServiceChange(Ubound(aobjServiceChange))=New clsServiceChange
‘record service name
            aobjServiceChange(Ubound(aobjServiceChange)).Name=objService.Name
‘record service alias
            aobjServiceChange(Ubound(aobjServiceChange)).Caption=objService.Caption
‘record service description
            aobjServiceChange(Ubound(aobjServiceChange)).Description=objService.Description
‘record the initial startup mode of the service
            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 “***********************************************************************************”
‘output unspecified
    WriteLineLog vbcrlf
    WriteLineLog “—————————–“
    WriteLineLog “Not Rulered:”
    WriteLineLog “—————————–“
‘traverse change history
    For intIndex=0 To Ubound(aobjServiceChange)-1
‘service change status detection
        If aobjServiceChange(intIndex).State=cstrStateNotSetted Then
‘unset service information output
            WriteLineLog “””” & aobjServiceChange(intIndex).Name  & “””” & vbTab & “,””” & aobjServiceChange(intIndex).StartModeFrom & “””” & vbTab & “(” & aobjServiceChange(intIndex).Caption & “)” & “[” & aobjServiceChange(intIndex).Description & “]”
        End If
    Next
’empty object
    Set objService=Nothing
End Function
””””””””””””””””””””””
‘service setting function
””””””””””””””””””””””
Function SRV()
‘system services
    Dim objService
‘array count
    Dim intIndex
‘service description
    Dim strServiceDescription
‘service startup mode to be set
    Dim strStartModeTemp
‘service startup mode modification success count
    Dim intChangeSuccessCount
‘count of unsuccessful service startup mode modification
    Dim intChangeNotSuccessCount
    intChangeSuccessCount=0
    intChangeNotSuccessCount=0
    WriteLineLog vbcrlf
    WriteLineLog “=============================”
    WriteLineLog “Optimize Record:”
    WriteLineLog “=============================”
‘failed to modify log title
    WriteLineLog “—————————–“
    WriteLineLog “Change False:”
    WriteLineLog “—————————–“
‘traverse service set
    For Each objService In objServices
        For intIndex=0 To Ubound(aobjServiceChange)-1
            If objService.Name=aobjServiceChange(intIndex).Name Then
‘Start mode judgment
                If aobjServiceChange(intIndex).State=cstrStateChanged Then
‘modify startup mode
                    If aobjServiceChange(intIndex).StartModeTo=cstrAutoForRead Then
‘To set the startup type as self startup
                        strStartModeTemp=cstrAutoForWrite
                    Else
‘when setting the service type is not self starting
                        strStartModeTemp=aobjServiceChange(intIndex).StartModeTo
                    End If
‘modify service startup method
                    If objService.ChangeStartMode(strStartModeTemp)=0 Then
‘modify success count
                        intChangeSuccessCount=intChangeSuccessCount+1
                    Else
‘modify unsuccessful count
                        intChangeNotSuccessCount=intChangeNotSuccessCount+1
‘record the services that were not successfully modified
                        WriteLineLog objService.Name & “(” & objService.Caption & “)” & “[” & objService.Description & “]”
‘record the changes that should be made to the service
                        WriteLineLog objService.StartMode & vbTab & “—>” & vbTab & aobjServiceChange(intIndex).StartModeTo
                    End If
                End If
‘exit the loop after finding the service
                Exit For
            End If
        Next
    Next
    SRV=”Successed:” & intChangeSuccessCount & “,NotSuccessed:” & intChangeNotSuccessCount
    WriteLineLog “***********************************************************************************”
    WriteLineLog SRV
    WriteLineLog “***********************************************************************************”
’empty object
    Set objService=Nothing
End Function

Recommended Today

JS generate guid method

JS generate guid method https://blog.csdn.net/Alive_tree/article/details/87942348 Globally unique identification(GUID) is an algorithm generatedBinaryCount Reg128 bitsNumber ofidentifier , GUID is mainly used in networks or systems with multiple nodes and computers. Ideally, any computational geometry computer cluster will not generate two identical guids, and the total number of guids is2^128In theory, it is difficult to make two […]