3

我试图让这个脚本在重新启动之前运行一个文本文件并停止服务,并启动任何在机器重新启动后没有自动启动的服务。获取服务名称、状态和启动模式列表并逐行创建逗号分隔文本文件的脚本工作正常。这里供参考(取自互联网,在我的旅行中丢失了链接。稍作修改。):

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
4

1 回答 1

0

错误代码 0x80041004 表示 WMI 提供程序在初始化后遇到错误。但是,错误代码没有说明错误的原因,也没有提供任何细节。尝试运行WBEMTestWMIDiag跟踪错误。还要检查事件日志中的相关错误/警告。如果一切都失败了,请尝试重建 WMI 存储库

至于您的代码,我要做的第一件事是将其剥离到最低限度,以避免潜在的错误来源:

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
于 2013-08-09T17:58:47.673 回答