| Subcribe via RSS

VBS:Windows指定系统服务自动重启(多台)

五月 21st, 2008 Posted in VBS < by John.Lv >
下载: reset.vbs
'指定系统服务名称
servicename=chr(39)&amp;"系统服务名称"&amp;chr(39)
'指定日期
mydate=now
? my_day = day(mydate)
? my_month = month(mydate)
? my_year=year(mydate)
my_date = my_year &amp;? my_month &amp; my_day
'指定分区(系统第二的磁盘分区)
Set fso = CreateObject("Scripting.FileSystemObject")?
if fso.DriveExists("d:\")="True" then
getfso=fso.GetDrive("d:")
else
getfso=fso.GetDrive("e:")
end if
'指定log目录
if fso.folderExists(getfso&amp;"\log")=Flase then
fso.CreateFolder(getfso&amp;"\log")
end if
'log文件名
if fso.fileExists(getfso&amp;"\log\service_"&amp;my_date&amp;".log")=Flase then
fso.CreateTextFile(getfso&amp;"\log\service_"&amp;my_date&amp;".log")
set objNF=fso.OpenTextFile(getfso&amp;"\log\service_"&amp;my_date&amp;".log",8)
else
set objNF=fso.OpenTextFile(getfso&amp;"\log\service_"&amp;my_date&amp;".log",8)
end if
'判断hosts列表是否存在
if fso.fileExists("hosts")=Flase then
msgbox "无法读取服务器列表"
wscript.quit
end if
Const ForReading = 1
'读取Serverlist like linux /etc/hosts
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("hosts", ForReading)
set objlocator=createobject("wbemscripting.swbemlocator")
Do Until objFile.AtEndOfStream
strLine= objFile.ReadLine
'定义#不读取和ip以192.开始的
? If left(strLine,1)&lt;&gt;"#" and left(strLine,4)="192." Then
? strComputer=strLine
? objNF.writeline "server:"&amp;ComputerName&amp;" ip:"&amp;strComputer&amp;"? starttime:"&amp;now
? objNF.writeline ""

'关闭Services
Set objWMIService = objlocator.connectserver(strComputer ,"root\cimv2")
Set colListOfServices1 = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State = 'Running' and Name = "&amp;servicename)
For Each objService1 in colListOfServices1
objService1.StopService()
Next
'开启Services
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State &lt;&gt; 'Running' and Name = "&amp;servicename)
For Each objService in colListOfServices
objService.ChangeStartMode "Automatic" 'Manual,Automatic,Disabled
objService.StartService()
Next
end if
Loop

objFile.Close
objNF.close
msgbox "Services Restart Complete"

Leave a Reply