2011-04-27 42 views
0

我有一段VBScript代码查询Win32_Service基于特定的名称条件。现在我们将一半环境迁移到新名称,所以我需要修改脚本以在任一情况下运行代码块。我搜索了网页,但找不到任何示例。我正在粘贴两个代码块。我需要查询RGL或Reed作为服务名称的开始并返回结果。任何想法都表示赞赏。如果那么声明带有或条款在vbs/hta

' Initialize Objects. 
    Set g_objWMIService = GetObject("winmgmts:\\.") 
    Set g_colServices = g_objWMIService.InstancesOf("Win32_Service") 

    If c_Debug Then h_divStatus.style.display = "block" 

    For Each g_objService In g_colServices 
    'Status "Checking service..." 
    If c_Reedonly Then 
     If LCase(Left(g_objService.DisplayName, 4)) = LCase("RGL ") Then 
'  'If c_Debug Then 
'  'WScript.Echo "Name: " & g_objService.Name & vbCrLf & _ 
'  ' "Display Name: " & g_objService.DisplayName & vbCrLf & _ 
'  ' " Description: " & g_objService.Description & vbCrLf & _ 
'  ' " Path Name: " & g_objService.PathName & vbCrLf & _ 
'  ' " Start Mode: " & g_objService.StartMode & vbCrLf & _ 
'  ' " State: " & g_objService.State & vbCrLf 
'  'AddRowLinePartial(g_objService.Name) 
     AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
     End If 
    Else 
     AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
    End if 
    Next 
    Else 
    For Each g_objService In g_colServices 
    'Status "Checking service..." 
    If c_Reedonly Then 
     If LCase(Left(g_objService.DisplayName, 4)) = LCase("Reed") Then 
'  'If c_Debug Then 
'  'WScript.Echo "Name: " & g_objService.Name & vbCrLf & _ 
'  ' "Display Name: " & g_objService.DisplayName & vbCrLf & _ 
'  ' " Description: " & g_objService.Description & vbCrLf & _ 
'  ' " Path Name: " & g_objService.PathName & vbCrLf & _ 
'  ' " Start Mode: " & g_objService.StartMode & vbCrLf & _ 
'  ' " State: " & g_objService.State & vbCrLf 
'  'AddRowLinePartial(g_objService.Name) 
     AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
     End If 
    Else 
     AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
    End if 
    Next 

回答

0

只需在您的If语句中使用Or关键字。你可以一次做两个。

' Initialize Objects. 
Set g_objWMIService = GetObject("winmgmts:\\.") 
Set g_colServices = g_objWMIService.InstancesOf("Win32_Service") 

If c_Debug Then h_divStatus.style.display = "block" 

For Each g_objService In g_colServices 
    'Status "Checking service..." 
    If c_Reedonly Then 
     If LCase(Left(g_objService.DisplayName, 4)) = LCase("RGL ") _ 
       Or LCase(Left(g_objService.DisplayName, 4)) = LCase("Reed") Then 
' 'If c_Debug Then 
' 'WScript.Echo "Name: " & g_objService.Name & vbCrLf & _ 
' ' "Display Name: " & g_objService.DisplayName & vbCrLf & _ 
' ' " Description: " & g_objService.Description & vbCrLf & _ 
' ' " Path Name: " & g_objService.PathName & vbCrLf & _ 
' ' " Start Mode: " & g_objService.StartMode & vbCrLf & _ 
' ' " State: " & g_objService.State & vbCrLf 
' 'AddRowLinePartial(g_objService.Name) 
      AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
     End If 
    Else 
     AddRowLinePartial "h_tblServices", g_objService.DisplayName, g_objService.Name, g_objService.State, g_objService.StartMode 
    End If 
Next