【问题标题】:Passing objects as arguments in VBScript在 VBScript 中将对象作为参数传递
【发布时间】:2010-12-29 22:49:33
【问题描述】:

我正在开展一个项目,以使用 VBScript 捕获各种磁盘性能指标,并希望使用带有对象作为参数的子过程。在以下代码示例中,我所指的对象是objitem.AvgDiskQueueLength,它将提供磁盘队列长度的值。我还没有找到使它工作的方法,因为它被识别为字符串,然后不捕获该值。我的目标是让任何人都可以轻松地更改要捕获的计数器,只需在一个位置进行更改(过程调用参数)。我打算这样做的方式可能不是最好的,但我愿意接受建议。子过程调用如下。

PerfCounter "Average Disk Queue Length", "disk_queueLength", "objItem.AvgDiskQueueLength"

下面的代码是子程序。

Sub PerfCounter(CounterDescription, CounterLabel, CounterObject)
  Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48) 
  args_index = args_index + 1
  arrCriteria = split(command_line_args(args_index),",")
  strDriveLetter = UCase(arrCriteria(0))
  intCriticalThreshold = arrCriteria(1)
  intWarningThreshold = arrCriteria(2)
  For Each objItem in colItems
    With objItem
    WScript.Echo "objitem.name = " & objitem.name
    If InStr(objItem.Name, strDriveLetter & ":") > 0 Then
      intChrLocation = InStr(objItem.Name, strDriveletter)
      strInstanceName = Mid(objItem.Name, intChrLocation, 1)
    End If
    If strDriveLetter = strInstanceName AND InStr(objItem.Name, strDriveLetter & ":") > 0 Then
      If intActiveNode = 1  OR Len(intActiveNode) < 1 Then
        WScript.Echo "CounterDescription = " & CounterDescription
        WScript.Echo "CounterLabel = " & CounterLabel
        WScript.Echo "CounterObject = " & CounterObject
        If CInt(CounterOjbect) => CInt(intCriticalThreshold) Then 
          arrStatus(i) = "CRITICAL: " & strDriveLetter & ": " &  CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 2
          arrExitCode(i) = intExitCode
        ElseIf CInt(CounterOjbect) => CInt(intWarningThreshold) AND CInt(CounterObject) < CInt(intCriticalThreshold) Then
          arrStatus(i) = "WARNING: " & strDriveLetter & ": " & CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 1
          arrExitCode(i) = intExitCode
        Else
          arrStatus(i) = "OK: " & strDriveLetter & ": " & CounterDescription
          arrTrendData(i) = CounterLabel & "=" & CounterObject
          intExitCode = 0
          arrExitCode(i) = intExitCode
        End If
        Else
          PassiveNode CounterDescription, CounterLabel
        End If
      End If
    End With
  Next
  i = i + 1
  ReDim Preserve arrStatus(i)
  ReDim Preserve arrTrendData(i)
  ReDim Preserve arrExitCode(i)
End Sub

【问题讨论】:

    标签: performance vbscript wmi disk


    【解决方案1】:

    你为什么不能这样做......

    PerfCounter "Average Disk Queue Length", "disk_queueLength", objItem.AvgDiskQueueLength
    

    【讨论】:

      【解决方案2】:

      要传递一个对象,你必须传递一个对象,而不是字符串。
      要使此方法按预期工作,您必须在过程调用之前拥有该对象,但在您的代码示例中,您似乎正在尝试传递一个您没有的对象。一个工作示例:

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      UseFileSystemObject objFSO
      
      Sub UseFileSystemObject( objfso)
       'Now I can use the FileSystemObject in this procedure.
      End Sub
      

      但是像这样调用 UseFileSystemObject 过程是行不通的,

      UseFileSystemObject "objFSO"
      

      因为您传递的是字符串而不是对象。
      我能想到的完成你想要的唯一方法是使用 select 语句来编写对象的适当属性,就像这样。

      Call PerfCounter "Average Disk Queue Length", "disk_queueLength", "AvgDiskQueueLength"
      
      Sub PerfCounter(CounterDescription, CounterLabel, CounterObjectAttribute)
          Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfDisk_PhysicalDisk",,48) 
          For Each objItem in colItems
              Select Case CounterObjectAttribute
                  Case "ObjectAttribute1"
      
                  Case "ObjectAttribute2"
      
                  Case "AvgDiskQueueLength"
                      Wscript.Echo objItem.AvgDiskQueueLength
              End Select
          Next
      End Sub
      

      因此,在选择中,您必须为每个可以使用的属性添加一个大小写,但它允许您将字符串传递到过程中。我可能在这个问题上很遥远,但我不知道如果你没有先得到对象,你怎么能传递一个对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-28
        • 2012-07-13
        • 2023-04-02
        相关资源
        最近更新 更多