In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to optimize system service backup by VBS. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The code is as follows:
Option Explicit
'The ○○ starts
Const blnVBSShowCaption=True
'whether to add a description to the generated service backup
Const blnVBSShowDescription=True
'end of ●●
'File read and write identity constant
Const Forwriteing = 8
'the type of automatic mode read out
Const cstrAutoForRead= "Auto"
'the type of automatic mode used to set
Const cstrAutoForWrite= "Automatic"
'change the historical state
Const cstrStateNotFound= "NotFound"
Const cstrStateNotSetted= "NotSetted"
Const cstrStateNotChanged= "NotChanged"
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
'Startup mode after service
Public StartModeTo
'Service startup change identity
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
'The logo of the computer you are operating
Dim strComputer
'backup file path
Dim strVBSFilePath
'backup file name
Dim strVBSFileName
'Info
Dim strMessage
'Array index
Dim intIndex
'Log temporary storage
Dim astrLog ()
'Service change history
Dim aobjServiceChange ()
Redim aobjServiceChange (0)
Redim astrLog (0)
'add multiple change principles
'○○
'modify the location 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 "Olympio 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 location yourself (end)
'●●
'set the computer as the local computer
StrComputer = "."
Set objWShell = CreateObject ("Wscript.Shell")
'back up to my document
StrVBSFilePath = objWShell.SpecialFolders ("MyDocuments") & "\ ServiceBack\"
'use 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 paths and add folders
If Not objFileSystem.FolderExists (strVBSFilePath) Then
ObjFileSystem.CreateFolder (strVBSFilePath)
End If
'verify the existence of backup files
If objFileSystem.FileExists (strVBSFilePath & strVBSFileName) Then
'in the presence of a file
Msgbox "The File Has been in existence.", 16, "Warning!"
Else
'Native management module object initialization
Set objWinManagment = GetObject ("Winmgmts:\\" & strComputer & "\ Root\ Cimv2")
'The native service set is obtained
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.StartModeSRM 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 files are 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
''
'record Log information
''
Function WriteLog (strTemp)
'detect whether the current array value is initialized and initialized to a null value
If Isempty (astrLog (Ubound (astrLog) Then
AstrLog (Ubound (astrLog)) = ""
End If
AstrLog (Ubound (astrLog)) = astrLog (Ubound (astrLog)) & strTemp
End Function
''
'record Log information by line
''
Function WriteLineLog (strTemp)
'detect whether the current array value is initialized and initialized to a 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.Number0 Then
IntLogOutputError=intLogOutputError+1
ObjVBSFile.WriteLine "'? Illegal Code?"
End If
On Error Goto 0
Next
If intLogOutputError0 Then
ObjVBSFile.WriteLine "'? LogOutputError (" & intLogOutputError & ")?"
End If
End Function
''
'add change principles
''
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 the service name
AobjServiceChange (Ubound (aobjServiceChange)). Name=SRN
'record the startup mode to be changed by the service
AobjServiceChange (Ubound (aobjServiceChange)). StartModeTo=SRM
Redim Preserve aobjServiceChange (Ubound (aobjServiceChange) + 1)
End Function
''
'get a backup of 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
'Mark on matching with the 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, "")
'whether to display a description in the backup code
If blnVBSShowCaption Then
ObjVBSFile.Write "'"
'avoid garbled errors
On Error Resume Next
ObjVBSFile.Write "(& strServiceCaption &") "
'whether to display a description in the 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 name is the same
If strServiceName=aobjServiceChange (intIndex). Name Then
'modify whether to match the identity on the
BlnMatch=True
'the number of records matched
IntRuleredCount=intRuleredCount+1
'record the start mode of the service
AobjServiceChange (intIndex). StartModeFrom=strServiceMode
'record service display name
AobjServiceChange (intIndex). Caption=strServiceCaption
'record the service description
AobjServiceChange (intIndex). Description=strServiceDescription
'verify if changes are needed
If strServiceModeaobjServiceChange (intIndex). StartModeTo Then
'need to change the count
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 that are not specified in the rule
If blnMatch=False then
Set aobjServiceChange (Ubound (aobjServiceChange)) = New clsServiceChange
'record the service name
AobjServiceChange (Ubound (aobjServiceChange)). Name=objService.Name
'record service alias
AobjServiceChange (Ubound (aobjServiceChange)). Caption=objService.Caption
'record the 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 "* *"
'Exporting unspecified
WriteLineLog vbcrlf
WriteLineLog "-"
WriteLineLog "Not Rulered:"
WriteLineLog "-"
'traverse the change history
For intIndex=0 To Ubound (aobjServiceChange)-1
'Service change status detection
If aobjServiceChange (intIndex). State=cstrStateNotSetted Then
'output of unset service information
WriteLineLog "" & aobjServiceChange (intIndex). Name & "& vbTab &", "& aobjServiceChange (intIndex). StartModeFrom &" & "& vbTab &" ("& aobjServiceChange (intIndex). Caption &") & "[" & aobjServiceChange (intIndex). Description & "]"
End If
Next
'empty the object
Set objService=Nothing
End Function
''
'Service setting function
''
Function SRV ()
'system services
Dim objService
'Array count
Dim intIndex
'Service description
Dim strServiceDescription
'The service startup method to be set is temporarily stored.
Dim strStartModeTemp
'Service startup mode modification success count
Dim intChangeSuccessCount
'The count of unsuccessful modification of service startup mode
Dim intChangeNotSuccessCount
IntChangeSuccessCount=0
IntChangeNotSuccessCount=0
WriteLineLog vbcrlf
WriteLineLog "= ="
WriteLineLog "Optimize Record:"
WriteLineLog "= ="
'modify the failed Log title
WriteLineLog "-"
WriteLineLog "Change False:"
WriteLineLog "-"
'traverse the service set
For Each objService In objServices
For intIndex=0 To Ubound (aobjServiceChange)-1
If objService.Name=aobjServiceChange (intIndex). Name Then
'Startup mode judgment
If aobjServiceChange (intIndex). State=cstrStateChanged Then
'modify the startup mode
If aobjServiceChange (intIndex). StartModeTo=cstrAutoForRead Then
'when you want to set the startup type to self-startup
StrStartModeTemp=cstrAutoForWrite
Else
'when setting the service type is not self-startup
StrStartModeTemp=aobjServiceChange (intIndex). StartModeTo
End If
'modify the 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 have not been successfully modified
WriteLineLog objService.Name & "(" & objService.Caption & ") &" ["& objService.Description &"] "
'record the changes that should be made by 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 the object
Set objService=Nothing
End Function
This is the end of the article on "how to optimize system service backup by VBS". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.