如果然后用vbs / hta中的or子句声明

时间:2011-04-27 21:20:22

标签: vbscript wmi hta

我有一段VBScript代码,根据某个名称条件查询Win32_Service。现在我们将一半环境迁移到新名称,因此我需要修改脚本以在任一条件下运行代码块。我在网上搜索过,但找不到任何例子。我正在粘贴两个代码块。我需要查询RGL或Reed作为服务名称的开头并返回结果。任何想法都表示赞赏。

 ' 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 

1 个答案:

答案 0 :(得分: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