我正在尝试让此脚本在重新启动之前运行文本文件并停止服务,并启动在计算机重新启动后未自动启动的任何服务。获取服务名称,状态和启动模式列表并逐行创建逗号分隔文本文件的脚本可以正常工作。在这里它是供参考(取自互联网,在我的旅行中丢失了链接。稍微修改。):
Const ForAppending = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("service_list.txt", _
ForWriting, True)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service")
For Each objService in colListOfServices
objLogFile.Write objService.Name & ","
objLogFile.Write objService.StartMode & ","
objLogFile.Write objService.State
objLogFile.Writeline
Next
objLogFile.Close
下一位逐行读取文件,将所有服务的状态与机器关闭前记录的服务状态进行比较。如果它们匹配,则不执行任何操作,如果它们不同,则启动服务:
Const ForReading = 1
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objServiceName = objWMIService.get("Win32_Service.Name='" & ServiceName & "'")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("\\some path\service_list.txt",ForReading,True,-2)
Do Until objFile.AtEndOfStream
fLine = Split(objFile.ReadLine,",")
'wscript.echo fLine(2)
if InStr(fLine(2),"Running") then
'wscript.echo "it was running!"
if objServiceName.Started then
'do nothing
else
'Set servicetostart = objWMIService.ExecQuery ("Select " & ServiceName & " from Win32_Service Where Name ='Alerter'")
'servicetostart.StartService()
'Result = objServiceName.StartService
'If 0 <> Result Then
' wscript.echo "Start " & ServiceName & " error:" & Result
'End If
objServiceName.StartService
'wscript.echo Servicename & "could not start with error: " & Result
end if
end if
'wscript.echo objServiceName
Loop
截至目前,我正在尝试启动该服务时收到错误。我收到“提供商失败代码:80041004来源:SWbemObjectEX”。我一直在查看有关此错误的帖子并尝试修复建议。另外,正如你所看到的,我一直在尝试各种变化,但我恐怕只是在猜测。
那么就我的问题而言,是什么导致了“提供商失败”?我在这里查找了Win32_Service类的这些信息:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394418%28v=vs.85%29.aspx#methods
在这里查找方法:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa393660%28v=vs.85%29.aspx
但一直无法解决我出错的地方。
谢谢,
乔
在旁注,我正在测试的服务,即。确保服务正在启动,创建文本文件,然后停止服务并运行“启动服务”代码是Windows Defender。服务名称为“WinDefend”。
最终工作代码:
Const ForReading = 1
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("\\vmware-host\Shared Folders\Documents\Biffduncan\Monthly Server Maintanence\service_list.txt",ForReading,True,-2)
Do Until objFile.AtEndOfStream
fLine = Split(objFile.ReadLine,",")
Set objService = objWMIService.get("Win32_Service.Name='" & fLine(0) & "'")
if InStr(fLine(2),"Running") then
'wscript.echo "it was running!"
if objService.Started then
'do nothing
else
Result = objService.StartService()
if Result <> 0 then
wscript.echo "The service: " & objService.Name & " did not start with error: " & Result
else
wscript.echo "Service " & objService.Name & " started"
end if
end if
end if
Loop
答案 0 :(得分:0)
错误代码0x80041004表示WMI提供程序在初始化后遇到错误。但是,错误代码没有说明错误的原因,也没有提供任何细节。尝试运行WBEMTest
或WMIDiag
来追踪错误。还要检查事件日志中的相关错误/警告。如果其他一切都失败了,请尝试rebuilding the WMI repository。
至于你的代码,我要做的第一件事是把它剥离到最低限度,以避免潜在的错误来源:
Set wmi = GetObject("winmgmts://./root/cimv2")
Set svc = wmi.Get("Win32_Service.Name='WinDefend'")
rc = svc.StartService
WScript.Echo rc
另外,我不建议在某个随机时间点将服务状态写入文件,然后根据该文件的内容尝试启动服务。自创建文件以来,无法保证启动模式未被更改,或者甚至不再安装服务。
是否应启动服务由其StartMode
属性指示,因此请检查设置为Auto
的服务。设置为Manual
的服务将由系统按需启动,因此无需启动它们只是因为它们在您拍摄快照时正在运行。
qry = "SELECT * FROM Win32_Service WHERE StartMode='Auto'"
For Each svc In wmi.ExecQuery(qry)
If Not svc.Started Then svc.StartService
Next