服务状态检查失败

时间:2015-12-29 21:23:43

标签: service command-line vbscript wmi windows-server-2008-r2

我正在使用Windows 2008 Server R2。我发现这个VBScript应该检查服务是启动还是停止。

这是脚本:

'Declare Variables
Dim objWMIService, objProcess, colProcess, Status, strComputer, strService

'Assign Arguments
strComputer = WScript.Arguments(0)
strService = WScript.Arguments(1) 
Status = False

'Check For Arguments - Quit If None Found
If Len(strService) < 1 Then
    Wscript.echo "No Arguments Entered - Exiting Script"
    WScript.Quit
End If

'Setup WMI Objects
Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM Win32_Service WHERE DisplayName = '" & strService & "'")

'Check For Running Service
For Each objProcess In colProcess
    If InStr(objProcess.DisplayName, strService) > 0 And objProcess.State = "Running" Then
        Status = True
    End If
Next

If Status = True Then
    WScript.Echo "Service: " & UCase(strComputer) & " " & strService & " Running"
Else
    WScript.Echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
End If

通过命令行我像这样调用脚本

CSCRIPT ServiceCheckScript.vbs LOCALHOST "Print Spooler"

命令行的响应是

  

... \ ServiceCheckScript.vbs(20,1)(null):0x80041017

我看到0x80041017错误是指查询返回空值的结果,但我不确定为什么会这样。

1 个答案:

答案 0 :(得分:0)

上述代码的一些问题:

  1. 验证您是否从WMI查询中获得了结果,因此您不会尝试使用null。在if colProcess.count > 0 then

  2. 中使用结果
  3. 通过将代码包含在参数验证中来删除额外的.quit。使用参数计数比字符串函数wscript.arguments.count = 2更便宜/更干净,因为您只是检查以确保它们不是空的。如果您需要更复杂的验证,则需要更多逻辑。

  4. 没有任何理由使用instr(objProcess.Displayname, strService),因为您的WMI查询已经指定结果等于此where DisplayName = strService中的服务显示名称。

  5. 在循环内部执行服务状态的条件检查更简单/更清楚。

  6. 这是我的例子。

    'Declare Variables
    Dim objWMIService, objProcess, colProcess, Status, strComputer, strService
    
    'Verify arguments were passed
    if WScript.Arguments.Count = 2 then
       'Assign Arguments
       strComputer = WScript.Arguments(0)
       strService = WScript.Arguments(1) 
    
       'Setup WMI Objects
       Set objWMIService = GetObject("winmgmts:"& "{impersonationLevel=impersonate}!\\" & _
                           strComputer & "\root\cimv2") 
       Set colProcess = objWMIService.ExecQuery ("SELECT DisplayName, Status, State FROM " & _
                        "Win32_Service WHERE DisplayName = '" & strService & "'")
    
       'Ensure there were results returned
       if colProcess.count > 0 then
          'Check For Running Service
          For Each objProcess In colProcess
             If objProcess.State = "Running" Then
                WScript.Echo "Service: " & UCase(strComputer) & " " & strService & " Running"
             else
                WScript.Echo "Service: " & UCase(strComputer) & " " & strService & " Not Running"
             End If
          Next
       else
          WScript.Echo "Service: " & UCase(strComputer) & " " & strService & " Does not exist"
       end if
    end if
    

    编辑:只需添加我使用有效和无效的服务名称验证Win 8.1和Server 2012R2中的上述代码。但是,我会添加更多错误检查,例如验证计算机参数是否有效,以确保您的WMI查询不会不必要/莫名其妙地失败。