VBS:Windows指定系统服务自动重启(多台)
五月 21st, 2008 Posted in VBS < by John.Lv >
下载: reset.vbs
'指定系统服务名称
servicename=chr(39)&"系统服务名称"&chr(39)
'指定日期
mydate=now
? my_day = day(mydate)
? my_month = month(mydate)
? my_year=year(mydate)
my_date = my_year &? my_month & 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&"\log")=Flase then
fso.CreateFolder(getfso&"\log")
end if
'log文件名
if fso.fileExists(getfso&"\log\service_"&my_date&".log")=Flase then
fso.CreateTextFile(getfso&"\log\service_"&my_date&".log")
set objNF=fso.OpenTextFile(getfso&"\log\service_"&my_date&".log",8)
else
set objNF=fso.OpenTextFile(getfso&"\log\service_"&my_date&".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)<>"#" and left(strLine,4)="192." Then
? strComputer=strLine
? objNF.writeline "server:"&ComputerName&" ip:"&strComputer&"? starttime:"&now
? objNF.writeline ""
'关闭Services
Set objWMIService = objlocator.connectserver(strComputer ,"root\cimv2")
Set colListOfServices1 = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State = 'Running' and Name = "&servicename)
For Each objService1 in colListOfServices1
objService1.StopService()
Next
'开启Services
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State <> 'Running' and Name = "&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"
servicename=chr(39)&"系统服务名称"&chr(39)
'指定日期
mydate=now
? my_day = day(mydate)
? my_month = month(mydate)
? my_year=year(mydate)
my_date = my_year &? my_month & 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&"\log")=Flase then
fso.CreateFolder(getfso&"\log")
end if
'log文件名
if fso.fileExists(getfso&"\log\service_"&my_date&".log")=Flase then
fso.CreateTextFile(getfso&"\log\service_"&my_date&".log")
set objNF=fso.OpenTextFile(getfso&"\log\service_"&my_date&".log",8)
else
set objNF=fso.OpenTextFile(getfso&"\log\service_"&my_date&".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)<>"#" and left(strLine,4)="192." Then
? strComputer=strLine
? objNF.writeline "server:"&ComputerName&" ip:"&strComputer&"? starttime:"&now
? objNF.writeline ""
'关闭Services
Set objWMIService = objlocator.connectserver(strComputer ,"root\cimv2")
Set colListOfServices1 = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State = 'Running' and Name = "&servicename)
For Each objService1 in colListOfServices1
objService1.StopService()
Next
'开启Services
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where? State <> 'Running' and Name = "&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"