VBS实现的系统服务备份优化代码
程序员文章站
2022-04-10 13:48:10
复制代码 代码如下:option explicit'○○○○○○○○○○○○○○○○○○○○○○○○○○○○○开始const blnvbsshowcaption=true'...
复制代码 代码如下:
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