【问题标题】:If then statement with an or clause in vbs/hta在 vbs/hta 中带有 or 子句的 if then 语句
【发布时间】:2011-04-27 21:20:21
【问题描述】:

我有一段 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 

【问题讨论】:

    标签: vbscript wmi hta


    【解决方案1】:

    只需在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多